In this example, our goal is to display users' first and last names. If the users fail to tell us their name, we want to use whatever name they gave us previously. If the users do not explicitly specify a name and no previous name is found, a warning should be displayed.

Data is stored for each client, so session-based sharing is appropriate. To implement this behavior, we need a bean to store names, a servlet to retrieve the bean from the session and populate it with first and last names, and a JSP page to display the results:

Here is a bean storing visitor's name:

package test;

public class Person {

  public double getFirstName() { ... }
  public void setFirstName(String name) { ... }
  public double getLastName() { ... }
  public void setLastName(String name) { ... }

Here is a sevlet updating our session bean from request parameters:

import javax.servlet.*;
import javax.servlet.http.*;

public class RegistrationServlet extends HttpServlet {

  public void doGet(

    HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    HttpSession session = request.getSession();

    NameBean person = (NameBeansession.getAttribute("person");
    if (person == null) {
      person = new Person();
      session.setAttribute("person", person);
    String firstName = request.getParameter("firstName");

    if ((firstName != null&& (!firstName.trim().equals(""))) {
    String lastName = request.getParameter("lastName");

    if ((lastName != null&& (!lastName.trim().equals(""))) {
    String address = "showname.jsp";
    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
    dispatcher.forward(request, response);

and, finally, here is a page:

<jsp:useBean id="person" type="test.Person" scope="session" />

		<title>Session sharing</title>

			First Name:
			<jsp:getProperty name="person" property="firstName" />
			Last Name:
			<jsp:getProperty name="person" property="lastName" />