aspose file tools*
The moose likes JSP and the fly likes request.getAttribute returning null. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "request.getAttribute returning null." Watch "request.getAttribute returning null." New topic
Author

request.getAttribute returning null.

Mahesh Malviya
Ranch Hand

Joined: Aug 20, 2004
Posts: 39
Hi,
I have two jsps. One is callingJsp.jsp and other is mainJsp.jsp.
I am setting a variable "name" in request in callingJsp.jsp.
Code for the callingJsp.jsp is..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
</HEAD>
<BODY>

<FORM name = "f1" action="mainJsp.jsp" >
<%
request.setAttribute("name","Mahesh");
%>
<input type="submit">
</FORM>
</BODY>
</HTML>



When I submit callingJsp.jsp another page mainJsp.jsp is called.
the code in the mainJsp.jsp is...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
</HEAD>
<BODY>
<form>
<%

String nm = (String)request.getAttribute("name");
if(request.getAttribute("name") != null){
System.out.println(request.getAttribute("name").toString());
}
%>
<%=nm%>

</form>
</BODY>
</HTML>


But the value of nm is always null. I do not understand what is the reason.
Anyone please who can help me.

Thanks and Regards.
Mahesh Malviya
Stefan Willi
Ranch Hand

Joined: Mar 11, 2005
Posts: 47
Hy!

At the time, you are submiting the form, you are creating a new request. So, your mainJSP.jsp cannot see the old request.
If you have some input elements in the form, then they will be populated into the new request and you will have access to them in your mainJSP via request.getParameter("name_of_the_Parameter").

stefan
[ March 15, 2005: Message edited by: Stefan W. ]
Mahesh Malviya
Ranch Hand

Joined: Aug 20, 2004
Posts: 39
Thanks stefan.
I understood the reason why it is not available to the next page. I have one more problem. Please have a look on it. I am setting a variable into request and forwarding it to some other page. Yes the variable will be available to me in request in next page. but I want to access this variable in a page which is inside a frame in the called jsp page. How can I do this? In this case also the page loading into frame will have a different request and will not have access to the parent page's request object. How can I access the request object of the parent page?
Waiting for your reply.

Thanks and Regards

Mahesh Malviya
Stefan Willi
Ranch Hand

Joined: Mar 11, 2005
Posts: 47
I think, if you work only with jsp, then you should use the <jsp:include> Tag.

It will include your subPage to the original and the request is provided.

Let's have a look at this.

Inside the <jsp:include>, you can set variables, which will only visible on your sub-page ok, i give up, this smily is winning. the text should be jsp : param ;-)

Stefan

[ March 15, 2005: Message edited by: Stefan W. ]
[ March 15, 2005: Message edited by: Stefan W. ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

"Stefan W.",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender

P.S. You can defeat the smilies by clicking the 'disable smiless' checkbox near the end of the reply page.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request.getAttribute returning null.