• 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

JSP can't pass Session to a servlet?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Joe Kaz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please make sure that before executing response.encodeURL...... you had called at least once request.getSession() or request.getSession(true);
HTH
 
Joe Kaz
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic