• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaBean between Servlet and JSP

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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">
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic