4

As mentioned below I have made changes to the code which looks like following example, but it doesn't show firstname and lastname in JSP:

Servlet code:

//.... HttpSession session = request.getSession(); Person person = (Person) session.getAttribute("person"); if (person == null) { person = new Person(); } person.setNewId(newId); person.setFirstName(firstName); person.setLastName(lastName); session.setAttribute("person", person); RequestDispatcher rd = request.getRequestDispatcher("jsp Address"); rd.forward(request, response); 

Person Bean code:

private int newId; private String firstName; private String lastName; // Default Constructor public Person() {} public Person(int newId, String firstName, String lastName) { setNewId(newId); setFirstName(firstName); setLastName(lastName); } //Getter and Setter Methods public int getNewId() {return newId;} public void setNewId(int newID) {this.newId = newID;} public String getFirstName() {return firstName;} public void setFirstName(String FirstName) {this.firstName = FirstName;} public String getLastName() {return lastName;} public void setLastName(String LastName) {this.lastName = LastName;} 

And in JSP code:

<jsp:useBean id="person" type="app.models.Person" scope="session"> <jsp:getProperty name="person" property="firstName" /> <jsp:getProperty name="person" property="lastName" /> </jsp:useBean> 

Output of this JSP page: none

Expected output: firstName lastName

Questions:

1. How can i pass parameters from Servlets to JSP via Bean with help of Session? 2. Is there a better way to do this code? I am using MVC architecture. 
4
  • How are you accessing the jsp ? -- Also, there is no empty constructor in your Person class EDIT: Just saw your response about the empty constructor. Can you clarify how you are accessing the servlet/jsp? Commented Apr 27, 2011 at 1:34
  • I am using RequestDispatcher from Servlet to transfer control to named JSP. I have a default constructor. EDIT: I am accessing servlet via DD and JSP through Request Dispatcher. Commented Apr 27, 2011 at 1:38
  • After you've deployed this code to your servlet container, you are accessing the servlet first, I hope? Do you get any exceptions in the logs? Have you tried adding some logs before you dispatch request ? Commented Apr 27, 2011 at 1:43
  • No! I don't get any exceptions. But my JSP doesn't catch the parameters that I am passing through JSP >>Servlet. Commented Apr 27, 2011 at 1:45

1 Answer 1

2

Get rid of the <jsp:useBean>. When using the type attribute instead of class, it won't check if there's already an instance in the scope, it will plain override the Person instance which you created in the servlet with a new, blank, default constructed instance.

When using "MVC architecture", the <jsp:useBean> tag is useless. Remove it and just use usual EL to access it:

${person.firstName} ${person.lastName} 

Or better, to prevent XSS attacks, use JSTL <c:out>.

<c:out value="${person.firstName} ${person.lastName}" /> 
Sign up to request clarification or add additional context in comments.

3 Comments

I suggested the class but that didn't work either. Wonder why.
@CoolBeans: Ah well, then it's either not properly rebuilt or he is running the JSP the wrong way (by calling the JSP URL directly instead of the servlet URL).
I just used Expression Language instead of <jsp:useBean> and its working. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.