Share on Google+Share on Google+

Preventing the creation of a Session in a Jsp Page

Ads
In jsp we have been provided the implicit session object. In jsp the session is by default true, if we don't declare a session = "true" inside the directive, then the session is still available as it is by default true.

Preventing the creation of a Session in a Jsp Page

        

In jsp we have been provided the implicit session object. In jsp the session is by default true, if we don't declare a session =  "true" inside the directive, then the session is still available as it is by default true. There is not any such use to declare a session false, but to learn just imagine if we declare a session = "false", then what will happen. 

For check this make one jsp page or html page where we have given two field, one is of name and the second one is of password. Now make  a controller which will control the request and response object. Declare the session as false in the page, it will be declared inside the directive. We will try to retrieve the values of the field by using the request.getParameter(), request is the implicit object which has been provided to us by the jsp and the getParameter() is the method of request object. We can see that by declaring the session as false the values will not be able to retrieved by the controller. It will throw a exception.

The code of the program is given below:

<html>
	<head>
		<title>Welcome </title>
	</head>
	<body>
		<form method = "post" action = "SessionLess.jsp">
			<font size = 6>Enter your name<input type = 
"text" name = "name"></font><br><br>
			<font size = 6>Enter your password<input type=
"password" name = "pwd" ></font><br><br>
			<input type = "submit" name = "submit" value = "submit" >
		</form>
	</body>
</html>

 

<%@ page session="false"%>
<%  
    String name = request.getParameter("name");
	String password = request.getParameter("pwd");
	if(name.equals("Williams") && password.equals("abcde"))
	{
		session.setAttribute("username",name);
		response.sendRedirect("NextPageOfSessionLess.jsp");
	}
	else
	{
		response.sendRedirect("NoSession.html");
	}
	%>

 

<html>

	<head>

		<title>Welcome in In the program session</title>

	</head>

	<body>

      <font size = 6>Hello</font> <%= session.getAttribute("username") %>

	</body>

      </html>

The output of the program is given below:

Download this example.

Advertisements

Ads

Posted on: December 19, 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: Preventing the creation of a Session in a Jsp Page  

Post your Comment


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