JSP
JSP Tutorial for Beginners: Learn in 7 Days
What is JSP? Java Server Pages (JSP) is a technology which is used to develop web pages by...
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.
JSP handles form data processing by using following methods:
It is used to get the value of the form parameter.
It is used to return the multiple values of the parameters.
It is used to get the names of parameters.
It is used to read the binary data sent by the client.
Example:
In this example, we have taken a form with two fields."username" and "password" with a submit button
Action_form.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> <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>
Action_form_process.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> <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:
Action_form.jsp
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
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:
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.
What is JSP? Java Server Pages (JSP) is a technology which is used to develop web pages by...
What is Expression Language (EL)? Expression Language (EL) is mechanism that simplifies the...
What is JSP LifeCycle? JSP Life Cycle is defined as translation of JSP Page into servlet as a JSP...
What is JSP Filter? Filters are used for filtering functionality of the Java web application. They...
What are Cookies? Cookies are the text files which are stored on the client machine. They are used...
What are JSP Directives? JSP directives are the messages to JSP container. They provide global...