Servlets

J2EE specifies the Servlet interface and several extensions and implementations:

Upon receiving a post request the container locates an instance of the HttpServlet class that will handle the request. The container creates a Java HttpServletRequest object and an HttpServletResponse object and passes them to the doPost method of the selected servlet.

The response object contains an output stream connected back to the browser. The doPost method generates HTML strings and writes them into the output stream. Essentially, it is sending HTML back to the browser to be displayed.

Example

This servlet simply sends a web page containing the string "Hello World" back to the browser:

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

public class Responder extends HttpServlet {

   protected void doGet(
     HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
        processRequest(req, resp);
    }

   protected void doPost(
     HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
        processRequest(req, resp);
    }

   protected void processRequest(
       HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
        try {
          resp.setContentType("text/html");
          PrintWriter out = resp.getWriter();
          out.println(
            "<html> <head> <title> Response </title> </head> <body>"
         );

          out.println("Hello, World<br />");
          out.println("</body></html>");
         }
         catch(Exception e) {

         }
    }
 }

Translation

The first time a request is sent to a JSP, the container translates the JSP into an extension of the HttpJspPage class. It creates an instance of this class and calls the doPost or doGet method as above. Subsequent requests sent to this JSP are forwarded to the servlet.

Example

Here's a JSP:

<html>
<head><title> Search Me </title></head>
<body>

<%
   for(int i = 0; i < 5; i++) {
%>
   Hello, World! <br />
<%}%>
</body>
</html>

Here's the translation:

public class jspdemo extends HttpJspBase {

    public void _jspService(
      HttpServletRequest request, HttpServletResponse  response)
    throws IOException, ServletException {

        JspFactory _jspxFactory = null;
        PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        String  _value = null;
        try {

            if (_jspx_inited == false) {
                _jspx_init();
                _jspx_inited = true;
            }
            _jspxFactory = JspFactory.getDefaultFactory();
            response.setContentType("text/html;charset=ISO-8859-1");
            pageContext = _jspxFactory.getPageContext(this, request, response,
         "", true, 8192, true);

            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();

            // HTML // begin [file="C:\\downloads\\jakarta-tomcat-3.2.3\\webapps\\myapp\\demo.jsp";from=(0,0);to=(4,0)]
                out.write("<html>\r\n<head><title> Search Me </title></head>\r\n<body>\r\n\r\n");
            // end
            // begin [file="C:\\downloads\\jakarta-tomcat-3.2.3\\webapps\\myapp\\demo.jsp";from=(4,2);to=(6,0)]
                
                   for(int i = 0; i < 5; i++) {
            // end
            // HTML // begin [file="C:\\downloads\\jakarta-tomcat-3.2.3\\webapps\\myapp\\demo.jsp";from=(6,2);to=(8,0)]
                out.write("\r\n  Hello, World! <br />\r\n");
            // end
            // begin [file="C:\\downloads\\jakarta-tomcat-3.2.3\\webapps\\myapp\\demo.jsp";from=(8,2);to=(8,3)]
                }
            // end
            // HTML // begin [file="C:\\downloads\\jakarta-tomcat-3.2.3\\webapps\\myapp\\demo.jsp";from=(8,5);to=(10,7)]
                out.write("\r\n</body>\r\n</html>");
            // end

        } catch (Exception ex) {
            if (out != null && out.getBufferSize() != 0)
                out.clearBuffer();
            if (pageContext != null) pageContext.handlePageException(ex);
        } finally {
            if (out != null) out.flush();
            if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
        }
    }
}