The 'session' Attribute of JSP page Directive


 

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 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

0

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

<HTML>

1

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

<BODY>

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

2

</BODY>

</HTML>

 

3

OUTPUT

After submitting :-

4

After clicking "Click to Continue"

5

Download source code

Ads