Form Processing by Servlet
Form Processing by Servlet
Most of web applications are used to handle the incoming request of user through a client
i.e. web browser. Request generated from browser is a standard HTTP protocol request. Browser
renders a request according to HTTP protocol and sends request to server. Request contains header
and data that is processed by server. Server that handles request has inbuilt capability to understand
HTTP request and server starts to parse it. When server processes any request, it gets header and
data and intercepts them properly. Server implements a procedure that is used to accept the request
and to create response for that request.
One of the nice features of Java servlets is that all of this form parsing is handled
automatically. You simply call the getParameter method of the HttpServletRequest, supplying
the parameter name as an argument. Note that parameter names are case sensitive. You do this
exactly the same way when the data is sent via GET as you do when it is sent via POST. The return
value is a String corresponding to the uudecoded value of the first occurrence of that parameter
name. An empty String is returned if the parameter exists but has no value, and null is returned if
there was no such parameter. If the parameter could potentially have more than one value, as in the
example above, you should call getParameterValues instead of getParameter. This returns an
array of strings. Finally, although in real applications your servlets probably have a specific set of
parameter names they are looking for, for debugging purposes it is sometimes useful to get a full
list. Use getParameterNames for this, which returns an Enumeration, each entry of which can be
cast to a String and used in a getParameter call.
index.html
1 <html>
2 <head>
3 <title>This is Test Example</title>
4 </head>
5 <body>
6 <h1>Submit this form</h1>
7 <form action="ProcessForm" method="POST" enctype="">
8 Name :
9 <br/>
10 <input type="text" name="name"/>
11 <br/>
12 Phone Number :
13 <br/>
14 <input type="text" name="phone"/>
15 <br/>
16 Prefered Job type :
17 <br/>
18 <select name="jobtype">
19 <option>Full time</option>
20 <option>Part time</option>
21 <option>Weekend</option>
22 </select>
23 <br/>
24 Programming Language Known (Select mulitple pressing CTRL key):
25 <br/>
26 <select name="lang" size="4" multiple="multiple">
27 <option>Java</option>
28 <option>C</option>
29 <option>C#</option>
30 <option>C++</option>
31 <option>HTML</option>
32 <option>VB6</option>
33 <option>SQL</option>
34 <option>Javascript</option>
35 </select>
36 <br/>
37 Highest Qualification :
38 <br/>
39 <input type="radio" name="qualification" value="M Tech" />M Tech
40 <input type="radio" name="qualification" value="B Tech." />B Tech
41 <input type="radio" name="qualification" value="MCA" />MCA
42 <input type="radio" name="qualification" value="BCA" />BCA
43 <br/>
44 Select Prefered job location(s) :
45 <br/>
46 <input type="checkbox" name="locations" value="Delhi" />Delhi
47 <input type="checkbox" name="locations" value="Noida" />Noida
48 <input type="checkbox" name="locations" value="Pune" />Pune
49 <input type="checkbox" name="locations" value="Channai" />Channai
50 <br/>
51 Assress for communication :
52 <br/>
53 <textarea name="address" rows="4" cols="30">
54 </textarea>
55 <br/>
56
57 <input type="submit" value="Submit Now" />
58 </form>
59 </body>
60 </html>
ProcessForm.java
1
2 import java.io.*;
3 import javax.servlet.*;
4 import javax.servlet.http.*;
5
6 public class ProcessForm extends HttpServlet {
7
8 public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
9 response.setContentType("text/html");
10 PrintWriter out = response.getWriter();
11 String name = request.getParameter("name");
12 String phone = request.getParameter("phone");
13 String jobType = request.getParameter("jobtype");
14 String programmingLanguages[] = request.getParameterValues("lang");
15
16 String qualification = request.getParameter("qualification");
17 String locations[]=request.getParameterValues("locations");
18
19 String address = request.getParameter("address");
20
21 /////////////Printing received data/////////////////////
22 out.write("<br/>Name : " + name);
23 //data from text field
24 out.write("<br/>Phone Number : " + phone);
25 //data of drop down list
26 out.write("<br/>Job Type : " + jobType);
27 //data of drop down list with multiple select
28 out.write("<br/>Programming languages");
29 for (int i = 0; i < programmingLanguages.length; i++) {
30 out.write("<br> - " + programmingLanguages[i]);
31 }
32 //data of radio buttons
33 out.write("<br/>Qualification : " + qualification);
34
35 //Processing data of check boxes
36 out.write("<br/> Job Location(s) : ");
37 for (int i = 0; i < locations.length; i++) {
38 out.write("<br> - " + locations[i]);
39 }
40 //data from text area
41 out.write("<br/>Address : " + address);
42 out.close();
43 }
44
45 public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
46 doPost(request, response);
47 }
48 }
web.xml
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="2.5" 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_2_5.xsd">
3 <servlet>
4 <servlet-name>ProcessForm</servlet-name>
5 <servlet-class>ProcessForm</servlet-class>
6 </servlet>
7 <servlet-mapping>
8 <servlet-name>ProcessForm</servlet-name>
9 <url-pattern>/ProcessForm</url-pattern>
10 </servlet-mapping>
11 </web-app>
OUTPUTS :