Saturday, October 22, 2011

JSP Request Handle by servlet

  This is the post about how to map jsp to servlet. Reviews post I mention how the jsp page works. Jsp just like HTML page. But we need method for handle user request through jsp pages. For that we use servlet. In between jsp pages and servlet we use descriptor called web.xml to map jsp with servlet URL. Follow example illustrate way of servlet working.  

you access the application by navigating to URL http://localhost:8080/serveltMappingTute/ the web server serves the form.html file.

When you fill the form field and click on send form button, browser sends the HTTP POST(form initiate for POST method if form initiate GET method then request send with GETs ) request with name parameter. Based on the servlet mapping in web.xml(in form action indicate what servlet to call.in there mention servlet name in web.xml), the web container delegates the request to WelcomeServlet class.


index.jsp


<html> 
 <head> 
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head> 
   <body>
 <h1>This Servlet jsp Mapping Example</h1>
      <form method="POST" action="exampleServlet">
      Name : <input type="text" name="name" value="" /><br><br> 
      Age  :<input type="text" name="age" value="" /><br><br> 
     <input type="submit" value="send" name="send" /> 
 </form>
  </body> 
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <servlet>
        <servlet-name>exampleServlet</servlet-name>
        <servlet-class>Servlet.exampleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>exampleServlet</servlet-name>
        <url-pattern>/exampleServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>
</web-app>
How to define servlet in web.xml

<servlet>
        <servlet-name>exampleServlet</servlet-name>
        <servlet-class>Servlet.exampleServlet</servlet-class>
    </servlet>

 In between this <servlet></servlet> tags we can define servlet.<servlet-name> this tag use for define name for servlet. we can use any name for that.But in action must use that name.<servlet-class> in this tag we use wrile path where servlet java class located. this <servlet-name> use for mapping servlet with URL

How to map servlet in web.xml


      <servlet-mapping>
        <servlet-name>exampleServlet</servlet-name>
        <url-pattern>/exampleServlet</url-pattern>
     </servlet-mapping>

This code segment use for servlet mapping.Servlet mapping is used to map the URLs to a servlet.<servlet-name> is previously define servlet name and  <url-pattern> is URL pattern for servlet. that means any request from this URL pattern redirect to that servlet. 

 Servlet class(exampleServlet.java)


package Servlet;

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;

/**
 *
 * @author 
 */
public class exampleServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet exampleServlet</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<h1> Welcome  " + request.getParameter("name") + "</h1>");
            out.println("<h1>You are " + request.getParameter("age") + " Years Old</h1>");
            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}



request.getParameter("name")- use for get request parameter from name variable.

normally in servlet we not use to write html code.only use for handle request. but here we use to display request.

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet exampleServlet</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<h1> Welcome  " + request.getParameter("name") + "</h1>");
            out.println("<h1>You are " + request.getParameter("age") + " Years Old</h1>");
            out.println("</body>");
            out.println("</html>");


Outputs




Thursday, October 20, 2011

hello world in jsp



 In this post i would like to tell behavior of jsp page. Jsp is just like HTML but it has special tag for handle client request. in above figure you can understand how it work.JSP source code runs on the web server in the JSP Servlet Engine. The JSP Servlet engine dynamically generates the HTML and sends the HTML output to the client's web browser.
following example for print hello world techmemoryspace.wordpress.com in jsp page.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World techmemoryspace.wordpress.com!</h1>
</body>
</html>
Out Put
URL : http://localhost:8080/jspHelloWorld/
(i did at my computer so that's you i used as local host. otherwise use server machine URL).