Share on Google+Share on Google+

JSP: View Session

Ads
The view consists of the Html page or the jsp components that provide the user interface. View is what we see. It is mainly for presentation.

JSP: View Session

        

The view consists of the Html page or the jsp components that provide the user interface. View is what we see. It is mainly for presentation.

Java Bean: - It is a reusable component. It contains the setter and getter methods to set and get the values from the jsp page or database.

In session whenever a request comes for any resource, a unique token is generated by the server and transmitted to the client by the response object and stored on the client machine as a cookie. We can also say that the process of managing the state of a web based client is through the use of session IDs. Session IDs are used to uniquely identify a client browser, while the server side processes are used to associate the session ID with a level of access. Thus, once a client has successfully authenticated to the web applicatiion, the session ID can be used as a stored authentication voucher so that the client does not have to retype their login information with each page request.

In this program we are using the java Beans to set the values from the html page and set it to the jsp page which will works like a controller.
setAttribute(): is used to set the object. A key is set to look up the object. 
getAttribute(): is used to get the object.

The code of the program is given below:

<html>
	<head>
		<title>View and Session</title>
	</head>
	<body>
		<form method = "get" action = "JspAndSessionJsp.jsp">
		Enter your first Name : <input type = "text" name = "firstName"><br>
		Enter your last Name  : <input type = "text" name = "lastName"><br>
		Enter your email      : <input type = "text" name = "email"><br>
		Enter the address      : <input type = "text" name = "address"><br>
		<input type = "submit" name = "submit" value ="submit"><br>
		</form>
	</body>
</html>

 

package Mybean;
public class JspAndSession{
	private String firstName;
	private String lastName;
	private String email;
	private String address;
		public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
		public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
		public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
		public String getEmail() {
		return email;
	}
    public void setEmail(String email) {
		this.email = email;
	}
}

 

<%@ page import = "Mybean.JspAndSession"%>
<html>
	<body>
		<h1>Your Request</h1>
		Here is the information that you submitted to us for processing.<br>
		<%
		  JspAndSession jas = new JspAndSession();
             jas.setFirstName(request.getParameter("firstName"));
			 jas.setLastName(request.getParameter("lastName"));
			 jas.setEmail(request.getParameter("email"));
			 jas.setAddress(request.getParameter("address"));
		%>
		<% session.setAttribute("jas",jas ); %>
		<a href = "JspSessionView.jsp">Click Here to go forward</a>
	</body>
</html>

The output of the program is given below:

Download this example.

Advertisements

Posted on: December 17, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: JSP: View Session   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

Advertisement