In: Computer Science
In a Servlet, how do you read HTML Form Data (data from the previous Web page) into the Servlet?
Servlet :
A Servlet is a small java program that runs within a web server. Servlets recieve and respond to requests from web clients, usually across HTTP, the Hypertext Transfer Protocol. To implement this interface, we can write a generic Servlet that extends javax.
Servlet technology is robust and scalable because of java language. Before servlet, CGI ( Common Gateway Interface ) scripting language was common as a server-side programming language.
Reading data from the HTML form into servlet :
To read data from the HTML Form data ( data from the previous web page ) into the servlet, it uses two methods. These methods are GET Method and POST Method.
GET Method :
The GET Method sends the encoded user information attached to the page request. The page and the encoded information are seperated by the ? ( question mark ) symbol as follows
http:/ / www.test.com/ hello?key1 = value1&key2 = value2
The GET method is the default method to pass information from browser to web server and it produces a long string that appears in the browser's Location:box. We can't use the GET Method if we want to pass the password or other sensitive information to the server.
This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable and Servlet handles this type of requests using doGet() method.
POST Method :
A generally more reliable method of passing information to a backend program is the POST method. This packages the information in exactly the same way as GET method, but instead of sending it as a text string after a ? ( question mark ) symbol in the URL it sends it as a seperate message. This message comes to the backend program in the form of the standard input which we can parse and use for our processing. Servlet handles this type of requests using doPost() method.
Reading Form data using Servlet :
Servlet handles form data parsing automatically using the following methods depending on the situation -
GET Method Example using URL :
Here is the simple example URL which will pass two values to HelloForm program using GET method.
http://localhost:8080/HelloForm? first_name = ZARA&last_name = ALI
Given below is the HelloForm.java servlet program to handle input given by web browser. We are going to use getParameter() method which makes it very easy to access passed information.
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloForm extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<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" + "</ul>\n" + "</body>" + "</html>" ); } }
GET Method Example Using Form :
Here is a simple example which passes two values using HTML FORM and submit button. We are going to use same Servlet HelloForm to handle this input.
<html> <body> <form action = "HelloForm" method = "GET"> First Name: <input type = "text" name = "first_name"> <br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form> </body> </html>
POST Method Example Using Form :
Let us do little modification in the above servlet, so that it can handle GET as well as POST methods. Below is HelloForm.java servlet program to handle input given by web browser using GET or POST methods.
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloForm extends HttpServlet { // Method to handle GET method request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<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" + "</ul>\n" + "</body>" "</html>" ); } // Method to handle POST method request. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }