I am trying to pass two values (intMethod
and SpotDays
) from SourceServlet
to a JSP named CcySorting.jsp
.
I am using the setRequestAttribute()
method to set the values at servlet end and using getRequestAttribute()
at JSP end to receive the values. But I am receiving null values in JSP. My code is below. Please have a look at it and suggest the possible reason. I have tried a lot, but in vain.
I am also providing my JSP and servlet folder structure.
My folder structure:
- JSP path:
application.war\CcySorting.jsp
- Servlet path:
application.war\WEB-INF\classes\SampleServlet.class
My entries in Web.xml
:
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>SampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/SampleServlet</url-pattern>
</servlet-mapping>
My JSP Files:
CcySorting.jsp
function searchData(brn,ccy) { var frmObj =getUserFormObj('window','div0','form0'); window.open("/SampleServlet?BrnName="+brn+"&Currency="+ccy); var intMethod= <%= request.getAttribute("intMethod1") %>; var spotDay = <%= request.getAttribute("SpotDays1") %>; alert("data from servlet"+intMethod+"and spot"+spotDay1); }
SampleServlet.java
public class SampleServlet extends HttpServlet{ public void service(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{ // Some code to fetch the data from database and store in two variable intm and spot int int=2 int spot=3 request.setAttribute("intMethod1",int); request.setAttribute("SpotDays1", spot); RequestDispatcher rd=request.getRequestDispatcher("/CcySorting.jsp"); rd.forward( request, response ) ; } }