-1

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") %>">
newbieinjavaversion2
  • 489
  • 5
  • 12
  • 23
  • 1
    Why do you want to get and set same variable on the same page? Do not use scriptlets `<% %>` in JSP. You can use `
    ` tag of HTML to submit the value to servlet.
    – Aniket Kulkarni Jan 13 '14 at 13:13

2 Answers2

1

Use the getAttribute method instead of getParameter() like

<%= request.getAttribute("spanId") %>">
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Naveen Ramawat
  • 1,425
  • 1
  • 15
  • 27
0

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.

Swapnil Sawant
  • 620
  • 8
  • 21