File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes request.setAttribute question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.setAttribute question" Watch "request.setAttribute question" New topic

request.setAttribute question

Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
I have a servlet where I pull a param from the web.xml. I want to take that param, put it in the request, and let a bean access it on the jsp page.
My problem is that even though I'm putting it's a String I'm putting into the request.setAttribute method, my set method in the bean sees it as an Object, not a string. (I have to cast it to a String inside the method).
That results in my set and get methods for that particular variable not working with the jsp:setProperty and jsp:getProperty tags on the jsp page.
Is there a way I can add a paramater to the request object (that will be read as a string) while inside a servlet?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The request's set/get attribute takes and returns an object. You have to cast it to the appropriate type. You will have to read it as an object and perform the cast to a string while in your servlet.

So much trouble in the world -- Bob Marley
Jignesh Malavia
Ranch Hand

Joined: May 18, 2001
Posts: 81
Hello Mark,
1. Create a request wrapper class that extends HttpServletRequestWrapper.
2. Override getParameter() method to return the String value (that you read from web.xml) for the given param name.
3. Pass an instance of this wrapper to the RequestDispatcher.include() or forward() when calling the JSP page.
4. The JSP page will receive your wrapper class as its request and a call to getParameter() will return the value as a String as usual.
This is the standard way to introduce new parameters into the request when using Filters. Works for servlets too.
Btw, as it is you are reading the init param-value from web.xml, why not let the JSP page get it directly from there rather than via a servlet?.
For context-param
<jsp: setProperty name="aName" property="aProp" value='<%=application.getInitParameter("aParam")%>' />
For init-param
<jsp: setProperty name="aName" property="aProp" value='<%=config.getInitParameter("aParam")%>' />
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
Thanks, I'll try that. The reason I wasn't pulling it on the jsp was because the info was used in a bean, not on jsp page. Unfortuanately because my set method took an object, and my get method returned a string, the jsp:set and jsp:get tags didn't work.. though I could still call the methods manually xx.setSomething(object) .
I was just trying to clean it up so it worked using conventional style and tags.
I agree. Here's the link:
subject: request.setAttribute question
It's not a secret anymore!