Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request.setAttribute question

 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jignesh Malavia
Author
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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")%>' />
-j
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic