Reputation: 47
I want to pass value of a rowNo from one jsp file to another jsp file(to display image on that row) by using request.setAttribute() and request.getAttribute() whenever user clicks on a link.
But when I try to use request.getAttribute() on second jsp page it gives following exception: org.apache.jasper.JasperException: java.lang.NumberFormatException: null
On Index.jsp page I have :
<%!
int temp = 1;
%>
<a href="single.jsp" onclick="<%= request.setAttribute("val", temp) %>" >
<i class="glyphicon glyphicon-menu-right icon"></i> </a>
And on single.jsp page :
<%
dbConnect con = new dbConnect();
ResultSet r = con.getConnection().executeQuery("select * from tblpic ");
String tp = (String) (request.getAttribute("val"));
int i = Integer.parseInt(tp);
r.absolute(i);
%>
<div class="thumb-image"> <img src="<%= r.getString(2) %>" data-imagezoom="true" class="img-responsive"> </div>
r.absolute(i) is used to move to that specific row in table.
I have added this file on both jsp pages(I don't know it is necessary or not) <%@page import = "javax.servlet.http.HttpServletRequest"%>
I am working on net beans and struts framework.
Upvotes: 1
Views: 30891
Reputation: 915
I would prefer session.setAttribute()
instead of request.setAttribute()
to pass value from one jsp to another.
So your code will be:
<a href="single.jsp" onclick="<%= session.setAttribute("val", temp) %>" >
And
String tp = (String) (session.getAttribute("val"));
Upvotes: 1
Reputation: 680
You are setting values in request scope. Request scope attributes are only accessible in the same request. Whenever the end user clicks on the link(provided by ), a new request is generated hence you loose the attributes that you set in the previous request.
To Solve the problem you could do
1) URL forwarding as explained by Piyush Aghera
2) You can store the value in the session as explained by PVR
3) If you prefer to store the values in request attribute only, then I would suggest you to use RequestDispatcher to forward your request. This would work as your request on the next page would remain same.
Upvotes: 1
Reputation: 965
You have mixed server side code and client side code together. "request" is a java object and part of servelet request handling. it works at server side only while rendering jsp in servlet container.
"onclick" is a java script click trigger run at browser side.
Once page get displayed "request" is not logger valid. and on clicking of submitting new request..new object of request will created.
For you, can easily pass parameter argument to jsp as follow.
on first jsp:
<a href="single.jsp?val=temp" >
on second jsp:
<%=request.getParameter("val")%>
Upvotes: 3