==========
<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>DemoServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/DemoServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>RdforwardEX</servlet-name>
<servlet-class>RdforwardEx</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RdforwardEX</servlet-name>
<url-pattern>/RdforwardEX</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>IncludeEx</servlet-name>
<servlet-class>IncludeEx</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>IncludeEx</servlet-name>
<url-pattern>/IncludeEx</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>
DemoServlet.java
================
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DemoServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse 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");
int ag = Integer.parseInt(age);
out.println("Before delegation");
out.println("<br /><br />");
if(ag < 18){
RequestDispatcher rd=request.getRequestDispatcher("RdforwardEX");
rd.forward(request, response);
}
else{
out.println("<br /><br />");
RequestDispatcher rd=request.getRequestDispatcher("IncludeEx");
rd.include(request, response);
}
out.println("<br /><br />");
out.println("After delegation:");
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
IncudeEx.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 IncludeEx extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("suserName");
String address = request.getParameter("saddress");
String age = request.getParameter("sage");
out.println("Welcome to "+name+" you nearest branch at "+address);
out.println("<br /><br />");
out.println("Including request...");
out.println("<br /><br />");
}
}
RdforwardEX.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 RdforwardEx extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("suserName");
String address = request.getParameter("saddress");
String age = request.getParameter("sage");
int ag = Integer.parseInt(age);
out.println("Forward request...");
out.println("<br /><br />");
out.println("Hi "+name+ " You are not eligible for membership please try after "+(18-ag)+" Years");
}
}
No comments:
Post a Comment