我有一个jsp页面,把两个值(用户:密码)设置到session里,传给一个servlet,servlet把这两个值设置到数据库里。
当我第一次运行的时候,servlet从session中得到的值正确,但第二次得到却是第一次的那个值,不知道原因是什么,那位帮我看看,谢谢!jsp部分代码如下:
<script>
<!--
function InsertUserID(){
open("/Login/userinsertservlet","bottom","");
}
//-->
</script>
</head><body text="#000066" link="#0000ff" vlink="#336699" alink="#33ffff">
<div id="Layer1">
<table width="100%" border="0" cellspacing="0">
<tr>
<th align="center"><span class="style1">登录确认</span></th>
</tr>
</table>
<!--Get UserID and Password-->
<%
String userId = request.getParameter("UserID");
String passwd = request.getParameter("Password");
session.setAttribute("UserID", userId);
session.setAttribute("Password", passwd);
%>
<p class="style2">
<div align="center">UserID【<%=userId%>】登录确认</div>
</p>
<form id="form1" name="form1" method="post" action="UserList.jsp">
<div align="center"><br />
<input type="button" name="Confirm" tabindex="1" value="确定" class="style3" onclick="InsertUserID()"/>
<input type="submit" name="Cancel" tabindex="2" value="取消" class="style3" />
</div>
</form>
</div></body>
</html>servlet部分代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = null;
String passwd = null;
String path = null;
ServletContext context = null;
CUserInsertManager cuim = null;
RequestDispatcher rd = null;
HttpSession hSession = null;
try {
context = getServletContext();
cuim = new CUserInsertManager();
hSession = request.getSession(true);
userId = (String)hSession.getAttribute("UserID");
passwd = (String)hSession.getAttribute("Password");
// 输出到一个页面,检查具体接收到的值
path = "/UserInsertError.jsp?UserID="+userId+"&Password="+passwd;
rd = context.getRequestDispatcher(path);
rd.forward(request, response);
当我第一次运行的时候,servlet从session中得到的值正确,但第二次得到却是第一次的那个值,不知道原因是什么,那位帮我看看,谢谢!jsp部分代码如下:
<script>
<!--
function InsertUserID(){
open("/Login/userinsertservlet","bottom","");
}
//-->
</script>
</head><body text="#000066" link="#0000ff" vlink="#336699" alink="#33ffff">
<div id="Layer1">
<table width="100%" border="0" cellspacing="0">
<tr>
<th align="center"><span class="style1">登录确认</span></th>
</tr>
</table>
<!--Get UserID and Password-->
<%
String userId = request.getParameter("UserID");
String passwd = request.getParameter("Password");
session.setAttribute("UserID", userId);
session.setAttribute("Password", passwd);
%>
<p class="style2">
<div align="center">UserID【<%=userId%>】登录确认</div>
</p>
<form id="form1" name="form1" method="post" action="UserList.jsp">
<div align="center"><br />
<input type="button" name="Confirm" tabindex="1" value="确定" class="style3" onclick="InsertUserID()"/>
<input type="submit" name="Cancel" tabindex="2" value="取消" class="style3" />
</div>
</form>
</div></body>
</html>servlet部分代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = null;
String passwd = null;
String path = null;
ServletContext context = null;
CUserInsertManager cuim = null;
RequestDispatcher rd = null;
HttpSession hSession = null;
try {
context = getServletContext();
cuim = new CUserInsertManager();
hSession = request.getSession(true);
userId = (String)hSession.getAttribute("UserID");
passwd = (String)hSession.getAttribute("Password");
// 输出到一个页面,检查具体接收到的值
path = "/UserInsertError.jsp?UserID="+userId+"&Password="+passwd;
rd = context.getRequestDispatcher(path);
rd.forward(request, response);
<%
String userId = request.getParameter("UserID");
String passwd = request.getParameter("Password");
session.setAttribute("UserID", userId);
session.setAttribute("Password", passwd);
%>
这样的代码的,这个应该在servlet里来写。
还有,如果没有猜错,你第一次访问页面的时候,得到的结果应该是UserID和Password都是空的,如果没有采用刷新的方式,应该看到的都是空值。因为你第一次执行jsp的时候request里还没有UserID和Password这两个值的。
只要把上面的代码放到servlet里就可以了。
那就debug看看了,你贴出来的代码好像是没有什么问题的。但是比较正常的话应该是吧这个值设置到新的request里才比较合适啊(采用隐含字段)。
祝你好运了。