To send information to web server, we can use two methods: GET Method and POST Method.
The GET method sends the encoded user information separated by the ? character appended to the page URL.
http://www.java2s.com/hello?key1=value1&key2=value2
The GET method is the default method to send information to web server. Since the GET method appends plain text string to the URL. We should avoid using GET method to send password or other sensitive information to the server.
The GET method also has size limitation. We can send only 1024 characters in a request string. This information sent is accessible getQueryString() and getParameter() methods of request object.
POST method is more reliable method of sending information to the server. This method sends the information as a separate message.
JSP handles this type of requests using getParameter() method to read simple parameters and getInputStream() method to read binary data stream coming from the client.
JSP handles form data using the following methods.
Method | Description |
---|---|
getParameter() | returns the value of a form parameter. |
getParameterValues() | returns multiple values, for example checkbox. |
getParameterNames() | returns a complete list of all parameters. |
getInputStream() | read binary data stream coming from the client. |
The following code shows how to get parameters using GET method.
The following code uses the getParameter() method to read parameters.
<html> <body> <b>First Name:</b><%= request.getParameter("first_name")%><br/> <b>Last Name:</b><%= request.getParameter("last_name")%> </body> </html>
Save the code above as main.jsp. And copy the file to the Tomcat deployment folder.
First we would type in the parameters and their value directly in the URL. Enter the following URL in your broswer address bar.
http://localhost:8080/main.jsp?first_name=James&last_name=Smith
We can use the following html form to pass the values to JSP main.jsp page.
<html> <body> <form action="main.jsp" 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>
Save this HTML in a file called Hello.htm and put it in Tomcat-installation-directory/webapps/ROOT directory.
Access it with http://localhost:8080/Hello.htm
The following line in the HTML code calls the jsp page we created to accept parameter and indicates that we are going to use the GET method to pass the information.
<form action="main.jsp" method="GET">
Create a html page as follows and use POST methods for the form. Save it as Hello.htm.
<html> <body> <form action="main.jsp" method="POST"> 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>
Below is main.jsp JSP program to handle input from by web browser using GET or POST methods.
<html> <body> <b>First Name:</b><%= request.getParameter("first_name")%><br/> <b>Last Name:</b><%= request.getParameter("last_name")%> </body> </html>
The following code shows how to use the JSP page to get the checkbox data from a form.
Here is the html form code. It uses the POST method and calls the main.jsp page.
<html> <body> <form action="main.jsp" method="POST" target="_blank"> <input type="checkbox" name="a" checked="checked" /> A <input type="checkbox" name="b" /> B <input type="checkbox" name="c" checked="checked" /> C <input type="submit" value="Select Subject" /> </form> </body> </html>
The following is the main.jsp code to handle input for checkbox button.
<html> <body> <b>A Flag:</b><%= request.getParameter("a")%> <b>B Flag:</b><%= request.getParameter("b")%> <b>C Flag:</b><%= request.getParameter("c")%> </body> </html>
The following code shows how to use getParameterNames() method from HttpServletRequest to read all the available form parameters.
This method returns an Enumeration object that contains the parameter names.
<%@ page import="java.io.*,java.util.*" %> <html> <body> <% Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<b>" + paramName + ":</b>\n"); String paramValue = request.getHeader(paramName); out.println(paramValue); } %> </body> </html>