Share on Google+Share on Google+

The 'session' Attribute of JSP page Directive

Advertisement
This section, illustrate about 'session' attribute of page directive with help of a example. It specifies if the generated servlet can access the session or not

The 'session' Attribute of JSP page Directive

This section, illustrate about 'session' attribute of page directive with help of a example. It specifies if the generated servlet can access the session or not. This directive attribute has Boolean data type. If the vale of "session" sets to "True" , then the session persist to the next page , if sets to "False" , then you can't use the session object or <jsp:useBean> element with scope="session" in JSP page. The default value of session is "True" .

EXAMPLE

Following 3 JSP pages are included in this example :

  • inputname.jsp
  • session.jsp
  • sessionresult.jsp

In this Example, User input their name, and redirected to "session.jsp" page , where he will have to click "Continue" hyperlink , which again redirected him to "sessionresult.jsp", which display his inputted name.

inputname.jsp

<HTML>

<HEAD><TITLE>INPUT YOUR NAME</TITLE></HEAD>

<BODY>

<FORM METHOD=POST ACTION="session.jsp">

What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>

<P><INPUT TYPE=SUBMIT value="Submit" value="Submit">

</FORM>

</BODY>

</HTML>

 

session.jsp

<%@page language="java" %>

<%

String name = request.getParameter( "username" );

session.setAttribute( "theName", name );

%>

<HTML>

<HEAD><TITLE>CLICK TO CONTINUE</TITLE></HEAD>

<BODY>

<A HREF="sessionresult.jsp">Click to Continue</A>

<BR><A HREF="inputname.jsp">Click Here to go back</A>

</BODY>

</HTML>

 

sessionresult.jsp

<%@page language="java" session="true" %>

<HTML>

<HEAD><TITLE>DISPALYING YOUR NAME</TITLE></HEAD>

<BODY>

<font color=red size="10">Hello, <%= session.getAttribute( "theName" ) %></font>

</BODY>

</HTML>

 

OUTPUT

After submitting :-

After clicking "Click to Continue"

Download source code

Advertisements

Posted on: June 12, 2010 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.