Reputation: 43
How to send/pass values from one servlet(consider it as one project) to another servlet(consider as another project). It's showing number format exception. Is it correct to pass values in sendredirect method or is there any other way
Example:
File: uzkpk2.java
String a1=request.getParameter("a[0]");
aa1=Integer.parseInt(a1);
String a2=request.getParameter("a[1]");
aa2=Integer.parseInt(a2);
String a3=request.getParameter("a[2]");
aa3=Integer.parseInt(a3);
String a4=request.getParameter("a[3]");
aa4=Integer.parseInt(a4);
response.sendRedirect("http://localhost:8080/CSP/czkpk1?y="+y+"&a1="+aa1+"&a2="+aa2+"&a3="+aa3+"&a4="+aa4);
}
catch(Exception e)
{
out.println(e);
}
}
}
File: czkpk1.java
aaa1=Integer.parseInt(request.getParameter("aa1"));
aaa2=Integer.parseInt(request.getParameter("aa2"));
aaa3=Integer.parseInt(request.getParameter("aa3"));
aaa4=Integer.parseInt(request.getParameter("aa4"));
Upvotes: 0
Views: 1379
Reputation: 2753
You are using the wrong request parameter to get the value.
aaa1=Integer.parseInt(request.getParameter("aa1"));
aaa2=Integer.parseInt(request.getParameter("aa2"));
aaa3=Integer.parseInt(request.getParameter("aa3"));
aaa4=Integer.parseInt(request.getParameter("aa4"));
Instead of this use
aaa1=Integer.parseInt(request.getParameter("a1"));
aaa2=Integer.parseInt(request.getParameter("a2"));
aaa3=Integer.parseInt(request.getParameter("a3"));
aaa4=Integer.parseInt(request.getParameter("a4"));
since in czkpk1.java you are using the variable names instead of parameters passed in the url present in response.sendRedirect();
And one advice chech for only numeric values before parsing it into string.
The best way to do this is use concept of
Servlet Chaining.
Upvotes: 1
Reputation: 13656
-> Write your value in request context as an attribute using request.setAttribute()
-> After forward the request to second servlet using RequestDispatcher.forward()
-> In second servlet read the value using request.getAttribute()
Upvotes: 0