Using setAttribute(), getAttribute() in servlets

I need to take a Java String or other Object and pass it from one servlet to another. I have been trying to use the setAttribute and getAttribute methods. I also tried servletContext().set/getAttribute(). Everything I have tried passes a null value. This seems to me to be a fairly simple, common task. Please help. Thanks!!

[338 byte] By [sigrney] at [2007-9-26 1:34:50]
# 1
Can u show ur code ?
shubhrajit_c at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

pls make sure you have actually set your attributes in your servlet context instead of the request/session object copied from your context.

usually before we do some handles we usually get the session object from context

HttpSession session = request.getSession();

and you use session variable do something

eg

String a = (String) session.getAttribute("A");

a="XXX";

the mistakes now:

session.setAttribute("A",a);

now you set the attribute to the copied one!

the correct one should be

request.getSession().setAttribute("A",a)

then you can set back the attribute back into your context.

sujianqiang at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

By all rights, this should work:

===========

Servlet A

===========

getServletConfig()

.getServletContext()

.setAttribute("myObject", myObject);

===========

Servlet B

===========

Object myObject =

getServletConfig()

.getServletContext()

.getAttribute("myObject");

If it doesn't, if you are always getting null values in Servlet B, then it is not your code, but your servlet container/runner environment.

As far as I know, both servlets must have the same CONTEXT - that is, they must be running in the same web application.That is, you can't pass a servlet from mydomain.com to a servlet on www.mydomain.com if your servlet runner defines these as separate applications or contexts.

Secondly, you might need to contact the developer of your servlet runner/dig through the documentation to discover if setting attributes in the servlet context is supported or not (it might not be!).

Otherwise, do the usual - check that the value is not null before setting it, make sure you're spelling the name of the attribute correctly, etc. etc.

Lastly, ALWAYS get the ServletConfig before you get the ServletContext - it's better practice and I've found that sometimes the ServletContext is null if you don't get the ServletConfig first.

Good Luck,

Justoon

justoon at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Pardon me for being a newbie to servlets.....What is servlet config? I tried your method of passing the object, but I still get null, so I guess I need to look at my servlet engine's docs (Tomcat). Also, what can you tell me about the use of Request Dispatcher? Thanks!
sigrney at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Is it good practise to store values in session object?For that we use session.putValue("asd","asd");I think maximum limit of session object is 2k. So better to use getServletConfig().getServletContext().setAttribute("asd","asd");
gerlin at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
hi,I think u are making mistake in choosing the class. The methods getAttribute() and setAttribute() are used in context with the interface HttpServletRequest.This will solve ur query.sameer
sameerleekha at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

If you don't want to keep the data for the whole session, you store the data in the request. The problem is that you can only forward the request object to a servlet/jsp in the same application as your current servlet. Then you use the requestDispatcher to pass the request along.

public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) {

/* do some stuff */

// Store your string

request.setAttribute("resultString",resultString);

// Pass the request along to the next object

getServletContext().getRequestDispatcher(jspOrServletPath).forward(request, response);

}

In your next jsp/servlet you can then use

String inputValue = request.getParameter("resultString");

to get the value you passed along.

Note that getValue and setValue are functionally identical, but have been deprecated.

sage_sam at 2007-6-29 2:18:28 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...