Monday, June 27, 2016

Java : doGet vs doPost


doGet vs doPost



In doget() method
  1. There is a security problems are arrival because when the user or client submit the form the associated user name or password has been display on the address bar.
  2.  client can send limited amount of data.
  3.  when the users tries to request for any Read Only data.
doPost() method
  1. when the user submit the form the associated user name or password are not display on the addressbar.
  2. client can send more then data.
  3.   used for Insertion /updation / deletion of data ,

Java : WebServer Database Connection and retreive data

WebServer Database Connection and retrieve data


Notes:

  1. doPost(..) method is generally used for Insertion /updation / deletion of data , 
  2. doGet(..) is used when the users tries to request for any Read Only data.

Pre -Req :

  1.  Using MySQL as DB Server
  2. Download "MySQL connector J.Jar" driver file and paste it into "Project/WebContent/WEB-INF/lib" folder path.
  3. Add the same file into project build directory.
  4. Download "Apache Commons DbUtils" jar file and paste it into "Project/WebContent/WEB-INF/lib" folder path.
  5. Repeat step 3.

DBHelper method: 
The below method return the results of the query in the form of Query list.


    public List SelectQuery(String sURL,String sUsn,String sPassword,String sQuery){
        ResultSet rs=null;
        MapListHandler rstoList=new MapListHandler();
        Map<String,Object> MapQuery=new HashMap<String,Object>();
        List resList=null;
       
        try {
            Class.forName("com.mysql.jdbc.Driver");   
            Connection connection = DriverManager.getConnection(sURL,sUsn, sPassword);           
            Statement st=(Statement) connection.createStatement();
             rs=st.executeQuery(sQuery);
             resList= rstoList.handle(rs);
             rs.close();
             st.close();
             connection.close();           
               
      }catch(Exception e){
          System.out.println("Failed to make connection!");
          e.printStackTrace();
      }       
        return resList;
    }


 Serverlet :
 Calling Method 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    
        DBHelper DBHelp=new DBHelper();
         PrintWriter out=response.getWriter();
        out.print(DBHelp.SelectQuery(sURL,sUsername,sPassword,"Select * from mydb.login;"));
    }

Wednesday, June 22, 2016

Creating a simple web Service

Creating a simple web Service


https://www.youtube.com/watch?v=Av6zh817QEc
http://crunchify.com/servlet-tutorial-getting-starting-with-jsp-servlet-example/

Tips:
  1. External Jars go into - Project\WebContent\WEB-INF\lib
  2. Java files (Serverlets) goes into - Project\Java Resources\src\com.package\HERE
  3. JSP files - Project\WebContent\
  4. html files -  Project\WebContent\
  5. JSP files are html files but with different extension 
  6. The 1st html file shown is index.jsp , so this shoulkd not be deleted.



Configuring TomCat WebServer to Eclipse
1. download eclise
2. Download ApacheTomcat zip file Eg:8.73
3. Unzip AcpacheTomcat into a folder
4. Open Eclipse > Window >Preference>Server > Run time Environment>Add
5. In Add Window , Select the respective Apache Server Downloaded and Ck on finish

Validation :
1. In Eclipse > Window >perspective > Open perspective> web
2. In server Tab below> Right ck > new > Server
3. Select the correct server
4. Finish
5. A server gets added into the server tab , Double click on it to open the server settings
6. In Server Locations> Select use custom location and provide new path to save ur server.
7. Also user can change the port numbers in "Ports" Tab
8. save
9. perform right ck on the new server created in server tab> Select start
10. Open browser tab in eclipse > open "http://localhost:8080"
11. User should get 404 error meaninf server is configured correctly and running

Create project

1.    In Eclipse>File> New >Dynamic web project
2. Projectname= webapp-02, Make sure Dynamic Web Module version=2.5, Target runtime =8.0 (or what ever downloaded)
3. next ,again Next
4. Make sure "Generate web.xml" is checked.
5. Finish
6. Project>Rt clk>new >jsp file
7. file name "index.jsp"
8. Next and finish

9. Double ck "Deployment Descriptor" to open web.xml file
10. project>java resources > src>Create a new package "com.pack1
11. project>java resources > src>select package "com.pack1>right ck and create new serverlet>class name as "MyServerlet" (MyServerlet is the name specified  )
12 .open index.jsp and add below lines at the end (note: they are case sensitive):
    <h1>Welcome</h1>
    <form action="MyServerlet">
    <input type="submit" value="send">
    </form>

    </body>
    </html>

13. Open MyServerlet.java > add below code in doGet method :
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        out.print("hell noi");

    }

14. save all
15. project >Right Ck>Run as "Run on Server"