package com;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class LoginAction extends HttpServlet {
Connection conn = null;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String username = req.getParameter("username");
String password = req.getParameter("password");
String role = req.getParameter("role");
boolean isValid = false;
// 比较:校验用户信息的合法性(db)
isValid = checkUserValid(username, password, role);
RequestDispatcher rd = null;
if (isValid) {
rd = req.getRequestDispatcher("successful.html");
} else {
rd = req.getRequestDispatcher("login.html");
}
}
private boolean checkUserValid(String username, String password, String role) {
// TODO Auto-generated method stub
boolean isLogin = false;
String sql = "select * from user where username=? and password=? and role=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.setString(3, role);
pstmt.executeQuery();
if (rs.next()) {
isLogin = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
return isLogin;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/tarena";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
System.out.println(conn);
System.out.println("Connected!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
提示信息:
com.mysql.jdbc.Connection@1368c5d
Connected!
java.lang.NullPointerException
at com.LoginAction.checkUserValid(LoginAction.java:48)
at com.LoginAction.doPost(LoginAction.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
不知道是什么问题