servlet为什么不更新参数?

时间:2018-12-11 09:53:59

标签: java jsp servlets

单击按钮时,Servlet接收请求。 Servlet将foo参数设置为随机数x。 System.out.println(x)在每个请求中产生不同的值。但是,在客户端,console.log(${foo})总是打印生成的第一个数字。发生了什么事?

Servlet:

public class First extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        double x = Math.random();
        req.getSession().setAttribute("foo", x);
        System.out.println(x);
        RequestDispatcher rd = req.getRequestDispatcher("/index.jsp");
        rd.forward(req, resp);
    }
}

index.jsp

<!DOCTYPE html>
<html>
    <head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    </head>
    <body>
        <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
        <c:out value = "${foo}"/>
        <script>
            $(document).on("click", "#button", function() { 
                $.get("first", function(responseText) {  
                 console.log(${foo});
                });
            });
        </script>
        <input type="submit" id="button">
    </body>
</html>

0 个答案:

没有答案