Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

request.getAttribute() returns null in jsp

This is a discussion on request.getAttribute() returns null in jsp within the JSP forums, part of the Web Development category; Hi Friends, I am facing a problem while trying to retrieve object from request scope. I have added a object in the request scope through a servlet and than using ...

Go Back   Geeks Talk > Web Development > JSP
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

JSP JSP Related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-18-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 987
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
request.getAttribute() returns null in jsp

Hi Friends,


I am facing a problem while trying to retrieve object from request scope.

I have added a object in the request scope through a servlet and than using RequestDispacther i have forwarded the request to the jsp and while trying to retrive the object in the jsp i am getting null.

Please find the code below,

Servlet Code :


package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.example.web.Person;

public class UseBeanTestServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

String personName = request.getParameter("personName");
Person personObj = new Person();
personObj.setName(personName);

request.setAttribute("PesronObj",personObj);

RequestDispatcher view = getServletContext().getRequestDispatcher("/UseBean.jsp");
view.forward(request,response);
}
}


Jsp Page :

<%@ page import="com.example.web.*"%>
<HTML>
<TITLE>
USE BEAN EXAMPLE
</TITLE>

<BODY>
Name : <%= ((String)request.getAttribute("personName"))%>
</BODY>

</HTML>



I have also tried adding object to servletContext, however after forwarding the request , for some reason i am not able to retrive the obj in the jsp.

Plz help me to do this..........


Thanks in advance............
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-17-2009
Junior Member
 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
ShruthiL is on a distinguished road
Re: request.getAttribute() returns null in jsp

Hi, in the request, you are setting the attribute "personobj" and in jsp you are trying to get the attribute "personName" . kindly check that.

Quote:
Originally Posted by rijus View Post
Hi Friends,


I am facing a problem while trying to retrieve object from request scope.

I have added a object in the request scope through a servlet and than using RequestDispacther i have forwarded the request to the jsp and while trying to retrive the object in the jsp i am getting null.

Please find the code below,

Servlet Code :


package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.example.web.Person;

public class UseBeanTestServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

String personName = request.getParameter("personName");
Person personObj = new Person();
personObj.setName(personName);

request.setAttribute("PesronObj",personObj);

RequestDispatcher view = getServletContext().getRequestDispatcher("/UseBean.jsp");
view.forward(request,response);
}
}


Jsp Page :

<%@ page import="com.example.web.*"%>
<HTML>
<TITLE>
USE BEAN EXAMPLE
</TITLE>

<BODY>
Name : <%= ((String)request.getAttribute("personName"))%>
</BODY>

</HTML>



I have also tried adding object to servletContext, however after forwarding the request , for some reason i am not able to retrive the obj in the jsp.

Plz help me to do this..........


Thanks in advance............
Reply With Quote
The Following User Says Thank You to ShruthiL For This Useful Post:
  #3 (permalink)  
Old 07-17-2009
Expert Member
 
Join Date: May 2009
Location: Bangalore
Posts: 987
Thanks: 155
Thanked 420 Times in 201 Posts
rijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nicerijus is just really nice
Re: request.getAttribute() returns null in jsp

Hi,

Yes, U r right.

The issue is already solved...

Anyways Thanks for u r effort...

The corrected jsp code...

<%@ page import="com.example.web.*"%>
<HTML>
<TITLE>
USE BEAN EXAMPLE
</TITLE>
<BODY>
Name : <%= ((Person)request.getAttribute("PesronObj"))%>
</BODY>

</HTML>


Thanks,
Riju.
Reply With Quote
  #4 (permalink)  
Old 07-17-2009
Junior Member
 
Join Date: Jul 2009
Posts: 4
Thanks: 0
Thanked 1 Time in 1 Post
ShruthiL is on a distinguished road
Re: request.getAttribute() returns null in jsp

Hi, Try the below. It will work.

your first jsp. (UseBeanTest.jsp)

Quote:
<HTML>
<body>
<form method = "get" action = "/UseBeanTestServlet">
<input type ="text" name = "personName" value =" "><br>
<input type ="submit" name = "AddButton"><br>
</form>
</body>
</HTML>
UseBeanTestServlet.java

Quote:
package learn;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class UseBeanTestServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

String personName = request.getParameter("personName");
Person personObj = new Person();
personObj.setName(personName);

request.setAttribute("PersonObj",personObj);

RequestDispatcher view = getServletContext().getRequestDispatcher("/jsp/UseBean.jsp");
view.forward(request,response);
}
}

UseBean.jsp

Quote:
<%@ page import="learn.*" %>
<HTML>
<TITLE>
USE BEAN EXAMPLE
</TITLE>

<BODY>
<%
Person p = (Person)request.getAttribute("PersonObj");
%>
Name : <%= p.getName()%>
</BODY>

</HTML>

Hope it helps.
Reply With Quote
Reply

  Geeks Talk > Web Development > JSP

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
What does printf function returns? chaitanyawithu C and C++ 6 05-20-2008 02:03 PM
I have 4 million row but mapping returns only 400000 rows palakpari Data Warehousing 1 07-03-2007 02:09 AM
Built-in and plugin stages, Chaning Null to Not Null and Types of lookup Geek_Guest Data Warehousing 0 04-06-2007 04:48 AM
Query that returns all responsibilities of a function JobHelper Oracle 0 01-04-2007 09:46 AM
Web Service method returns a vector of objects ssumitha ASP.NET 1 12-14-2006 08:25 AM


All times are GMT -4. The time now is 11:18 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved