Passing custom objects from servlet to JSP

I want to pass a custom object of type Student from the servlet to the JSP. I created a Student bean class. The student contains 2 properties firstname and lastName.

Student bean:

import java.io.Serializable; public class Student implements Serializable { public Student() { } String firstName; String lastName; 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; } } 

HTML file to get FirstName and LastName from the user:

 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <form id="myForm" method="POST" action="MyFormServlet"> FirstName<input type="text" id="firstName" name="FirstName"/><br> LastName<input type="text" id="lastName" name="LastName"/><br> <button type="submit" />Submit</button> </form> </body> </html> 

Servlet Code:

 import javax.servlet.RequestDispatcher; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class MyFormServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) { Student s = new Student(); s.setFirstName(request.getParameter("FirstName")); s.setLastName(request.getParameter("LastName")); HttpSession session =request.getSession(); session.setAttribute("student", s); try { RequestDispatcher rd = getServletContext().getRequestDispatcher("/myJsp.jsp"); rd.forward(request, response); } catch (Exception e) { e.printStackTrace(); } } } 

myJsp.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> </head> <body> <% // I want to do something like this : //Student student =(Student)session.getAttribute("student"); //String fullName=student.firstName + student.lastName; %> <h1><%=fullName%></h1> </body> </html> 

I want to get the student object, access its attributes and store it in a JSP variable for further processing.

+6
source share
4 answers

The setAttribute() method on request , session and servletContext will already make it available as JSP / EL by attribute name.

In your specific case, with the next line in the servlet

 session.setAttribute("student", s); 

it is available in JSP / EL as ${student} . So, only this should do:

 <body> <h1>${student.firstName} ${student.lastName}</h1> </body> 

If you want to save it as another variable in JSP so that it can be reused several times, use JSTL <c:set> .

 <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> ... <body> <c:set var="fullName" value="${student.firstName} ${student.lastName}" /> <h1>${fullName}</h1> </body> 
+10
source

You must pass attributes to query the scope if you do not want to use the entire session. In Servlet,

  request.setAttribute("student", s); 

In JSP,

 Student student =(Student) request.getAttribute("student"); String fullName = "Default"; if(student!=null){ fullName=student.firstName +" " + student.lastName; } 
+3
source

It is not necessary to use request.setAttribute () to send data. you can use HttpSession for this. You must install it first, as you have already done.

 HttpSession session =request.getSession(); session.setAttribute("student", s); 

Now you can return it to jsp using getAttribute ()

 Student student =(Student) session.getAttribute("student"); 

Now you can play with your student as you wish. (You can import your Student class into jsp.)

+1
source

I had the same problem as after the trace and error, I got my solution as shown below -

 <% Student student = (Student)request.getAttribute("student");%><br> <table><br> <tr><td>First Name :</td><td><%=student.getFirstName() %></td></tr><br> <tr><td>Last Name :</td><td><%=student.getLastName() %></td></tr><br> </table><br> 
0
source

Source: https://habr.com/ru/post/914020/


All Articles