Сервлет получает запрос при нажатии кнопки.Сервлет устанавливает для параметра foo случайное число x.System.out.println(x)
производит различное значение при каждом запросе.Однако на стороне клиента console.log(${foo})
всегда печатает первое сгенерированное число.Что происходит?
Сервлет:
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>