• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Session in JSP

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Ben,
    I will remember that.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic