java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3946
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use session-based bean sharing E-mail
User Rating: / 36
PoorBest 

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 java.io.*;
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(""))) {
      person.setFirstName(firstName);
    }
    
    String lastName = request.getParameter("lastName");

    if ((lastName != null&& (!lastName.trim().equals(""))) {
      person.setLastName(lastName);
    }
    
    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" />

<html>
	<head>
		<title>Session sharing</title>
	</head>
	<body>

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


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.