Followers

Generic Servlet Example

index.html
==========


<html>
    <head>
        <title>get Request Demo </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="DemoServlet" method="get">
            <p>Please enter your name : <input type="text" name="suserName" ></p>
            <p>Please enter your address : <input type="text" name="saddress" ></p>
            <p>Please enter your Age : <input type="text" name="sage" ></p>
            <p><input type="submit" value="Send" ></p>
           
        </form>
    </body>
</html>

web.xml
========

<?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>DemoServlet</servlet-name>
        <servlet-class>GenericServletEx</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DemoServlet</servlet-name>
        <url-pattern>/DemoServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>



GenericServletEx.java
======================

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class GenericServletEx extends GenericServlet {

    protected void processRequest(ServletRequest request, ServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("suserName");
        String address = request.getParameter("saddress");
        String age = request.getParameter("sage");
       
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Get Request Handling.</title>");          
            out.println("</head>");
            out.println("<body>");
            out.println("Welcome to this application...");
            out.println("<br/><br/>");
            out.println("<center>");
            out.println("<br/><br/>");
            out.println("<h1> Hello  " + name + " How are you!");
            out.println("<br/><br/>");
            out.println("Do you have water problem at <b> "+address+"</b>");          
            out.println("<br/><br/>");
            out.println("How do you feel your life at this age of  <b> "+age+"</b>");          
            out.println("<br/><br/>");
            out.println("Bye, Bye");
            out.println("</center>");
           
            out.println("</body>");
            out.println("</html>");
       
    }


    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        processRequest(req,res);
    }


}

No comments:

Post a Comment