Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a request attribute set by a servlet in JSP?

I'm trying to retrieve attribute values set by a servlet in a JSP page, but I've only luck with parameters by ${param}. I'm not sure about what can I do different. Maybe its simple, but I couldn't manage it yet.

public void execute(HttpServletRequest request, HttpServletResponse response) {      //there's no "setParameter" method for the "request" object     request.setAttribute("attrib", "attribValue");      RequestDispatcher rd = request.getRequestDispatcher("/Test.jsp");     rd.forward(request,response); } 

In the JSP I have been trying to retrieve the "attribValue", but without success:

<body>     <!-- Is there another tag instead of "param"??? -->     <p>Test attribute value: ${param.attrib} </body> 

If I pass a parameter through all the process (invoking page, servlets and destination page), it works quite good.

like image 234
Alex Avatar asked Jun 05 '12 13:06

Alex


People also ask

How can we retrieve data from servlet and display in JSP page?

1) First create data at the server side and pass it to a JSP. Here a list of student objects in a servlet will be created and pass it to a JSP using setAttribute(). 2) Next, the JSP will retrieve the sent data using getAttribute(). 3) Finally, the JSP will display the data retrieved, in a tabular form.

What is request setAttribute in JSP?

To pass the value from servlet to html/jsp files, setAttribute() method is called by the request object. setAttribute() method takes an input as an object which sends the data from servlet to the requesting website public void setAttribute(String name, Object obj) Sets the specified object in the application scope.

What is request attribute in servlet?

A request attribute is an object added to the request scope on the server side used for the request processing. We can set and get the value of attributes associated with the request by using the methods of ServletRequest object.

How can we pass list values from JSP to servlet?

ArrayList lst = new ArrayList(); lst. add("test1"); lst. add("test2"); session. setAttribute("list", lst);


2 Answers

It's available in the default EL scope already, so just

${attrib} 

should do.

If you like to explicitly specify the scope (EL will namely search the page, request, session and application scopes in sequence for the first non-null attribute value matching the attribute name), then you need to refer it by the scope map instead, which is ${requestScope} for the request scope

${requestScope.attrib} 

This is only useful if you have possibly an attribute with exactly the same name in the page scope which would otherwise get precedence (but such case usually indicate poor design after all).

See also:

  • Our EL wiki page
  • Java EE 6 tutorial - Expression Language
like image 62
BalusC Avatar answered Sep 20 '22 22:09

BalusC


Maybe a comparison between EL syntax and scriptlet syntax will help you understand the concept.

  • param is like request.getParameter()
  • requestScope is like request.getAttribute()

You need to tell request attribute from request parameter.

like image 26
smwikipedia Avatar answered Sep 18 '22 22:09

smwikipedia