Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Scope of Request across jsp pages in a web app  RSS feed

 
aj rattan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to use request.setAttribute() to pass values from one jsp page to another.It does not work.I keep getting a null pointer exception.
When i use a session and the store the attribute it works fine. I get that the attribute becomes associated with the entire session and thus is available to any part of the current session.
Why am i unable to use request.setAttribute() to do the same.
Am attaching the three forms i am using

Form1.jsp
<html>
<body>
Choose A Shirt<br>
<form action="Form2.jsp">
Nike<input type="checkbox" name="round" value="nike" /><br>
Reebok<input type="checkbox" name="round" value="reebok" /><br>
Adidas<input type="checkbox" name="round" value="adidas" /><br>

<input type="submit" value="submit" name="sbmtButton" />
</form>
</body>
</html>

Form2.jsp
<html>
<%@ page import="java.util.*,javax.servlet.http.*" %>
<body>
<%
String[] al1 = request.getParameterValues("round");
int i = 0;int count = 0;
while(i<al1.length){
count++;
i++;
}
List a = new ArrayList();
for(i=0;i<al1.length;i++){
a.add(al1);
}
Iterator it = a.iterator();
String s;
while(it.hasNext()){
s=(String)it.next();
%><br><%=s%>
<%}
[i] request.setAttribute("shirts", a);
//setting Attribute

out.print("Values :"+a);
//HttpSession hs = request.getSession(false);
//hs.setAttribute("shirts", a);
%>
Choose A Pant<br>
<form action="Form3.jsp">
AllenSolly<input type="checkbox" name="pant" value="allensolly" /><br>
Pantaloons<input type="checkbox" name="pant" value="pantaloons" /><br>
Levis<input type="checkbox" name="pant" value="levis" /><br>

<input type="submit" value="submit" name="sbmtButton" />
</form>
</body>
</html>

Form3.jsp

<html>
<body>
<%@ page import="java.util.*" %>

<%
int shirts_cost=0;
int pants_cost=0;
String s;
List a = (List)request.getAttribute("shirts");//trying to get attribute. gives a null value
//List a = (List)session.getAttribute("shirts");
String[] pants =request.getParameterValues("pant");

if (a != null){
Iterator it = a.iterator();
while(it.hasNext()){
s=(String)it.next();
if(s.equals("nike")){
out.println("One Nike Cost "+ 25);
shirts_cost+=25;
}
else if(s.equals("reebok")){
out.println("One Reebok Cost "+ 35);
shirts_cost+=35;
}
else if(s.equals("adidas")){
out.println("One Nike Cost "+ 45);
shirts_cost+=45;
}
}
out.println("Shirts Cost is" + shirts_cost);
} else
out.println("Not working");
%>
</body>
</html>
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all: do not use scriptlets. This is not 1998 anymore. Immediately stop using scriptlets and switch to taglibs and EL, starting with JSTL. For the part which isn't solveable with taglibs or EL, you should be using a Java class, for example a servlet or javabean.

Regarding to your actual problem: a request lives from the time that the client sent it to the server until the time that the response is sent from server to client. At the moment that the response is displayed in the webbrowser, the request is already garbaged for long time, including all of its attributes. When you submit the form or invoke a new request, a brand new request object will be created. The previous will not be reused.

There are several ways to transfer data from request to request:
1) Pass it as hidden input value.
2) Store it in session scope.
 
aj rattan
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking along the same lines but could not get a definite hit on it.
As for using the scriptlet Code I have just started JSP some time back and am finding my way.
Thanks for the advice .Will try to implement what you wrote.

Thanks again
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!