Monday 14 July 2014

Analyze Hello World Application

main.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="get" action="/servlet/HelloWorld">
            <input type="submit">
        </form>
    </body>
</html>

Whenever user clicks the button, then the url-pattern “/servlet/HelloWorld” will be invoked. In the form tag, method attribute has the value get, so doGet method of the corresponding servlet is called. If method attribute has the value post, then doPost method of the corresponding servlet is called.

web.xml
Open the web.xml file from the configuration files

web.xml file contains below configurations
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


Observe the above URL, it specifies, from servlet project run the url-pattern HelloWorld.

<servlet>
   <servlet-name>Hello</servlet-name>
   <servlet-class>Hello</servlet-class>
</servlet>

<servlet-mapping>
   <servlet-name>Hello</servlet-name>
   <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

whenever a request comes for url-pattern "/HelloWorld", servlet container is going to match the servlet-name with the url-pattern and will try to find the corresponding servlet-class and will forward the control to the Servlet Hello.

Hello.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Hello extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       
       PrintWriter out = res.getWriter();
       res.setContentType("text/html");
       String str = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> Hello World " +
                    "</h1> </body>" +
                    "</html>";
       out.println(str);
   } 
}

Usually java programs, start their execution from main method, but servlets don't have main method. When a server dispatches a request to a servlet, the service method of the servlet called. The service method accepts two objects, one is request (contiains all the information to process this request like input data, headers etc.,) and other is response object, it is used to return the response.

Usually we won't override the service method in HttpServlet class. We override doXXX() methods. 'XXX' will be get, post, put etc., It is the service method, calls doGet(), doPost() like methods based on the given input. In the above program doGet() is overridden, if form method is of type 'get', then 'doGet' method will be invoked.

Each time the web server receives a get request for this servlet, the server invokes 'doGet' of this servlet.

Once user clicks the button , then doGet method of the Hello.java will be executed.

PrintWriter out = res.getWriter()
Returns a PrintWriter object that can send character text to the client.

res.setContentType("text/html")
Sets the content type of the response being sent to the client.

out.println(str)
Prints a String and then terminates the line.







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment