Servlet Tutorial

Http Servlet

Http Servlet:

  1. HttpServlet is the sub-class of GenericServlet.
  2. HttpServlet contains all the life cycle methods of GenericServlet and the service () method of GenericServlet is further divided into the following two methods, they are
  3. public void doGet (HttpServletRequest, HttpServletResponse) throws ServletException, IOException 
    public void doPost (HttpServletRequest, HttpServletResponse) throws ServletException, IOException
    
  4. Whenever client makes a request, the servlet container (server) will call service () method, the service () method depends on type of the method used by the client application.
  5. If client method is get then service () method will call doGet () method and doGet () method internally creates the objects of HttpServletRequest and HttpServletResponse. Once doGet () method is completed its execution, the above two objects will be destroyed.

LIMITATIONS of get method:

  1. Whatever data we sent from client by using get method, the client data will be populated or appended as a part of URL. For example: http://localhost:7001/servlet/DDservlet?uname=scott&pwd=tiger
  2. Large amount of data cannot be transmitted from client side to server side.
  3. When we use post method to send client data, that data will be send as a part of method body and internally the service () method will called doPost () method by creating the objects of HttpServletRequest and HttpServletResponse.

ADVANTAGES of post method:

  1. Security is achieved for client data.
  2. We can send large amount of data from client to server.
  3. HttpServletRequest extends ServletRequest and HttpServletResponse extends ServletResponse.
  4. HttpServlet, HttpServletRequest and HttpServletResponse belong to a package called javax.servlet.http.*
  5. The request which we make from the client side that requests are known as http requests where as the responses which are given by a servlet are known as http responses.

Note:All real world applications always extends HttpServlet only and it is always recommended to overwrite either doGet () method or doPost () method.

Associated with servlet we have three names which are specified in web.xml, they are public URL name (known to everybody), deployer URL name or dummy name (known to that person who is deploying) and secret or internal URL name (known to servlet container or server).

  • The purpose of <servlet-mapping> is that it maps public URL name to deployer URL name.
  • The purpose of <servlet> is that it maps deployer URL name to actual Servlet class name.

Write a servlet which retrieves the data from database?

Answer:

Servlet program:

package ddrs;

import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;

public class RetrieveDataBaseServ extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        try {

            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            Connection con = DriverManager.getConnection("oracle:jdbc:thin:@localhost:1521:Hanuman", "scott", "tiger");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from emp");
            while (rs.next()) {

                pw.println(rs.getString(1) + " " + rs.getString(2));
            }
        } catch (Exception e) {
            res.sendError(504, "PROBLEM IN SERVLET...");
        }
    }
};

web.xml:

<web-app>
    <servlet>
        <servlet-name>pskills</servlet-name>
        <serclet-class>ddrs.RetrieveDataBaseServ</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>pskills</servlet-name>
        <url-pattern>psurl</url-pattern>
    </servlet-mapping>
</web-app>

In the above program we are making use of 'n' number of servlet classes, OracleDriver class; it is required to set the classpath for servlet-Api.jar and classes111.jar

For weblogic:

set classpath=F:\bea\weblogic81\server\lib\weblogic.jar;F:\oracle\ora92\jdbc\ lib\classes111.jar;

For Tomcat:

set classpath=F:\Program Files\Apache Software Foundation\Tomcat 5.5\common\ lib\servlet-api.jar;F:\oracle\ora92\jdbc\lib\classes111.jar;

When we run the above program on weblogic, it is not necessary to copy classes111.jar into lib folder of document root. Since, the weblogic server itself contains an existing jar file called ojdbc14.jar to deal with OracleDriver.