UserDao.java
package com.sys.dao;
import java.sql.*;
import java.sql.ResultSet;
import com.sys.entity.User;
import com.sys.util.DBConnectionPool;
public class UserDao {
public User login(String username,String password) {
Connection conn = DBConnectionPool.getInstance().getConn();
PreparedStatement ps;
ResultSet rs;
User user = new User();
System.out.println("username"+username);
System.out.println("password"+password);
try {
ps = conn.prepareStatement("select * from login where username=? and password=?");//报空指针的语句
ps.setString(1,username);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next()){
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSchool(rs.getString("school"));
user.setRole(rs.getString("role"));
user.setName(rs.getString("name"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DBConnectionPool.getInstance().releaseConn(conn);
return user;
}
}
LoginServlet.java
package com.sys.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.sys.dao.UserDao;
import com.sys.entity.User;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
//获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
if(password==null||username==null){
System.out.println("Error");
}
//获取UserDao实例
UserDao userDao = new UserDao();
User user = userDao.login(username,password);//报空指针异常的语句
HttpSession session=request.getSession(true);
session.setAttribute("user",user);
// 判断user是否为空
if(user != null){
// 转发到LoginSuccess.jsp页面
// getRequestDispatcher()是请求转发
request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
}else{
// 登录失败
request.getRequestDispatcher("/WEB-INF/pages/Login.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);//报空指针的异常的语句
}
}
在登陆后一段时间,就自动报出上面这个错误,刷新页面也回不到原来的界面了,怎么解决这个问题