JSP Form Processing
Forms are the common method in web processing. We need to send information to the web server and that information.
There are two commonly used methods to send and get back information to the web server.
- GET Method:
- This is the default method to pass information from browser to web server.
- It sends the encoded information separated by ?character appended to URL page.
- It also has a size limitation, and we can only send 1024 characters in the request.
- We should avoid sending password and sensitive information through GET method.
- POST Method:
- Post method is a most reliable method of sending information to the server.
- It sends information as separate message.
- It sends as text string after ?in the URL.
- It is commonly used to send information which are sensitive.
JSP handles form data processing by using following methods:
- getParameter():It is used to get the value of the form parameter.
- getParameterValues():It is used to return the multiple values of the parameters.
- getParameterNames()It is used to get the names of parameters.
- getInputStream()It is used to read the binary data sent by the client.
In this example, we have taken a form with two fields.”username” and “password” with a submit button
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Guru Form</title> </head> <body> <form action="action_form_process.jsp" method="GET"> UserName: <input type="text" name="username"> <br /> Password: <input type="text" name="password" /> <input type="submit" value="Submit" /> </form> </body> </html>
<%@ 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> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Form Processing</h1> <p><b>Welcome User:</b> <%= request.getParameter("username")%> </p> </body> </html>
Explanation of the code:
Code Line 10: Here we have defined a form and through which we have process the action to some other JSP. In action parameter, we add that JSP to which it has to be processed through GET method.
Here we are using GET method to pass the information i.e username and password.
Code Line 11-14: Here we are taking fields like username and password which are text fields, and we are getting the input from the user.
This input can be fetched using getParameter method. Also, we have submit button with type submit type which helps us to pass the field values into action_form_process.jsp
Code Line 14: Here we get the values of the input fields from action_form.jsp using request object’s getParameter method.
When we execute the above code, we get the following output:
When we execute action_form.jsp, we get a form with two fields username and password and a submit button.Then after entering username and password, we can click on submit, and it processes to next page which gives output as Form processing page with a welcome message.