==========
<html>
<head>
<title>post Request Demo </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="DemoServlet" method="post">
<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>PostRequestEx</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>
PostRequestEx.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 PostRequestEx extends HttpServlet {
protected void processRequest(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 sage = request.getParameter("sage");
if(name.equals("") || address.equals("")){
out.println("Invalid data try again");
return;
}
int age = 0;
try{age = Integer.parseInt(sage);}
catch(Exception ex){
out.println("Invalid age try again");
return;
}
if(age < 18){
out.println("Sorry client, you are not allowed for fashion show.");
out.println("<be /><br />");
out.println("B'cos you are minor");
out.println("<be /><br />");
out.println("Try after few years");
}else{
out.println("Welcome to fashion show");
out.println("<center><b>");
out.println("You are name is "+name);
out.println("<be /><br />");
out.println("You are address is "+address);
out.println("<be /><br />");
out.println("You are age is "+age);
out.println("<be /><br />");
out.println("</b></center>");
out.println("<be /><br />");
out.println("We are dispacthed entry card to your address");
out.println("<be /><br />");
out.println("Wish you all the Best");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
No comments:
Post a Comment