This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes JavaBean between Servlet and JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "JavaBean between Servlet and JSP" Watch "JavaBean between Servlet and JSP" New topic

JavaBean between Servlet and JSP

Mark Chen

Joined: Sep 03, 2001
Posts: 4
I got a problem with sharing one JavaBean between Servelt and JSP. I instaniated a JavaBean called UserBean in my Servelt clas called UserController and stored it in the session object and then forwarded it to a JSP called login.jsp. In login.jsp I can access this UserBean instance(get the value which is set in Servlet). There's no problem with that. What I try to do is using this UserBean intance to bring some information the user types in back to the UserController servlet. Here the problem comes out. I find I can't get the right value which the user inputs in login.jsp. Instead I still get the value I set in UserController servelt before forwarding to login.jsp. I don't know what causes the problem and how to fix it. It would be much appreciated if someone could gives some hints or suggestions.
The servelt/JSP engin is TOMCAT 3.2.3
My code snippets:
public class UserBean {
private String customerID;
public UserBean() {
public String getCustomerID() {
return customerID;
public void setCustomerID(String cID) {
customerID = cID;
public class UserController extends HttpServlet {
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
UserBean user = null;
user = (UserBean)session.getAttribute("User");
if ( user == null ) {
user = new UserBean();
session.setAttribute(UserBean.USER, user);
RequestDispatcher rd = null;
String page = request.getParameter("Page");
if (page == null ) {
rd = getServletConfig().
getServletContext ().
} else if (nextPage.equals("LOGIN" ) ){
UserBean f = (UserBean)session.getAttribute("User");
if ( f != null ){
System.out.println("ID="+f.getCustomerID()); //???
if ( rd != null ){
rd.forward(request, response);
<%@ page import="superVISor.*" session="true" %>
<jsp:useBean id="User" class="superVISor.UserBean" scope="session" />
<jsp:setProperty name="User" property="customerID" param="customerID"/>
<FORM ACTION="/superVISOR/UserController" METHOD="post">
<INPUT TYPE="text" NAME="customerID" VALUE='<%=User.getCustomerID()%>' SIZE=30 MAXLENGTH=35></TD>
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
<INPUT TYPE="hidden" NAME= "Page" VALUE="LOGIN">
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037

Near as I can tell you have not confirmed that the session is even showing up in the servlet. The code:
HttpSession session = request.getSession(true);
will generate a new empty session if one is not found. Seems to me the first thing to do is to see if that is a new session or the old one.
If the JSP and the servlet are not in the same "web application" they won't be able to share a session.
Mark Chen

Joined: Sep 03, 2001
Posts: 4
Thanks Bill,
I've checked that the sessions I get via request.getSession(true) are the same. I can access the JavaBean stored in session. And this JavaBean brings the data to JSP and can't bring the new data in JSP back. It seems <jsp:useBean ...> and <jsp:setProperty ...> don't do their job.
Arun, Shanmugham
Ranch Hand

Joined: Apr 05, 2001
Posts: 34
Hi Mark,
I checked your code. I think that is because of the harcoded value "12345" in setCustomerID(). I think you are not setting the new value back in the session object by getting the new value with request.getParameter() function.
Please verify if this is correct in your respect,
Changed the code as
if (request.getParameter("customerID")==null)

Sun Certified Programmer for Java2 Platform

Sun Certified Programmer for Java2 Platform
I agree. Here's the link:
subject: JavaBean between Servlet and JSP
It's not a secret anymore!