Servlet做一个简单登录,然后为什么跳转是空白?求大神解救
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置request与response的编码
response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 获取表单中属性值
String name=request.getParameter("name");
String password=request.getParameter("password");
if (valid(name, password)) {
request.getRequestDispatcher("../login_success.jsp").forward(request,response);
}else {
System.out.println("密码错误");
}
}
public boolean valid(String username,String password){
boolean isValid = false;
DBConnect db = new DBConnect();
if(db.createConn()){
String sql = "select * from account where name='"+username+"' and password='"+password+"'";
db.query(sql);
if(db.next()){
isValid = true;
}
db.closeRs();
db.closeStm();
db.closeConn();
}
return isValid;
}
package db;
import java.sql.*;
public class DBConnect {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://127.0.0.1:3306/newssys?useUnicode=true&characterEncoding=utf8";
private String username = "root";
private String password = "";
private Connection conn;
private Statement stm;
private ResultSet rs;
//创建连接
public boolean createConn() {
boolean b = false;
try {
Class.forName(driver);// 加载Mysql驱动程序
conn = DriverManager.getConnection(url, username, password);
b = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 获取连接
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//修改
public boolean update(String sql){
boolean b = false;
try {
stm = conn.createStatement();
stm.execute(sql);
b = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//查询
public void query(String sql){
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//判断有无数据
public boolean next(){
boolean b = false;
try {
if(rs.next()){
b = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return b;
}
//获取表字段值
public String getValue(String field) {
String value = null;
try {
if (rs != null) {
value = rs.getString(field);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}
//关闭连接
public void closeConn() {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭statement
public void closeStm() {
try {
if (stm != null) {
stm.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//关闭ResultSet
public void closeRs() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}