Servlets, JSP, Struts and MVC

1.General Information about servlet:


Servlet Request & Response Model:

  • There are three different players in this picture: browser, web server, and servlet
  • Container.
  • In many cases, a web server and a servlet container are running in a same machine even in a single virtual machine.
  • So they are not really distinguished in many cases. Anyway, the role of the web server is to receive HTTP request and then passes it to the web container or servlet container
  • Which then creates Java objects that represent “HTTP request” and a “session” and then dispatches the request to the servlet by invoking service() method defined in the servlet.        

  • The most common form of client requests are HTTP GET and HTTP POST
  • requests.
  • In the HTTP GET request, the user entered information is appended to the URL as a query string.
  • In the HTTP POST request, on the other hand, the user entered information is sent as data.

Servlet Classes and Interfaces:

This picture shows important servlet interfaces and classes. The ones in yellow color are Java interfaces while the ones in green color are Java classes.

Servlet Life Cycle

 

  • The init() method gets called once when a servlet instance is created for the first time.
  • And then service() method gets called every time there comes a new request.
  • Now service() method in turn calls doGet() or doPost() methods for incoming HTTP requests.
  • And finally when the servlet instance gets removed, the destroy() method gets called.
  • So init() and destroy() methods get called only once while service(),doGet(), and doPost() methods are called a number of times depending on how many HTTP requests are received.

Request Dispatcher Interface:

  • The RequestDispatcher interface is used to forward a request from a servlet to other resources, such as servlet or a Jsp page.
  • A Servlet Context is the directory in which the servlets are deployed in the web server. Servlet that are executing in the same server belong to the same context.
  • The forward method of this interface is used to delegate a task to the resource encapsulated by a particular interface object.
  • The getServletContext( ) method of the ServletConfig interface is used to obtain a reference to servlet context in which a servlet execute.
  • ServletConfig interface is used to store servlets startup configuration values and the initialization parameters.
  • The getServletConfig( ) method of the servlet interface is used to obtain information about the configuration clause of a servlet.

Environmental  Servlet APIs

  • The ServletConfig object servlet specific initialization parameters, while ServletContext receive webapps specific initialization parameters.
  • It is useful to think of the ServletConfig as the Servlet specific configuration, and the ServletContext as an interface to resource available from the servlet engine.

2.Steps for Simple Servlet Example:


Connecting First Jsp page to Last page through servlet.

Requirement:


  • Two Jsp page (First page and result Page)
  • One Web.xml (Deployment Descriptor)
  • Controller class (Contains doGet and doPost method), which forward the client request with the appropriate response.

Create Jsp page:

  • Create a Project in Tomcat webapps with specified folder structure,
  • <Projectname>    WED-INF
  • Here we will deploy all our Project pages.
  • <Projectname>    WED-INF    Classes    lib     web.xml

First Jsp page coding were given below:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="/TestServlet/key">
<TABLE BORDER=2 BORDERCOLOR="#009900">
<TR BGCOLOR="#FFFF66">
    <TD>NAME:</TD>
    <TD><INPUT TYPE="text" NAME="name"></TD>
   
</TR>
<TR BGCOLOR="#FFFF66">
    <TD>SEX</TD>
    <TD><INPUT TYPE="text" NAME="sex"></TD>
</TR>
<TR>
    <TD colspan="2" ALIGN="CENTER" BGCOLOR="#FFFF66">
<INPUT TYPE="submit" value="submit"></TD>
   
</TR>
</TABLE>
</FORM></BODY>
</HTML>

Result Jsp page coding were given Below.   
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
</HEAD>
<BODY>
<TABLE BORDER=2 BORDERCOLOR="#009900">
<TR BGCOLOR="#FFFF66">
    <TD>NAME:</TD>
    <TD><%=request.getParameter("name")%></TD>
   
</TR>
<TR BGCOLOR="#FFFF66">
    <TD>SEX</TD>
    <TD><%=request.getParameter("sex")%></TD>
</TR>
</TABLE>
</BODY>
</HTML>

Deployment Descriptor:


  • Web.xml is located inside the WEB-INF.
  • Which consists of servlet name and servlet url pattern for mapping the appropriate java class to the Jsp form which the  user gives request.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

   <servlet>
    <servlet-name>TestServlet</servlet-name>
   <servlet-class>com.company.FirstAction</servlet-class>
   </servlet>

   <servlet-mapping>
    <servlet-name> TestServlet </servlet-name>
      <url-pattern>/key</url-pattern>
  </servlet-mapping>

</web-app>
</web-app>

Controller class:


  • The class which extends HttpServlet. Will forward the suitable response for the request given by the user in the first page Jsp.
  • The class also handles IOServletException and ServletException.
  • The request and response are handled by HttpServletRequest Interface and HttpServletResponse Interface.
  • RequestDispacter method will forward the result to the end Jsp page.


package com.company;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FirstAction extends HttpServlet {
    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        RequestDispatcher rd = getServletContext().getRequestDispatcher
                ("/Result.jsp");
        rd.forward(req, res);
    }

}

Library files required for this Process are:
  • servlet-api

output Screen:

  • The first screen shot is first page of the project.

  • The Second  screen shot is the result page .



I hope all goes well! Look forward to seeing more Java tutorials from me in the future.

Comments

Popular posts from this blog

Hibernate Count Query

STRUTS ACTION - AGGREGATING ACTIONS IN STRUTS