• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get attribute from servlet

 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a JSP, use POST method to a servlet. in the JSP i write a scriptlet <%request.setAttribuet("name","Duck");%>. but when i try to get the attribute out in the servlet i get a null , any body tell me why ?thank you !!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't really a question related to SCWCD. You should rather be posting this at the JSP forum.

At any way, this is what is happening:
* You fire a request on a JSP.
* You're setting an attribute on that request using some scriptlet.
* JSP generates and sends HTML response to the client. At that point JSP/Servlet is finished. There is no request and response anymore.
* In the client side you fire a new request by submitting the form.

I think that it may be obvious that the new request doesn't contain the attribute anymore.

To solve your functional requirement, just use a HTML hidden input element.

That said, use of scriptlets is highly discouraged. This isn't 1998 anymore. Java code belongs in Java classes. JSP is a view technology, use it for presentation only. You should use EL, JSTL and other taglibs to control the presentation and the flow in JSP.

Oh, your topic title is also fairly confusing. You're asking how to get attribute from servlet, but your actual problem is how to get attribute in servlet.
 
long meng
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Bauke Scholtz!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic