Solved

JSP session + cookies + tomcat upgrade

Posted on 2003-11-24
6
333 Views
Last Modified: 2010-04-01
Hi,
My intranet application was working fine on tomcat 3.2 but when i try to upgrade to 4.1.24 it goes wrong with the code. Here is my example.
I have a loginaction.java which has following code
...
Cookie cookie =  createCookie(request, "userId", userId);
response.setHeader("realm","../user/login.jsp");
response.sendRedirect("../user/login.jsp");
...
In my jsp i have something like:
...
<%
    String userId=null;
    Cookie[] cook = request.getCookies();
    if( cook!= null){
     for (int k=0; k<cook.length ; k++){          
          if(cook[k].getName().equals("userId"))
               userId= cook[k].getValue();              
               }
            }
     if(userId==null){
     response.setHeader("Back","..");
     response.sendRedirect("..");
     return;          
     }
...

My problem is that the redirect always occurs... in other words the cookies are not set... but it used to work in 3.2?
Any Idea?

Thanks!
0
Comment
Question by:drieske
  • 3
6 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9811045
is the cookie picked up in jsp pages following that one?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9811068
>  createCookie(request, "userId", userId);

where is this method from?

I thought you had to do:

response.addCookie( new Cookie( "userId", userId ) ) ;
0
 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 9811078
So your loginAction.java code you posted becomes:

Cookie cookie = new Cookie( "userId", userId ) ;
response.addCookie( cookie ) ;
response.setHeader("realm","../user/login.jsp");
response.sendRedirect("../user/login.jsp");
0
 
LVL 15

Assisted Solution

by:jimmack
jimmack earned 250 total points
ID: 9818843
>> My problem is that the redirect always occurs

Which redirect?  The one in the JSP (I'm guessing) or the one in loginaction.java (presumably a servlet)?

Is the loginaction servlet definitely being called (in turn redirecting to the login.jsp)?

As Tim asks, what is the code for the createCookie() method?

Are you calling the setMaxAge() method anywhere?

Does this all happen in a single session (the default lifetime of a cookie), or is the problem occurring when a user later goes directly to the login.jsp page?
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question