aspose file tools*
The moose likes JSP and the fly likes JSP can't pass Session to a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP can Watch "JSP can New topic
Author

JSP can't pass Session to a servlet?

Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
Hello everyone,
I am trying to call a servlet from a Form generated in a JSP. The code, working OK with Tomcat 3.3, looks like this:
<form method="POST" action="<%=response.encodeURL("http://localhost:8080/servlet/AddItemServlet")%> ">
The problem is that the servlet receives a null Session object from:
HttpSession sess = request.getSession(false);
Can anyone suggest a better way to make the transfer and have the Session picked up correctly by the servlet?
Thanks very much
Joe Kaz
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Is this JSP and Servlet in the same web-application ?
If yes, then you don't have to do
<form method="POST" action="<%=response.encodeURL("http://localhost:8080/servlet/AddItemServlet")%> ">
a simpler action attribute will help you....
<form method="POST" action="AddItemServlet">
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
Madhav,
I am running under the default application, with the JSP in
...webapps/examples/jsp
and the servlet in
...webapps/examples/web-inf/servlets also I stuck it in
...webapps/examples/web-inf/classes
I get a "not found" if I don't fully qualify the name - shortening it didn't work.
Thanks
Joe K
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
please make sure that before executing response.encodeURL...... you had called at least once request.getSession() or request.getSession(true);
HTH
Joe Kaz
Greenhorn

Joined: Aug 13, 2001
Posts: 14
Yes beforehand I called
<% HttpSession sess = request.getSession(false); %>
using (false) because an object was written and stored to the session earlier, so I don't want any new (true) session.
Joe K
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

<form method="POST" action="AddItemServlet">
...webapps/examples/web-inf/classes
I get a "not found" if I don't fully qualify the name - shortening it didn't work

My mistake....
Try this:
<form method="POST" action="/AddItemServlet">
Also default application is NOT "examples" (atleast in Tomcat). It is "Root"
- satya
hob hartman
Greenhorn

Joined: Mar 04, 2002
Posts: 11
I was looking in Google and found this article dealing with encodeURL.
Question How can I enable session tracking for JSP pages if the browser has disabled cookies?
Topics Java:API:JSP:Sessions, Java:API:Servlets:Cookies, Sessions, and State Management
Author Govind Seshadri
Created Nov 15, 1999 Modified Aug 1, 2001

Answer
We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting.
URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response.
Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.
Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.
Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other. Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object.
Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages.
Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.

hello1.jsp

<%@ page session="true" %>
<%
Integer num = new Integer(100);
session.putValue("num",num);
String url =response.encodeURL("hello2.jsp");
%>
<a href='<%=url%>'>hello2.jsp</a>
hello2.jsp

<%@ page session="true" %>
<%
Integer i= (Integer )session.getValue("num");
out.println("Num value in session is "+i.intValue());
%>

Hope this helps.
hob
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSP can't pass Session to a servlet?