登陆验证的问题,麻烦大家指点。
我改用servlet来验证时,老是提示密码错误,但这个密码确实是我的库表中的,怎么不行呢,我开始怀疑我的连数据库的bean不对,但经过多次测试,证明连库的bean是正确的,但为什么老是提示密码错误呢,请朋友们指点,是不是我的代码存在问题代码如下
//////////用来验证登陆信息的servlet//////////
package mypack;
import *;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class login_confirm extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String message=null;
String id=null;
id=req.getParameter("id");
HttpSession session=req.getSession(true);
session.setAttribute("id",String.valueOf(id));//id存入session
String password=null;
password= req.getParameter("password");//获取密码
String kind =null;
kind=req.getParameter("kind");//获得类别
String temp =getPassword(req,res,id,kind);
if( password.equals(temp))///登陆密码与登陆id对应密码的比较
goo(req,res,kind);///若相等,则进行等如后的相关操作
else {
message="用户名或密码有误!";
doError(req,res,message) ;
}
}
public void goo(HttpServletRequest req, HttpServletResponse res,String kind)
throws ServletException,IOException
{
if(kind.equals("student")) {
RequestDispatcher rd = getServletContext().getRequestDispatcher("/student.jsp");
rd.forward(req, res);}
if(kind.equals("teacher")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/teacher.jsp");
rd.forward(req, res);}
if(kind.equals("admin")){
RequestDispatcher rd = getServletContext().getRequestDispatcher("/admin.jsp");
rd.forward(req, res);}
}
public String getPassword(HttpServletRequest req, HttpServletResponse res,
String id,String kind)
throws ServletException, IOException {
sqlBean2 db=new sqlBean2();
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);
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String action = action = req.getParameter("action");
if ("logout".equalsIgnoreCase(action)) {
HttpSession session=req.getSession(true);
session.invalidate();
RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.jsp");
rd.forward(req, res);
} }
}
///////////////连接数据库的javabean ////////////////
package mypack;
import *;
import java.sql.*;
public class sqlBean2{///
public Connection con=null;
public sqlBean2()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String SERVANDDB="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=zibian";
String USER="sa";
String PWD="123";
try
{
Class.forName(CLASSFORNAME);
con = DriverManager.getConnection(SERVANDDB,USER,PWD);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void deleteContact(String sql)throws Exception
{
try
{ Statement stmt=con.createStatement();
stmt.execute(sql);
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public ResultSet executeQuery(String sql)
{
try{
Statement stmt=con.createStatement( );
ResultSet rst=stmt.executeQuery(sql);
return rst;
}
catch(SQLException ex)
{
System.err.println("执行查询有错误:"+ex.getMessage() );
System.out.print("执行查询有错误:"+ex.getMessage()); //输出到客户端
}
return null;
}
}