aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "JavaBean between Servlet and JSP" Watch "JavaBean between Servlet and JSP" New topic
Author

JavaBean between Servlet and JSP

Mark Chen
Greenhorn

Joined: Sep 03, 2001
Posts: 4
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">
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5

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.
Bill
Mark Chen
Greenhorn

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.
Mark
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)
user.setCustomerID("123456");
else
user.setCustomerID(request.getParameter("customerID"));

Regards
Arun
Sun Certified Programmer for Java2 Platform


Sun Certified Programmer for Java2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaBean between Servlet and JSP