aspose file tools*
The moose likes Servlets and the fly likes session problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "session problem" Watch "session problem" New topic
Author

session problem

Erick Martin
Greenhorn

Joined: May 17, 2002
Posts: 8
Hi everybody. I am developing a web application following the MVC pattern. I have the feeling my so called �session problem� is quite trivial, but this is the price one has to pay when moving from the back-end scene to the jsp-servlet world. This is the scenario:
View:
There are a few pages having static content. For these pages the structure is as follow:
pageName.jsp � contains custom tags template
pageName.html � the actual content of the page
To increase performance, I use direct html links to access these pages, skipping over the main controller. So far, so good.
Controller:
MainServlet is the main controller. At this point it only does simple redirecting, you can take a peek at code below:
----------------------------
public class MainServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doProcess(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doProcess(request, response);
}

private void doProcess(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String nextScreen = null;

HttpSession session = request.getSession(true);
session.setAttribute("whateverAttribute", "attributeValue");

nextScreen = request.getParameter("currentScreen");
if (nextScreen == "x") {
gotoPage("/page1.jsp", request, response);
} else if (nextScreen == "admin") {
gotoPage("/page2.jsp", request, response);
} else {
gotoPage("/default.jsp", request, response);
}
}

private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(response.encodeURL(address));
dispatcher.forward(request, response);
}
}
------------------------
The problem:
I need to keep the session information generated in MainServlet (whateverAttribute=attributeValue) available to all my static jsp pages and eventually to other servlets, but every time I go to a new page I get a different session ID.
There are some conditional pieces of content to be embedded into the custom tags template, depending on session info.
Can anybody bring some enlightment to this troubled collegue? Thanks in advance,
[ May 17, 2002: Message edited by: Erick Martinez ]

sa<br />Sun Certified Programmer for Java 2 Platform<p>"... black holes are those places in the Universe where God attempted to divide by zero"
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
sillyAgent
First off:
Welcome to the Java Ranch, we hope you’ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Second:
The problem you're having is probably due to the links in the HTML pages. Since thay are static they do not get the session id that you do when you use the encodeURL in the controller. to carry it over you'll need to make them JSPs in order to get the session info into the link.
[ May 17, 2002: Message edited by: Dave Vick ]

Dave
Erick Martin
Greenhorn

Joined: May 17, 2002
Posts: 8
Hi Dave, thanks for both your suggestion and advice. I got it working right. By the way, for anyone else reading this posting here it is how the solution should look like:
<A HREF="<%= response.encodeURL("yourPage.jsp") %>">Linked Text </A>
I also had to add the encodeURL to the ACTION tag in the form calling my servlet:
<FORM ENCTYPE="multipart/form-data" NAME="FormName" METHOD="POST" ACTION="<%= response.encodeURL("yourServlet") %>">
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"Erik M"
You've almost got the naming policy right: "For your publicly displayed name, use a first name, a space, and a last name."
-- Please provide a full word for a last name, not just an initial.
Thanks!
Debashish Chakrabarty
Ranch Hand

Joined: May 14, 2002
Posts: 226

The problem you're having is probably due to the links in the HTML pages. Since thay are static they do not get the session id that you do when you use the encodeURL in the controller. to carry it over you'll need to make them JSPs in order to get the session info into the link.

Correct me if I am wrong but I was under the impression that session is not interrupted even if some midlying pages are HTML.
This is the reason, I recall, experts recommend using encodeURL while redirecting, to help the sessiontracker decide whether the URL should be rewritten (say when the browser has disabled the cookies or does not support them at all)to maintain state.
[ May 20, 2002: Message edited by: Debashish Chakrabarty ]

Debashish
SCJP2, SCWCD 1.4
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Debashish
You're correct. In fact if you use encodeURL and cookies are enabled then encodeURL doesn't do anything to the URL it is given.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session problem