0

I am creating an instance of a bean and setting its value in 1 jsp page and after setting that bean in session attribute, I am trying to retrieve value of that bean using jsp action .

In the first jsp page, I am creating instance of Person using new keyword and then setting it as attribute in session.

On the second page, I am trying to print the name of the Person using jsp:getProperty tag.

The value comes null in the second page.

Code for first Page is as below

 <%
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        if((username.equals("ravi") && password.equals("kumar")))
            {
                Person person = new Person();
                person.setName(username);
                session.setAttribute("username",person);

                response.sendRedirect("Home.jsp");
            }
        else
            response.sendRedirect("Error.jsp");
        %>

Code for second page is as below :

 <jsp:useBean id="person" class="com.ravi.entity.Person" scope="request"/>
 Person created by Servlet&nbsp;<jsp:getProperty name="person" property="name"/>

Can any one help on this?

Ravi.Kumar
  • 761
  • 2
  • 12
  • 34

1 Answers1

2

The first JSP page stores the person in the session. The second JSP page tries to get it from the request. That's why you get null:

<jsp:useBean id="person" class="com.ravi.entity.Person" scope="request"/>
                                You should have "session" here ---^

Note that jsp:useBean and scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and user servlets for the Java code, or an MVC framework.

informatik01
  • 16,038
  • 10
  • 74
  • 104
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1. Also Sotirious's comment points the OP in the right direction. As an additional reading for the OP: [The use of scriptlets in JSP is discouraged](http://stackoverflow.com/a/3180202/814702) – informatik01 Dec 04 '13 at 20:33
  • I disagree. After a POST, and especially a POST to a login form, a redirect is the right thing to do. – JB Nizet Dec 04 '13 at 22:01
  • Actually what I meant was that he correctly explained the **reason** behind why the previously saved attributes were no longer available. And I considered it as an **addition to your answer**. – informatik01 Dec 04 '13 at 23:54
  • I am beginner is JSP, So, I am trying to learn these things. I am pretty much aware of that I should build application using MVC framework. – Ravi.Kumar Dec 05 '13 at 02:23
  • JB Nizet : I tried using session in scope but still getting null value. – Ravi.Kumar Dec 05 '13 at 02:24
  • When I am using EL for printing value for name. I am getting no value – Ravi.Kumar Dec 05 '13 at 07:03
  • Check your browser cookie settings: cookies should be accepted. Also check with Firebug or Chrome dev tools that the session cookie is received after the first JSP and sent with the request sent to the second JSP. – JB Nizet Dec 05 '13 at 07:31