这是我自写的代码,与数据库连接后,登录时总提示“用户名、密码错误!”,不知道是哪的错误package stuSystem.stu08.control;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.commons.dbutils.handlers.BeanListHandler;
import stuSystem.stu08.pojo.Student;
import stuSystem.stu08.utils.DBOperator;
import stuSystem.stu08.utils.StringTool;public class studentLoginServlet extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//request.setCharacterEncoding("UTF-8");
String log = request.getParameter("log");
if("out".equals(log)) {
this.logout(request, response);
} else {
this.login(request, response);
}
} public void logout(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("tudent.login.nickname");
session.removeAttribute("tudent.login.isValid");
} public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sname = request.getParameter("sname");
String password = request.getParameter("password");
String utype = request.getParameter("utype");
if(StringTool.isNull(utype)) {
utype = "0";
} String message = "";
boolean isValid = true;
if(!StringTool.isNull(sname)) {
if(!StringTool.isNull(password)) {
//用户名、密码均不为空,则连接数据库,到数据库表中查找比对
String selectStr = "select sname, password, utype from student where sname = ? and password = ? and utype = ?";
Object[] params = {sname, password, utype};
DBOperator dbOp = new DBOperator();
List<Student> student = (List<Student>)dbOp.query(selectStr,params,new BeanListHandler(Student.class));
if(student != null) {
if(student.size() > 0) {
} else {
message = message + "用户名、密码错误!或者身份错误!<br>";
isValid = false;
}
} else {
message = message + "用户名、密码错误!或者身份错误!<br>";
isValid = false;
}
} else {
message = message + "密码为空!<br>";
isValid = false;
}
} else {
message = message + "用户名为空!";
isValid = false;
}
if(isValid) {
HttpSession session = request.getSession();
session.setAttribute("student.login.sname",sname);
session.setAttribute("student.login.identity", utype);
}
request.setAttribute("student.login.message", message);
request.setAttribute("student.login.isValid", isValid);
request.getRequestDispatcher("/student/studentLoginResult.jsp").forward(request, response);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.commons.dbutils.handlers.BeanListHandler;
import stuSystem.stu08.pojo.Student;
import stuSystem.stu08.utils.DBOperator;
import stuSystem.stu08.utils.StringTool;public class studentLoginServlet extends HttpServlet { /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//request.setCharacterEncoding("UTF-8");
String log = request.getParameter("log");
if("out".equals(log)) {
this.logout(request, response);
} else {
this.login(request, response);
}
} public void logout(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("tudent.login.nickname");
session.removeAttribute("tudent.login.isValid");
} public void login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sname = request.getParameter("sname");
String password = request.getParameter("password");
String utype = request.getParameter("utype");
if(StringTool.isNull(utype)) {
utype = "0";
} String message = "";
boolean isValid = true;
if(!StringTool.isNull(sname)) {
if(!StringTool.isNull(password)) {
//用户名、密码均不为空,则连接数据库,到数据库表中查找比对
String selectStr = "select sname, password, utype from student where sname = ? and password = ? and utype = ?";
Object[] params = {sname, password, utype};
DBOperator dbOp = new DBOperator();
List<Student> student = (List<Student>)dbOp.query(selectStr,params,new BeanListHandler(Student.class));
if(student != null) {
if(student.size() > 0) {
} else {
message = message + "用户名、密码错误!或者身份错误!<br>";
isValid = false;
}
} else {
message = message + "用户名、密码错误!或者身份错误!<br>";
isValid = false;
}
} else {
message = message + "密码为空!<br>";
isValid = false;
}
} else {
message = message + "用户名为空!";
isValid = false;
}
if(isValid) {
HttpSession session = request.getSession();
session.setAttribute("student.login.sname",sname);
session.setAttribute("student.login.identity", utype);
}
request.setAttribute("student.login.message", message);
request.setAttribute("student.login.isValid", isValid);
request.getRequestDispatcher("/student/studentLoginResult.jsp").forward(request, response);
}
}
2,打开你的show_sql功能,看看执行的sql语句是否正确或者改成
String selectStr = "select sname, password, utype from student where sname = " +sname+ " and password = '"+password +"' and utype =" + utype ;打印出sql语句直接去数据库查一下看看