File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Session in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Session in JSP" Watch "Session in JSP" New topic

Session in JSP

Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
Hi all,

I have a JSP page "register1.jsp" that contains a form with a hidden field <input type=hidden name=from_register1 value=true>. The form submits to itself.

In the beginning of the JSP page I move the the sent field into a session variable, and then sendRedirect the page to itself.

It some kind of a trick to prevent the annoying alert window if the user hits the refresh button after he sumbitted the form.

The probelm is that after that I sendRedirect the page, the session variable seems to being disappeared.

After I submit, it STILL prints:

session.getAttribute("from_register1") DOES NOT EXIST!!!

Why? I used to do such things in ASP and it worked fine.
[ May 03, 2005: Message edited by: Joseph Sweet ]

We must know, we will know. -- David Hilbert
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Just try to print the session id prior to use session.

Then match the ids. Whether id is the same both the times or not.
Joseph Sweet
Ranch Hand

Joined: Jan 29, 2005
Posts: 327
The session id's are the same before and after i submit the form.

but the attribute "from_register1" is gone after i sendRedirect("register1.jsp")...

karthik venkatesan

Joined: Dec 31, 2004
Posts: 26

try the below code,
<%@ page
contentType="text/html; charset=ISO-8859-1"
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META http-equiv="Content-Style-Type" content="text/css">


<% if (request.getParameter("from_register1")!=null)
session.setAttribute("from_register1", "yes");
// Removed the send.Redirect Code from here
if (session.getAttribute("from_register1")!=null)
System.out.println("session not equal to null");
out.println("session.getAttribute(\"from_register1\") EXISTS!!!<br>");
out.println("session.getAttribute(\"from_register1\") DOES NOT EXIST!!!<br>");
<!-- added action attribute in the form tag here -->
<form method=post name=myForm action = "index.jsp">
<input type=hidden name=from_register1 value=true>
<input type=submit value=Submit>

hope this helps.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

An important thing to remember is that sendRedirect does not stop the execution of your servlet/jsp.

Here is what's happening when you post:
  • You send the post.
  • At line 3, you check to see if the param is null. If not you set the session attribute and redirect. (remember what I just said)
  • On line 10 you check to see if the session attribute is null. If not you print a message to the out stream (you never actually see this because you have already sent the redirect but you've probably got an InvalidStateException in your logs).
  • You then remove the attribute on line 13.

  • The simple way to fix this is to add a return; statement just after your call to sendRedirect.
    As a rule of thumb, you should always put a return statment after calls to sendRedirect or forward.
    [ May 03, 2005: Message edited by: Ben Souther ]

    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Joseph Sweet
    Ranch Hand

    Joined: Jan 29, 2005
    Posts: 327
    Thanks, but you did not understand the problem.

    With what you have done, you do not call the destination page right after the form was submitted, so if you hit the browser's refresh button, there is an annoying alert window asking if you want to resend the form variables.

    If I did not plan to get rid of that annoying window, I would not do the session trick in the first place.
    [ May 03, 2005: Message edited by: Joseph Sweet ]
    Ben Souther

    Joined: Dec 11, 2004
    Posts: 13410

    Your version still adds then removes the session attribute in one request:

    There is no else clause. They are two different if blocks.
    Joseph Sweet
    Ranch Hand

    Joined: Jan 29, 2005
    Posts: 327
    Thanks Ben,
    I will remember that.
    I agree. Here's the link:
    subject: Session in JSP
    It's not a secret anymore!