整个会话期间都没有记住JavaBean

时间:2013-12-03 18:14:37

标签: java jsp session servlets

我目前正在学习JSP / Servelets。以下是包含2个JSP页面和servlet的代码,其工作方式如下:

  1. JSP#1请求用户信息,&将它传递给servlet。
  2. servlet实例化一个JavaBean类,并使用 Session 对象将其传递给第二个JSP。
  3. 然后第二个JSP显示用户在第一个JSP中输入的信息;然后,用户可以点击返回按钮返回第一个JSP。
  4. 我的问题是:我已经阅读了各种教程(尤其是Murach的JSP,这段代码所基于的),如果将Javabean属性传递给会话对象而不是请求对象,应该在整个会话期间维护JavaBean的值。但是当我返回第一页时,JB字段为空。我做错了吗?我将不胜感激任何帮助!

    以下是代码:

    第一个JSP:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Email Application</title>
        </head>
    
        <body>
        <h1>Join our Email list</h1>
        <p>To join our email list, enter your name and email address below.<br>
        Then, click on the Submit button</p>
    
        <form action="addToEmailList" method="post">
        <jsp:useBean id="user" scope="session" class="business.User"/>
            <table cellspacing="5" border="0">
                <tr>
                    <td align="right">First name:</td>
                    <td>
                        <input type="text" name="firstName"
                        value = "<jsp:getProperty name="user" property="firstName"/>">
                    </td>
                </tr>
                <tr>
                    <td align="right">Last name:</td>
                    <td><input type="text" name="lastName"
                        value = "<jsp:getProperty name="user" property="lastName"/>">
                    </td>
                </tr>
                <tr>
                    <td align="right">Email address:</td>
                    <td><input type="text" name="emailAddress"
                        value = "<jsp:getProperty name="user" property="emailAddress"/>">
                    </td>
                </tr>
                <tr>
                    <td>I'm interested in these types of music:</td>
                    <td><select name="music" multiple>
                            <option value="rock">Rock</option>
                            <option value="country">Country</option>
                            <option value="bluegrass">Bluegrass</option>
                            <option value="folkMusic">Folk Music</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td ></td>
                    <td><input type="submit" name="Submit"></td>
                </tr>
            </table>
        </form>
    
    </body>
    

    的Servlet

    public class AddToEmailListServlet extends HttpServlet
    {
    private static final long serialVersionUID = 1L;
    private HttpServletRequest request;
    private HttpServletResponse response;
    private String firstName;
    private String lastName;
    private String emailAddress;
    private String[] musicTypes;
    private Music music;
    private User user;
    
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        this.request = request;
        this.response = response;
        setInstanceVariables();
        writeDataToFile();
        forwardResponseToJSPpage();
        System.err.println("Hello!");
    }
    
    private void setInstanceVariables()
    {
        this.firstName = getFirstName();
        this.lastName = getLastName();
        this.emailAddress = getEmailAddress();
        this.musicTypes =  request.getParameterValues("music"); //getMusic();
        this.user = new User(firstName,lastName,emailAddress);
    }
    
    private String getFirstName()
    {
        return  request.getParameter("firstName");
    }
    
    private String getLastName()
    {
        return request.getParameter("lastName");
    }
    
    private String getEmailAddress()
    {
        return request.getParameter("emailAddress");
    }
    
    
    private void writeDataToFile() throws IOException
    {
        String path = getRelativeFileName();
        UserIO.add(user,path);
    }
    
    private String getRelativeFileName()
    {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/EmailList.txt");
        return path;
    }
    
    private void forwardResponseToJSPpage() throws ServletException, IOException
    {
        if(this.emailAddress.isEmpty()||this.firstName.isEmpty()||this.lastName.isEmpty())
        {
            String url = "/validation_error.jsp";
            RequestDispatcher dispatcher = 
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request,response);
        }
        else
        {
        music = new Music(musicTypes); 
    
        HttpSession session = this.request.getSession();
        session.setAttribute("music", music);
        session.setAttribute("user", user);
    
        String url = "/display_email_entry.jsp";
        RequestDispatcher dispatcher = 
                getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request,response);
        }
    }
    
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request,response);
    }
    
    }
    

    第二个JSP:

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@ include file="/header.html" %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Email Application</title>
    </head>
        <body>
        <h1>Thanks for joining our email list</h1>
    
         <p>Here is the information that you entered:</p>
    
    <jsp:useBean id="user" scope="session" class="business.User"/>
    <table cellspacing="5" cellpadding="5" border="1">
        <tr>
            <td align="right">First name:</td>
            <td><jsp:getProperty name="user" property="firstName"/></td>
        </tr>
        <tr>
            <td align="right">Last name:</td>
            <td><jsp:getProperty name="user" property="lastName"/></td>
        </tr>
        <tr>
            <td align="right">Email Address:</td>
            <td><jsp:getProperty name="user" property="emailAddress"/></td>
        </tr>
    </table>
    
    
    <p>We'll use the Email to otify you whenever we have new releases of the following types of music:</p>
    
    <c:forEach items="${music.musicTypes}" var="i">
        <c:out value="${i}"></c:out><br/>
    </c:forEach>
    
    <p>To enter another email address, click on the Back <br>
    button in your browser or the Return button shown <br>
    below.</p>
    
    <form action="join_email_list.jsp" method="post">
        <input type="submit" value="Return">
    </form>
    </body>
    <%@ include file="/footer.html" %>
    </html>
    

0 个答案:

没有答案