• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why am I always getting a new session *  RSS feed

 
Jay Menorca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
* - when my webapp is deployed in Weblogic server 10

Hello good folks

Pls. refer to my code below. Why am I always getting a new session with this code? anything wrong with it? It runs ok in tomcat - no new sessions everytime. In WLS, new session every form submit...sigh...what to do ...pls help

-------------------------
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String title = request.getParameter("subject");
String strTimeStr = request.getParameter("starttime");
int startTime = Integer.parseInt(strTimeStr);
String endTimeStr = request.getParameter("endtime");
int endTime = Integer.parseInt(endTimeStr);
String[] days = request.getParameterValues("day");

HttpSession sess = request.getSession(true);
SchoolSchedule sched = (SchoolSchedule) sess.getAttribute("sched");

if (days != null){
for (int i=0;i<days.length;i++){
String dayString = days[i];
int day;
if(dayString.equalsIgnoreCase("SUN")) day = 0;
else if(dayString.equalsIgnoreCase("MON")) day = 1;
else if(dayString.equalsIgnoreCase("TUE")) day = 2;
else if(dayString.equalsIgnoreCase("WED")) day = 3;
else if(dayString.equalsIgnoreCase("THU")) day = 4;
else if(dayString.equalsIgnoreCase("FRI")) day = 5;
else day = 6;

SchoolClass clazz = new SchoolClass(title, startTime,endTime, day);
sched.addClass(clazz);
}
}

request.getSession().setAttribute("sched", sched);
getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request, response);
}
-------------------------

I added a few more lines in my servlet and I've noticed that for the WLS deployment, I ALWAYS get a new session when I click the form submit button. That's why the stuff that should've been save in the session from the previous form submit is missing.

I only have two elements in my webapp, a jsp page and a servlet. The jsp page has 2 sections, one for getting user inputs and the other part for displaying it. The servlet gets the data inputted in the jsp, massages this data and adds it to an attribute in the session object. The proper thing would be for the data to persist in the session attributes - but it's not behaving like this.

appreciate any suggestions. ^_^
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has your container been configured not to use cookies for session handling?
If so, then you would need to urlEncode all of your links, hrefs, src's, and form actions in order for sessions to work.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66151
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use real words when posting to the forums. Abbreviations such as "pls" in place of "please" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
 
Jay Menorca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think my WLS 10 is currently using cookies for session tracking. I tried to do that URL rewriting / URL encoding both in my servlet and my jsp, but i'm still getting a new session at every form submit.

here are some code snippets:

//-----------------servlet------------------------

HttpSession sess = request.getSession();
SchoolSchedule sched = (SchoolSchedule) sess.getAttribute("sched");

if (sched == null) {
sched = new SchoolSchedule();
}

// here I add somthing to the SchoolSched - it's an Arraylist of
// some class so i just add objects to it

sched.addClass(clazz);

request.getSession().setAttribute("sched", sched);

//below to encode URL - Am I doing this correctly?
response.encodeURL("/Schedule.jsp");
getServletContext().getRequestDispatcher("/Schedule.jsp").forward(request,response);


//-----------------DD------------------------
<servlet>
<servlet-name>ScheduleServlet</servlet-name>
<servlet-class>
com.jay.schoolSched.controller.ScheduleServlet
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ScheduleServlet</servlet-name>
<url-pattern>/Scheduler</url-pattern>
</servlet-mapping>

//-----------------Schedule.jsp------------------------
below is for the URL rewriting..uhm i'm also not sure if this is the way
to do this. Is it? When I click this one, it should go back to the servlet - add what ever data i indicated to the session (i assume that this should be the current session so it still has the value from the previous setAttribute()) and dispatches it again to this jsp for display.

<form action="<c:url value='/Scheduler' />" method="post">


//-------------------------------------------------------

Really appreciate your help. ^_^ When I get this ironed out I'll have pizza sent from here in singapore to whereever you are ^_^
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you try testing with a simpler JSP?



Keep clicking the link.
If you see the same id every time, then you know session based cookies are working. If not, they they might be disabled.
Try wrapping the URL with urlEncode and try again.
 
Jay Menorca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried it, for tomcat, i always get the same session ID. For the WLS deployment, i always get a new one.

uhm... how do you exactly wrap the URL using urlencode? I take it I should apply the url encode to thelink in the jsp right?

And for the case of my app, I should apply the url encode in the form action attribute?
Would this (code below) be the way to do it if my jsp passes control to a servlet with urlpatter "/Scheduler" ?

<form action="<c:url value='/Scheduler' />" method="post">

Appreciate your suggestions...looking forward to more tips
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks good.
The URL rewriting won't get done unless the container can't use the default session handling mechanism (usually cookies).

So, if I add an encoded url like this:


The url will just be: /asdf

Once I disable cookies in Firefox and refresh the page, it looks like:


Session ID: 4650DD23BC74D7FB0690359F143121CC
click me
SomeURL: /asdf;jsessionid=4650DD23BC74D7FB0690359F143121CC




Just a thought, it might be less work to figure out why Websphere is configured not to use cookies for session handling and then (if there is no good reason) get your admin to configure it to do so.

It's a lot of work to find every link in an app and wrap it with c:url.
 
Leandro Dantas
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to change the HttpSession sess = request.getSession(true);

putit with no arguments: HttpSession sess = request.getSession();
 
balakrishnan periysawamy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay,
One of the reason might be your JSP is not participating in a Session.

Just copy this line to your JSP

<%@ page session="true" %>

Regards,
 
Nishanth Sivaraman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when url is created dynamically, wen can do like this...

here varname is variable, and the link will open in a new page.

<%String url="abc.jsp?ticket_number=" + varname ; %>
<a href=<c:url value="<%=url%>"/> target="_blank">
 
Schandha Ravi
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Probably you may try this. I tried this before and it worked for me.

I'm not sure if it is what you are looking for. So don't blame me



This way you can ensure that either you would use the existing session if present or go for a new session.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ben Souther:
Just a thought, it might be less work to figure out why Websphere is configured not to use cookies for session handling and then (if there is no good reason) get your admin to configure it to do so.

It's a lot of work to find every link in an app and wrap it with c:url.


My philosophy is always to pass all URLs through the c:url pattern any way so the app doesn't break when a client doesn't use cookies.

Easy(ier) to say when you are starting an application rather than working on an existing app of course...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!