title: Passing custom objects from servlet to JSP
body: <p>I want to pass a custom object of type Student from a servlet to JSP.
  I have created a Student bean class. Student contains 2 properties firstname and lastName. </p>
  
  <p>Student bean:</p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p>HTML file for taking FirstName and LastName from the user:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="myForm" method="POST" action="MyFormServlet"&gt;
          FirstName&lt;input type="text" id="firstName"  name="FirstName"/&gt;&lt;br&gt;
          LastName&lt;input type="text" id="lastName" name="LastName"/&gt;&lt;br&gt;
          &lt;button type="submit" /&gt;Submit&lt;/button&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Servlet Code:</p>
  
  <pre><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();
              }
          }
  
      }
  </code></pre>
  
  <p>myJsp.jsp</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;/head&gt;
      &lt;body&gt;
          &lt;%
              // I want to do something like this :   
              //Student student =(Student)session.getAttribute("student");
              //String fullName=student.firstName + student.lastName;
          %&gt;      
          &lt;h1&gt;&lt;%=fullName%&gt;&lt;/h1&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I want to get the 'student' object, access its attributes and store it in a JSP variable for  further processing.</p>