刚学servlet求帮助
小弟遇到个问题,以下为代码,问题在最后。
String u=req.getParameter("stuName");
String p=req.getParameter("password");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
ct=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=stu","sa","yuanrunli");
sm=ct.createStatement();
rs=sm.executeQuery("select top 1 password from student where stuName='"+u+"'");
//判断是否有该用户名
if(rs.next())
{
String dbpassword=rs.getString(1);
System.out.println(dbpassword);---------------------------------1
System.out.println(p);-------------------------------------2
if(p.equals(dbpassword))
{
//合法用户跳转到登入成功页面
System.out.println(dbpassword);
System.out.println(p);
resp.sendRedirect("login3");
}
}
else
//不合法跳转到登入页面
{
resp.sendRedirect("login1");
}
以上为代码,测试能得到1的dbpassword和p的值,都是admin,可是为什么没进入下面的相等判断呢?
------解决方案--------------------检查空格
------解决方案--------------------用中文登录不成功属于编码问题了。。。
页面跳到servlet后
Java code
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
......
response.setContentType("text/html;charset=gb2312");
response.sendRedirect ( "XXXX.jsp" );
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}