Beans and the session object

In this JSP Tutorial we’ll cover Beans and the session object. The session object is an object that has a certain state per user. You can enter data in it so the server will remember user-input. You can insert objects in this object (on the condition that the class where the object instantiated from is serializable). To transfer data from a JSP to a servlet it’s possible to insert some data in this session object in one of the files and read it from the other one. We will use Beans for this. JSP Beans are classes that are serializable and that can store data.

We could use a Bean to remember the dice from the dice-game we created earlier.

public class DiceGameBean implements Serializable {
    private int die1;
    private int die2;
}

We’ll have to create a setter and a getter for both integers.

Now we’ll insert some data in the Bean:

public class DiceServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int first = (int)((Math.random()*6)+1);
        int second = (int)((Math.random()*6)+1);

        HttpSession session = request.getSession();
        DiceGameBean diceGameBean = ((DiceGameBean)session.getAttribute("diceGameBean"));

        diceGameBean.setDie1(first);
        diceGameBean.setDie2(second);
    }
}

You can see that the code is less messy and the data is stored inside the bean. The session object looks for a property with the name “diceGameBean” and returns it.

On itself it doesn’t do very much, so we’ll have to create a JSP to handle the data:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="diceGameBean" class="beans.DiceGameBean" scope="session" />
<html>
  <head><title>Simple jsp page</title></head>
  <body>

    First number: ${diceGameBean.die1}
  <br />
    Second number: ${diceGameBean.die2}

  </body>
</html>

First we’ll define the Bean at the top of the JSP. We’ll define the type (class) as well and make sure the object is callable anywhere inside the session scope.

The ${}-tags are used to call properties from a Bean. It uses reflection so we don’t have to call the getDie1 method but just call die1. The JSP-engine will call the getDie1 method for us. This way a designer has to read less JAVA-code when creating a great layout.

The last thing to do is go to the right page. We’ll browse to “dice.do” and we want to end up at the JSP. We’re going to use the “response.sendRedirect” method to redirect to a JSP.

public class DiceServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int first = (int)((Math.random()*6)+1);
        int second = (int)((Math.random()*6)+1);

        HttpSession session = request.getSession();
        DiceGameBean diceGameBean = ((DiceGameBean)session.getAttribute("diceGameBean"));

        diceGameBean.setDie1(first);
        diceGameBean.setDie2(second);

        response.sendRedirect("dicegame.jsp");
    }
}