Hi,
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:
UserBean.java
public class UserBean {
private
String customerID;
public UserBean() {
}
public String getCustomerID() {
return customerID;
}
public void setCustomerID(String cID) {
customerID = cID;
}
}
UserController.java
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();
user.setCustomerID("13456");
session.setAttribute(UserBean.USER, user);
}
RequestDispatcher rd = null;
String page = request.getParameter("Page");
if (page == null ) {
rd = getServletConfig().
getServletContext ().
getRequestDispatcher("/jsp/login.jsp");
} 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);
}
}
}
login.jsp
<%@ 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">