• 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

Using setAttribute() in servlets.v2.2

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings,
I recently had my machine upgraded. I am running JDK1.3.1 (I was before, too). But now my version of servlets, which was 2.1 is now either 2.2 or 2.3. How can I tell?
But my problem is this. I had an application that was using the HttpSession object and was using the putValue(), getValue() and getValueNames() methods. When I recompiled under the new version I got error messages telling me that they were now deprecated. So I changed my programs to use the new methods, setAttribute(), getAttribute() and getAttributeNames(). SO they now compile OK but when I run them, they will not run past that line. I get no error messages, no exceptions, no nothing; it just stops.
Now I have tried everything that I can think of to fix this. I have changed my class path. I have tried all sorts of things. I just cannot figure out what I have done wrong. Does anyone have any idea what I can do so that I can save objects in my HttpSession?
If you cannot help me, do you know anyone who can?

Thanks for listening.
 
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have upgraded recently to API 2.2. I am using HttpSession and setAttribute(successfully). Could you post some code?
Regards
Beksy
[This message has been edited by Beksy Kurian (edited October 18, 2001).]
 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THanks,
Here's a test class that I created and I call these methods from a servlet. I think that I may have some of my settings wrong (classpath, etc), but I do not know where to look.
------------------------------------
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionMethodsTest {
public static HttpSession startSession (HttpServletRequest request) {
System.out.println ("Starting Session...");
HttpSession session= null;
String sessionID = "";
String sessionIDformatted = "";
java.util.Date sessionStartDate= new java.util.Date();
long sessionStart = 0;
//try {
session = request.getSession(true);
if(session.isNew()) {
//session.setMaxInactiveInterval(1000);
sessionID = session.getId();
int len = sessionID.length();
int start=0;int end=3;
sessionIDformatted = sessionID.substring(start,end) + " ";
while((end)<len) {
start=end;end=end+4;
if((end)>len)end=len;
sessionIDformatted += sessionID.substring(start,end) + " ";
} // while
sessionStart = session.getCreationTime();
sessionStartDate.setTime(sessionStart);
Integer ival = (Integer) session.getAttribute("sessiontest.counter");
if (ival==null)
ival = new Integer(1);
else
ival = new Integer(ival.intValue() + 1);
session.setAttribute("sessiontest.counter", ival);
System.out.println ("Session ID: " + sessionIDformatted + "was started at: " + sessionStartDate.toString());
session.setAttribute("ShoppingCart", new Vector());
session.setAttribute("SuggestionList", new Vector());
session.setAttribute("SavedList", new Vector());
} else {
Integer ival = (Integer) session.getAttribute("sessiontest.counter");
if (ival==null)
ival = new Integer(1);
else
ival = new Integer(ival.intValue() + 1);
//session.setAttribute("sessiontest.counter", ival);
} // if/else
//} // try
//catch (SQLException e) {
//System.err.println (e.getMessage ());
//System.exit (1); // Driver error
//} // catch
System.out.println ("Session started...");
return session;
} // startSession
public static void printSession (HttpSession session) {
String sessionID = "";
String sessionIDformatted = "";
java.util.Date sessionStartDate= new java.util.Date();
long sessionStart = 0;
System.out.println ("Session ID: " + sessionIDformatted + "was started at: " + sessionStartDate.toString());
} // printSession
public static void showSessionObjects (HttpSession session) {
String sessionID = "";
String sessionIDformatted = "";
java.util.Date sessionStartDate= new java.util.Date();
long sessionStart = 0;
/*String [] sessionObjects = session.getAttributeNames();
for ( int i=0;i<sessionObjects.length;i++) {
System.out.println ("Name [" + i + "]: " + sessionObjects[i] + " Value: " + session.getValue(SessionObjects[i]));
} // for
*/
Enumeration sessionObjects= null;
try{
sessionObjects = session.getAttributeNames();
}catch (Exception e){
System.out.println ("Blow up " + e);
}
System.out.println("here in showSessionObjects");
while (sessionObjects.hasMoreElements ()) {
System.out.println ("Name = " + sessionObjects.nextElement());
}//while
} // showSessionObjects

} // class SessionMethods
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got your servlet to work just fine, this is what I did:
copied your SessionMethodsTest to a file "SessionMethodsTest.java" and put it in the package "com.Jess"
I then created a simple jsp file:

I then packaged it in a war file:

Then when I hit the /testSessions.jsp I get the following output in my application server:
 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jessica but you are so beyond me. I have only learned java in the past couple months.
But you did help because it shows that the code works, it must be some setting (or whatever) that I have wrong. Now if I could just figure out what that is....

Thanks again.
 
Beksy Kurian
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this small servlet to see whether necessary files are in the classpath.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class Classpath extends HttpServlet {

public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(System.getProperty("java.class.path"));
}
/**
* Forwards requests to the doGet(...) method
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Also, what is the servlet engine you are using? Put a system.out.print message to see whether you are getting correct sessionid...(session.getID()). Are you getting any error messages? Also, check this post, http://www.javaranch.com/ubb/Forum7/HTML/004648.html
 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Beksy I ran your servlet and could see nothing wrong.
I am using JWS1.3.1 and Windows2000 with IE5.5
I tried going back to the deprecated methods and it works OK. So I still have no clue why set and getAtribute will not work.
Thanks everyone.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are using Java Web Server? I can't be certain, but I'm pretty sure JWS is 'deprecated' itself.

Use Tomcat instead, because this is the servlet runner that actually understands the latest servlet/jsp specs.

It's one thing to compile your servlets and of course, if you use setAttribute() you will need the most recent version of servlet.jar in your CLASSPATH.

To *run* this servlet, you will need a servlet runner that *also* understands this latest spec.

 
Mary Cushing
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mike,
I also asked that question. This is in an education environment and we are using JWS. I also had problems using JSP with the JWS. Next week, we are going to be learning about Tomcat so I will try my code with that server. Hopefully you are right and my code(with setAttribute) will work there. In the meantime I will use the deprecated methods, I still have a ton of work to do on my project.
Thanks again everyone.
BTW, this is a great site, you are all very helpful. I posted my problem on a few other boards, including sun's and have not gotten one response. I hope you all do not mind, but I will definitely be back and maybe someday I can help someone else.
reply
    Bookmark Topic Watch Topic
  • New Topic