Forums Register Login

request.getAttribute() returns null in jsp

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Harsh,

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

Jsp Page :


+Pie Number of slices to send: Send
Above code looks fine to me,
It should print hash code of Person object created in servlet.
+Pie Number of slices to send: Send
OP, did you notice the typo here?

Edit:
Nevermind, it looks like you used the same in your servlet.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 64875 times.
Similar Threads
unable to access requested scoped attribute in page scoped JSP
can't able to run <jsp:useBean> in jsp page
Problem with retreving the Attribute in jsp
Problem displaying implicit request attributes on the forwarded page.
jsp:usebean not working
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:41:11.