HttpSession不会从JSP持久存在于Servlet中

时间:2015-06-03 18:15:05

标签: java jsp session servlets

我有InitializeServlet创建并实例化HttpSession,然后重定向到JSP(betFinalize.jsp)。在这里,我可以参加我的会议。从该JSP开始,我将(通过表单)重定向到另一个Servlet,FinalizeServlet我松开了会话。我无法弄清楚为什么。以下代码。

InitializeServlet.java

public class InitializeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String fName = request.getParameter("fName");
        String lName = request.getParameter("lName");
        String result = request.getParameter("result");


        Bet s = new Bet();
        s.setFirstLastName(fName + " " + lName);
        s.setResult(result);
        s.setMultiplier(calculateMultiplier());

        request.getSession(true).setAttribute("bet", s);

        RequestDispatcher rd = getServletContext().getRequestDispatcher("/betFinalize.jsp");
        rd.forward(request, response);
    }

    private double calculateMultiplier() {

        return 0.8;
    }

}

betFinalized.jsp

<%@ page import="it.unibo.tw.model.beans.Bet"%>
<%@ page import="it.unibo.tw.model.beans.Bets"%>

<%@ page session="true"%>

<jsp:useBean id="bet" class="it.unibo.tw.model.beans.Bet" scope="session"></jsp:useBean>
<jsp:useBean id="finalizedBets" class="it.unibo.tw.model.beans.Bets" scope="application"></jsp:useBean>

<!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">
</head>
<body>

    <h1>Hi, <%= bet.getFirstLastName() %></h1>
    <h2>Current bet: <i><%= bet.toString() %></i></h2>

    <form action="finalize" method="get">
        <input id="import" type="number" name="import" onkeyup="calculateWin()" ><br />
        <input id="win" type="text" name="win" readonly ><br />
        <input type="submit">
    </form>

    <hr />

    <ul>
        <%  for(Bet s : finalizedBets.getList()) { %>
        <li><%= s.toString() %></li>
        <%  } %>
    </ul>
</body>

<script>
    function calculateWin() {
        var multiplier = <%= bet.getMultiplier() %>

        var imp = document.getElementById("import").value
        document.getElementById("win").value = imp * multiplier
    }


</script>

</html>

FinalizeServlet.java

public class FinalizeServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        double vincita = Double.parseDouble(request.getParameter("win"));

        Bet s = (Bet) request.getSession().getAttribute("bet");
        if(s != null)   {

            // NEVER REACH HERE

            s.setWin(vincita);
            s.setFinalized(true);


            Bets scommesseFinalized = (Bets) getServletContext().getAttribute("scommesseFinalized");
            scommesseFinalized.getList().add(s);


        }

        request.getSession().invalidate();

        RequestDispatcher rd = getServletContext().getRequestDispatcher("/start.html");
        rd.forward(request, response);
    }
}

2 个答案:

答案 0 :(得分:1)

您正试图获取错误的属性。设置会话变量时:

request.getSession(true).setAttribute("scommessa", s);

当您尝试阅读时:

request.getSession().getAttribute("bet");

它应该是:

request.getSession().getAttribute("scommessa");

代替。

答案 1 :(得分:0)

在表单的操作中对会话ID进行编码,如下所示:

<form action="<%=response.encodeURL("finalize")%>" method="get">