Pr.Pg Next Pg

Reading Form Data using Servlet tutorials

  • Servlets handles form data parsing automatically using the following methods depending on the situation:

    1. getParameter(): Returns the value of a request parameter as a String, or null if the parameter does not exist. request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. You should only use this method when you are sure the parameter has only one value.

    2. getParameterValues(): Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

    3. getParameterNames(): Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.

GET Method Example Using URL

  • Here is a simple URL which will pass two values to NewServlet program using GET method.

http://localhost:8080/WebApplication1/URLdata?First_Name=Rahul&Last_Name=Dev&Sex=Male

 

 

Example: Servlet program for form processing

Sam Sir

//Program name URLdata.java

// Program for form processing

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class URLdata extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String title = "Using GET Method to Read Data Passed from URL Bar";

out.print("<html>\n" +

"<head><title>" + title + "</title></head>\n" +

"<body bgcolor=\"#f0f0f0\">\n" +

"<h1 align=\"center\">" + title + "</h1>\n" +

"<ul>\n" +

" <li><b>First Name</b>: "

+ request.getParameter("First_Name") + "\n" +

" <li><b>Last Name</b>: "

+ request.getParameter("Last_Name") + "\n" +

"<li><b>Sex</b>: "

+request.getParameter("Sex")+

"</ul>\n" +

"</body></html>");

}

}

Output

Servlet URL data

 

  • Note: Pass this following URL :

  • http://localhost:8080/WebApplication1/URLdata?First_Name=Rahul&Last_Name=Dev&Sex=Male in the

  • browser after URLdata.java compilation


 

Example: Servlet program for form processing using GET method

Sam Sir

//Program name doGetData.java

// Program for form processing using GET method

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 doGetData extends HttpServlet

{

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

String firstName = request.getParameter("firstname");

String lastName = request.getParameter("lastname");

String sex = request.getParameter("sex");

String email = request.getParameter("email");

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Processed Data</title>");

out.println("</head>");

out.println("<body>");

out.println("<Center><h1> Form Processing using Get method </h1></Center><BR>");

out.println("First Name : "+firstName+"<BR>");

out.println("Last Name : "+lastName+"<BR>");

out.println("Sex : "+sex+"<BR>");

out.println("Email : "+email+"<BR>");

out.println("</body>");

out.println("</html>");

}

}

 

 

 

 

HTML form for user input for doGetData.java servlet

Sam Sir

<!--index.html-->

<!-- HTML form for user input -->

 

<HTML>

<HEAD>

<TITLE>

Form Processing

</TITLE>

</HEAD>

<BODY>

<FORM action="doGetData" method="get">

<P>

<LABEL for="firstname">First name: </LABEL>

<INPUT type="text" name="firstname"><BR>

<LABEL for="lastname">Last name: </LABEL>

<INPUT type="text" name="lastname"><BR>

<LABEL for="email">email: </LABEL>

<INPUT type="text" name="email"><BR>

<INPUT type="radio" name="sex" value="Male"> Male<BR>

<INPUT type="radio" name="sex" value="Female"> Female<BR>

<INPUT type="submit" value="Send"> <INPUT type="reset">

</P>

</FORM>

</BODY>

</HTML>

Output

GET method

 

Servlet GET method

 

Note: Try to enter the form details and then click submit button to see the result on your local machine where tomcat is running. Based on the input provided, it will generate similar result as mentioned in the above example.


POST Method Example Using Form Tag

  • Let us do little modification in the above servlet, so that it can send data using POST methods. Below is doPostData.java servlet program to handle input given by web browser using POST methods.

 

 

Example: Servlet program for form processing using POST method

Sam Sir

//Program name doPostData.java

//Program for form processing using POST method

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 doPostData extends HttpServlet

{

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

String firstName = request.getParameter("firstname");

String lastName = request.getParameter("lastname");

String username=request.getParameter("username");

String password=request.getParameter("password");

String sex = request.getParameter("sex");

String email = request.getParameter("email");

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<head>");

out.println("<title>Processed Data</title>");

out.println("</head>");

out.println("<body>");

out.println("<Center><h1> Form Processing using Post method </h1></Center><BR>");

out.println("First Name : "+firstName+"<BR>");

out.println("Last Name : "+lastName+"<BR>");

out.println("User Name : "+username+"<BR>");

out.println("Password : "+password+"<BR>");

out.println("Email : "+email+"<BR>");

out.println("Sex : "+sex+"<BR>");

out.println("</body>");

out.println("</html>");

}

}

 

 

 

 

HTML form for user input for doPostData.java servlet

Sam Sir

<!-- index2.html -->

<HTML>

<HEAD>

<TITLE>

Form Processing

</TITLE>

</HEAD>

<BODY>

<FORM action="doPostData" method="post">

<P>

<LABEL for="firstname">First name: </LABEL>

<INPUT type="text" name="firstname"><BR>

<LABEL for="lastname">Last name: </LABEL>

<INPUT type="text" name="lastname"><BR>

<LABEL for="username">User Name: </LABEL>

<INPUT type="text" name="username"><BR>

<LABEL for="password">Password: </LABEL>

<INPUT type="password" name="password"><BR>

<LABEL for="email">email: </LABEL>

<INPUT type="text" name="email"><BR>

<INPUT type="radio" name="sex" value="Male"> Male<BR>

<INPUT type="radio" name="sex" value="Female"> Female<BR>

<INPUT type="submit" value="Send"> <INPUT type="reset">

</P>

</FORM>

</BODY>

</HTML>

Output

POST method

 

Servlet post method

 

Passing Checkbox Data to Servlet Program

  • Checkboxes are used when more than one option is required to be selected.

 

 

Example: Servlet program to pass Checkbox Data to Servlet

Sam Sir

//Program name checkdata.java

// Program to Pass Checkbox Data to Servlet Program

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class checkdata extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String title = "Reading Checkbox Data";

String hd=request.getParameter("HardDisk");

out.print("<html>\n" +

"<head><title>" + title + "</title></head>\n" +

"<body bgcolor=\"#f0f0f0\">\n" +

"<h1 align=\"center\">" + title + "</h1>\n" +

"<ul>\n" +

" <li><b>Hard disk Flag : </b>: "

+ request.getParameter("HardDisk") + "\n" +

" <li><b>Mouse Flag: </b>: "

+ request.getParameter("Mouse") + "\n" +

" <li><b>Key Board Flag: </b>: "

+ request.getParameter("KeyBoard") + "\n" +

" <li><b>Monitor Flag: </b>: "

+ request.getParameter("Monior") + "\n" +

"</ul>\n" +

"</body></html>");

}

}

 

 

 


 

HTML form for user to tick a checkbox

Sam Sir

<!—index3.html -->

<HTML>

<HEAD>

<TITLE>

Form Processing

</TITLE>

</HEAD>

<BODY>

<FORM action="checkdata" method="get">

<P>

<INPUT type="checkbox" name="HardDisk" checked="checked"/> Hard Disk

<INPUT type="checkbox" name="Mouse" checked="checked"/> Mouse

<INPUT type="checkbox" name="KeyBoard"/> Key Board

<INPUT type="checkbox" name="Monior"/> Monitor

<INPUT type="submit" value="Submit" />

</P>

</FORM>

</BODY>

</HTML>

Output

Servlet checkbox data process

 

Servlet checkbox data processing

Pr.Pg border                                              Next Pg