这是 a.jsp
<% String str = (String)session.getAttribute("str");
if(str == null) {
str = "aaa";
}
out.println("<script>alert('"+str+"');</script>");
%>
<html><head></head>
<body>
<form name="myform" method="post">
<input type="button" name="ss" onclick="go()" />
</form>
</body></html>
<script>
function go(){
myform.action = "/test";
myform.submit();
}
</script>以下是 test servlet
dopost(...){
session.setAttribute("str","bbb");
out.println("<script>window.location.href='a.jsp';</script>");
}
请大家看看,第一次访问a.jsp的时候,alert 应该是 "aaa",但点击button后,为什么
alert 的还是"aaa"呢?要手动刷新一次页面,才会alert "bbb" 。请大家给点意见,谢谢!
<% String str = (String)session.getAttribute("str");
if(str == null) {
str = "aaa";
}
out.println("<script>alert('"+str+"');</script>");
%>
<html><head></head>
<body>
<form name="myform" method="post">
<input type="button" name="ss" onclick="go()" />
</form>
</body></html>
<script>
function go(){
myform.action = "/test";
myform.submit();
}
</script>以下是 test servlet
dopost(...){
session.setAttribute("str","bbb");
out.println("<script>window.location.href='a.jsp';</script>");
}
请大家看看,第一次访问a.jsp的时候,alert 应该是 "aaa",但点击button后,为什么
alert 的还是"aaa"呢?要手动刷新一次页面,才会alert "bbb" 。请大家给点意见,谢谢!
throws ServletException,IOException
{ response.setContentType("text/html; charset=gb2312");
request.setAttribute("str","bbb");
ServletContext sc = getServletContext(); RequestDispatcher rd = null; rd = sc.getRequestDispatcher("/a.jsp"); //定向的页面 rd.forward(request, response);}然后是a.jsp<% String str = (String)request.getAttribute("str");
if(str == null) {
str = "aaa";
}
out.println(" <script> alert('"+str+"'); </script> ");
%>
<html> <head> </head>
<body>
<form name="myform" method="post">
<input type="button" name="ss" onclick="go()" />
</form>
</body> </html>
<script>
function go(){
myform.action = "/test";
myform.submit();
}
</script>
还有,可以参考这个代码,通知IE,不要缓冲
<%
request.setCharacterEncoding("GBK");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<%!// return current time to proxy server request
@Override
public long getLastModified(@SuppressWarnings("unused")
HttpServletRequest request) {
return System.currentTimeMillis();
}%>原文地址:http://www.java2000.net/viewthread.jsp?tid=288
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">以后写jsp页面,都加这些东西。