wood burning stoves 2.0*
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
Author

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")...

why?
karthik venkatesan
Greenhorn

Joined: Dec 31, 2004
Posts: 26
hi

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

<TITLE>index.jsp</TITLE>
</HEAD>
<BODY>

<% 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>");
session.removeAttribute("from_register1");
}else
{
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>
</form>
</html>

hope this helps.
Ben Souther
Sheriff

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
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    Karthik,
    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: http://aspose.com/file-tools
     
    subject: Session in JSP
     
    Similar Threads
    how to add data in request object and pass it to next page?
    Calling JNDI from JSP
    URL rewriting
    session.getAttribute give null when retreiving session Data in jsps
    Posting session variables in single JSP page