1

I have the following snippet in my jsp file:

<%
String name = request.getParameter("name");     
out.println(pageContext.findAttribute("name"));

%>

<br><br>

Name in request scope: <%=name %><br>

The output I get for passed parameter name = Swatanya is the following:

null 

Name in request scope: swatanya

why does findAttribute return null when there is value in name parameter of request scope ?

SwatiBhalla
  • 39
  • 1
  • 6

2 Answers2

2

There is a difference between request parameter and attribute. Don't confuse between both.

If you are talking about query string then its passed as request parameter not attribute.


What JspContext#findAttribute() states:

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

What ServletRequest#getParameter() states:

Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.


You can try in this way if needed:

request.setAttribute("name", request.getParameter("name"));

Note:

I suggest you to use JavaServer Pages Standard Tag Library or Expression Language instead of Scriplet that is more easy to use and less error prone.

Braj
  • 46,415
  • 5
  • 60
  • 76
0

In first case you are searching in specific scope in request scope. Where in second case you are searching in page,request ,session and application scope in order.it means if in case of get parameter if name is not present in request scope it will return null and in find attribute it will search all the scope

  • Unfortunately your answer doesn't address the question. In question's scenario `pageContext.findAttribute("name")` is returning `null`. Whereas `request.getParameter("name")` is returning `swatanya`. – Md. Abu Nafee Ibna Zahid May 27 '18 at 15:14