Pass Value by Using Request.setAttribute

11.1k Views Asked by At

I am trying to get attribute of spanId and set Attribute by using request. Then I want to pass the output. Although the first input is has value, it still return me null.

Below are my codes. Help will be appreciate! :)

<input id="spanId" name="spanId">

<%  
    String spanId = request.getParameter("spanId");
     request.setAttribute("spanId",spanId);
%>

<%= request.getParameter("spanId") %>">
2

There are 2 best solutions below

1
On

Use the getAttribute method instead of getParameter() like

<%= request.getAttribute("spanId") %>">
0
On

Just think if you are setting an attribute to request object how you can get it? by using getAttribute.... String spanId = request.getAttribute("spanId").

Also request attribute has request scope (scope lasts only till 1 request).

More on this over here http://docs.oracle.com/javaee/1.4/api/javax/servlet/jsp/JspContext.html

Avoid using scripting elements in your JSP.. remember you are tightly coupling your presentation and business logic.