J2EE Programming

Hands On No. 2 : Reading Parameters using GET and POST methods in Servlet

Source Code of Form(index.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 name="frmmain" method="post" action="p1servlet">
<table width='300px' border='0' align='center'>
<tr>
<td>Enter Name</td>
<td><input type="text" name='txtname'></td>
</tr>
<tr>
<td>Enter Age</td>
<td><input type="text" name='txtage'></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value='Check'></td>
</tr>
</table>
</form>
</body>
</html>

Source Code of Servlet(p1servlet.java)
                                

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

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 Dhaval Mehta
*/
public class p1servlet 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
*/
int i=2020;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* 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 p1servlet</title>");
out.println("</head>");
out.println("<body>");

String s = request.getParameter("txtname");
int age = Integer.parseInt(request.getParameter("txtage"));

out.println("<table width='300px' border='0' align='center'>");
out.print("<tr><td>Hi " + s +"</td></tr>");



if(age >=18)
{
out.print("<tr><td><span style='color:green'>Congrats You are elligible for Voting</span></td></tr>");
}
else
{
out.print("<tr><td><span style='color:red'>Sorry You are not elligible for Voting</span></td></tr>");

}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}

// <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>

}

Source Code of 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>p1servlet</servlet-name>
<servlet-class>p1servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>p1servlet</servlet-name>
<url-pattern>/p1servlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>