Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session in JSP

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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 ]
     
    Joseph Sweet
    Ranch Hand
    Posts: 327
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 327
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Ben,
    I will remember that.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic