JSP Forms and User Input :

JSP Forms :

JSP form is a way to interact with user input with web server or database server.The browser uses two methods to pass this information to web server. These methods are GET and POST Method.

GET method :

Imagine that you have a form on a JSP Page and clicking the "submit" button sends the data in the form back to the server, as "name=value " pairs. Choosing GET as the "method" will append all of the data to the URL and it will show up in the URL bar of your browser.

JSP Forms :

JSP form is a way to interact with user input with web server or database server.The browser uses two methods to pass this information to web server. These methods are GET and POST Method.

GET method :

Imagine that you have a form on a JSP Page and clicking the "submit" button sends the data in the form back to the server, as "name=value " pairs. Choosing GET as the "method" will append all of the data to the URL and it will show up in the URL bar of your browser.


The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.

The GET method is the default method to pass information from browser to web server.

The page and the encoded information are separated by the ? character as follows:

http://www.javawebtutor.com/hello?key1=value1&key2=value2

Post method :

A POST on the other hand will send the information through a socket back to the web server and it won't show up in the URL bar. You can send much more information to the server this way - and it's not restricted to textual data either.
It is possible to send files and even binary data such as serialized Java objects.

Reading Form Data using JSP :

JSP handles form data parsing using the following methods depending on the requirement.

  • getParameter(): You can call getParameter() method on request object to get the value of a form parameter.
  • getParameterValues(): You can Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
  • getParameterNames(): Call this method if you want a complete list of all parameters in the current request.
  • getInputStream(): Call this method to read binary data stream coming from the client.

GET Method Example Using URL :

Here is a simple URL which will pass two values using GET method. http://localhost:9999/JSPFORMS/inex.jsp?firstname=Mukesh&lastname=Kumar Below is index.jsp JSP program to handle input given by web browser. We are going to use getParameter() method which makes it very easy to access passed information:

index.jsp
<html>
<head>
<title>GET Method Example to Read Form Data</title>
</head>
<body>
<center>
<h2>Hello these are the values:</h2>
<ul>
<li><p><b>First Name:</b>
   <%= request.getParameter("firstname")%>
</p></li>
<li><p><b>Last  Name:</b>
   <%= request.getParameter("lastname")%>
</p></li>
</ul>
</body>
</html>

Browser Output :

JSP Forms

GET Method Example Using Form :

We are going to create simple example in eclipse which passes two values using HTML FORM and submit button.

Step 1 : Create Dynamic Web Project

Open Eclipse and go to File -> New -> Project and select Dynamic Web Project in the New Project wizard screen.

JSP Forms

Provide the name of the project as JSPFORMSEXAMPLE . Once this is done, select the target runtime environment as Apache Tomcat v6.0. This is to run the project inside Eclipse environment. In configuration select default and press Next -> Next -> Next.

JSP Forms


Step 2 : Create Login Page:

Create a jsp file inside WebContent directory of the project and provide the name of this file as login.jsp and add following code into this file.

login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
	<form action="result.jsp" method="GET">
		First Name: <input type="text" name="firstname"> <br>
		Last Name: <input type="text" name="lastname" /> <input type="submit"
			value="Submit" />
	</form>
</body>
</html>

Step 3 : Create result.jsp Page:

Create result.jsp file inside WebContent directory of your Project with following entry .It will collect the user input.

result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Using GET and POST Method to Read Form Data</title>
</head>
<body>
	<h1>Using GET Method to Read Form Data</h1>
	<ul>
		<li><p>
				<b>First Name:</b>
				<%=request.getParameter("firstname")%>
			</p></li>
		<li><p>
				<b>Last Name:</b>
				<%=request.getParameter("lastname")%>
			</p></li>
	</ul>




Step 4 : Run the Project :

Right click on login.jsp file -> Run as -> Run on Server select tomcat ->Next and Finish.You can see following output in your Browser.

JSP Forms

Provide the values and click on Submit button.

JSP Forms

you will see following output in your browser.In URL bar you can see the request parameter as query String.

JSP Forms


POST Method Example Using Form :

Let us do little modification in login.jsp file to handle POST methods. change method="GET" to method="post".After changing the login.jsp follow the same steps mentioned above and analyse the final output.This time in URL bar request data will not display.Request will processed using message body as shown below.

JSP Forms





comments powered by Disqus