package com.course;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class login_confirm extends HttpServlet{
//响应Post请求
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String message=null;
String id=null;
//接收用户的登录名
id=req.getParameter("id");
//创建session对象
HttpSession session=req.getSession(true);
//将用户登录名存入session中
session.setAttribute("id",String.valueOf(id));
String password=null;
//接收用户登录的密码
password= req.getParameter("password");
String kind =null;
//接收用户级别
kind=req.getParameter("kind");
//调用getPassword方法,获取数据库中查询出来的密码
String temp =getPassword(req,res,id,kind);
//对比查询出的密码和用户输入的密码是否匹配
if(password.equals(temp))
//密码输入正确,调用goo方法
goo(req,res,kind);
else {
//密码输入错误
message="用户名或密码有误!";
doError(req,res,message) ;
}
}
//根据用户的级别,分别转向不同的页面
……
//根据用户的级别和输入的用户名,查询对应的密码
public String getPassword(HttpServletRequest req, HttpServletResponse res,
String id,String kind)
throws ServletException, IOException {
//声明数据库连接类sqlBean的实例
sqlBean db= new sqlBean();
String pw="";
String sql="select password from "+kind+" where id='"+id+"'";
try{
//进行数据库查询操作
ResultSet rs=db.executeQuery(sql);
if(rs.next() ){
pw= rs.getString("password");
}
}
catch(Exception e)
{ System.out.print(e.toString());}
return pw;
}
//处理错误页面
public void doError(HttpServletRequest req,
HttpServletResponse res,
String str)
throws ServletException, IOException {
req.setAttribute("problem", str);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/errorpage.jsp");
rd.forward(req, res);
}
……
}
我想问题应该出在.equals()语句上,因为若把报错语句改成
message=password+","+temp+"用户名或密码有误!";
千巷猫影
慕田峪4524236
相关分类