• Post Reply Bookmark Topic Watch Topic
  • New Topic

request.getAttribute() returns null in jsp

 
Harsh Motha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,

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.

Would appreciate all your help.

Regards,
Harsh
 
Harsh Motha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small correction ,

In Jsp Page :

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

<BODY>
Name : <%= ((Person)request.getAttribute("PesronObj"))%>
</BODY>

</HTML>

I have tried every possible thing i could i am not getting solution

Please help.
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harsh,

use code tag for a code that will be easy to read.
Servlet Code :

Jsp Page :


 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Above code looks fine to me,
It should print hash code of Person object created in servlet.
 
tom mickey
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP, did you notice the typo here?

Edit:
Nevermind, it looks like you used the same in your servlet.
 
Harsh Motha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Shailesh I will use code tag,

I have tried all possible ways i am not able to solve this, in fact i even tried setting servletcontext.attribute and tried accessing in the jsp, even that dint work, however when i try accessing the attribute in the same servlet where i set the value in request or servletcontext scope i was able to but i am not able to access it after i forward it to jsp using requestDispacther. I seriously dont understand, please help me , your help will be greatly appreciated.

Regards,
Harsh
 
Deepali Anand
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing seems to be wrong in the code.

My guess - Hope you are not entering the name of your JSP file in the browser. Remember, you need to open HTML page in the browser. Enter the fields which go to servlet and then result is displayed in JSP.

Hope this helps
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!