servlet代码如下:public class Check extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
UserOper us = new UserOper();
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
if( "".equals(name) || "".equals(pwd)){
return;
}
User u = new User(pwd, name);
response.setCharacterEncoding("GBK");
boolean userExistsFlag = us.checkUserExists(u);
if (userExistsFlag == true) {
// 验证用户名和密码匹配
boolean namePwdMatch = us.matchNamePwd(u);
if(namePwdMatch == true) {
response.sendRedirect("../index.jsp?name=" + name);
System.out.println("in sevlet check name is " + name);
}else {
response.sendRedirect("../noPri.jsp?reason=nomatch");
}
} else {
response.sendRedirect("../noPri.jsp?reason=noname");
}
}
}JSP代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link href="css/page.css" rel="stylesheet" type="text/css"></style>
<script language="javascript" src="js/check.js"></script>
</head>
<%
request.setCharacterEncoding("GBK");
String sessionName = (String)session.getAttribute("sessionName");
String paramName = (String)request.getParameter("name");
System.out.println("In index.jsp, param from servlet is " + paramName);
if(!"".equals(sessionName)){
out.println("<b><font color='blue'>" + sessionName + "</font></b> login");
}
if(!"".equals(paramName)){
out.println("<b><font color='blue'>" + paramName + "</font></b> login");
session.setAttribute("sessionName", paramName);
}
%>
<body>
<form action="servlet/Check" method="post" onSubmit="return check();">
<table width="120">
<tr>
<td align="right" >name</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td align="right">password</td>
<td><input type="password" name="pwd" id="pwd"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>打印出paramName异常~~~求解。在线等。
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
UserOper us = new UserOper();
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
if( "".equals(name) || "".equals(pwd)){
return;
}
User u = new User(pwd, name);
response.setCharacterEncoding("GBK");
boolean userExistsFlag = us.checkUserExists(u);
if (userExistsFlag == true) {
// 验证用户名和密码匹配
boolean namePwdMatch = us.matchNamePwd(u);
if(namePwdMatch == true) {
response.sendRedirect("../index.jsp?name=" + name);
System.out.println("in sevlet check name is " + name);
}else {
response.sendRedirect("../noPri.jsp?reason=nomatch");
}
} else {
response.sendRedirect("../noPri.jsp?reason=noname");
}
}
}JSP代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link href="css/page.css" rel="stylesheet" type="text/css"></style>
<script language="javascript" src="js/check.js"></script>
</head>
<%
request.setCharacterEncoding("GBK");
String sessionName = (String)session.getAttribute("sessionName");
String paramName = (String)request.getParameter("name");
System.out.println("In index.jsp, param from servlet is " + paramName);
if(!"".equals(sessionName)){
out.println("<b><font color='blue'>" + sessionName + "</font></b> login");
}
if(!"".equals(paramName)){
out.println("<b><font color='blue'>" + paramName + "</font></b> login");
session.setAttribute("sessionName", paramName);
}
%>
<body>
<form action="servlet/Check" method="post" onSubmit="return check();">
<table width="120">
<tr>
<td align="right" >name</td>
<td><input type="text" name="name" id="name"/></td>
</tr>
<tr>
<td align="right">password</td>
<td><input type="password" name="pwd" id="pwd"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>打印出paramName异常~~~求解。在线等。
不行的话,自己在写一个 filter 转为前后台 改变encoding 的filter
response.setCharacterEncoding("gbk");
null login (7 login
页面会出现这样的内容。"用户名"传到JSP变成了"(7"~~~~
把name放到request中
request.setAttribute("name",name);
request.getRequestDispatcher("..//index.jsp").forward(request, response);
你试试
为什么要重定向
response.sendRedirect("../index.jsp?name=" + name); ????
不能用转发吗? 13楼是这个意思吧?对嘛 转发的话,就开启的话会又创建一个request,response对象么.
所以共享不到URL跟过来的的属性 name 对嘛?
LZ 可以这么理解吗?