Sunday, January 19, 2014

Java JSP - Servlet Tutorial -- Part 4























[Enter
Post Title Here]


JSP - Servlet


Now you know how to configure Tomcat, How to run JSP alone, How to run Servlet alone.  And here we are going to see all these in a single project..I.e  Communicating a JSP to Servlet through Tomcat.

Create a dynamic web project



 Create the JSP file



  • In the "Project Explorer" view, R-click
    "WebContent" / New / JSP.
  • Name your JSP - to be consistent with the example, name
    it "Home.jsp"
  • place the JSP the following code:




Paste this in Home.jsp


<%@pagelanguage="java"contentType="text/html;
charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;
charset=ISO-8859-1"
>
<title>My
first JSP
</title>
</head>
<body>
      <formaction="FirstServlet">
            Please
enter a color 
<br>
<inputtype="text"name="color"size="20px">
<inputtype="submit"value="submit">
      </form>
</body>
</html>






Create the Servlet 










  • place the following code in the class:


package
com.cgs.controller;

import
java.io.IOException;
import
java.io.PrintWriter;

import
javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class
HelloWorldServlet
 */
@WebServlet("/FirstServlet")
public class
HelloWorldServlet extends HttpServlet {
        private static final long
serialVersionUID = 1L;


        /**
         * @see HttpServlet#doGet(HttpServletRequest
request, HttpServletResponse response)
         */
        protected void
doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
                        // reading the user
input
                        String color=
request.getParameter("color");
                        PrintWriter out =
response.getWriter();
                        out.println (
                        "<!DOCTYPE
html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">\n" +
"<html> \n" +
                         "<head> \n" +
                         "<meta
http-equiv=\"Content-Type\" content=\"text/html;
charset=ISO-8859-1\"> \n" +
                         "<title> My first
jsp</title> \n" +
                        "</head>
\n" +
                         "<body> \n" +
                         "<font size=\"12px\"
color=\"" +
                        color +
"\">" +
                        "Hello
World" +
                         "</font> \n" +
                         "</body> \n" +
                         "</html>" );

        }

}



Define your servlet in
"web.xml"


<?xmlversion="1.0"encoding="UTF-8"?>
<web-appid="WebApp_ID"version="2.4"
      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">

      <servlet>
            <servlet-name>Hello</servlet-name>
            <servlet-class>com.cgs.controller.HelloWorldServlet</servlet-class>
      </servlet>
      <servlet-mapping>
            <servlet-name>Hello</servlet-name>
            <url-pattern>/FirstServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
            <welcome-file>Home.jsp</welcome-file>
      </welcome-file-list>
</web-app>

Select  CGS-JSPServletproject explorer, right click  Run As à Run On Server à select Tomcat 7


 What has happened under the hood?


While we run servlet project,
1)      First it will look into WebContent/WEB-INF/web.xml  for the initial jsp file to be loaded under<welcome-file-list>. If its null, it will search for “index” or “default” html/jsp files are there.  Once it identify the file, iThis will compile our Servlet class and launch Home.jsp
2)      Once we enter a color on Input Box and click on “Submit” , the JSP file search for form à action . Here its <formaction="FirstServlet">. The control will come back to Web.XML ‘s<url-pattern>and will search for  <servlet-name>under</servlet-mapping>. Here “Hello” is the<servlet-name>.
3)      Once it identify the  <servlet-name>It will search for which servlet class to be loaded by looking into<servlet-class>under<servlet> . Here it’s<servlet-class>com.cgs.controller.HelloWorldServlet</servlet-class>andsimply generate an HTTP Get request.I.e"FirstServletis now mapped tocom.cgs.controller.HelloWorldServlet
4)      HelloWorldServlet will do some magic which we have written under doGet(), update the response object and finish processing. Tomcat will get notified and it will simply draw that response on screen.


The URL formation flow is as below