Login.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<link rel="stylesheet" type="text/css" href="css/LoginCss-1.css">
</head>
<body>
<img src="images/default_04.png" width="1031" height="136">
<form name="form1" method="post" action="servlet/LoginServlet";">
<h1>登录:
</h1>
<table width="304" height="148" border="1" align="center">
<tr>
<td width="101" align="right">账号:</td>
<td width="187" align="left"><label>
<input name="txtUserName" type="text" id="txtUserName" size="8" maxlength="8">
</label></td>
</tr>
<tr>
<td height="32" align="right">密码:</td>
<td align="left"><label>
<input name="txtPassword" type="password" id="txtPassword" size="6" maxlength="6">
</label></td>
</tr>
<tr>
<td height="38" colspan="2" align="center"><label>
<select name="list" id="list">
<option value="0">教师登录</option>
<option value="1" selected>学生登录</option>
</select>
</label></td>
</tr>
<tr>
<td colspan="2" align="right"><label>
<input type="submit" name="button3" id="button3" value="登录">
</label></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>LoginServlet.servlet:
package stusys_servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import stusys_util.LoginCheck;public class LoginServlet extends HttpServlet
{
public LoginServlet()
{
super();
}
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("gb2312");
//接受客户端传来的数据
String username = request.getParameter("txtUserName");
String password = request.getParameter("txtPassword");
int logintype = request.getIntHeader("list");
//处理数据
String listType;
if(logintype == 0)listType = "Teacher";
else listType = "Student";
boolean flag = LoginCheck.Cheak(username,password,listType);
//响应客户端
String path = (flag==true)?listType+"Operation.jsp?username="+username:"../error.jsp";
response.sendRedirect(path);
}
public void init() throws ServletException
{
}
}
LoginCheck.class
package Login_util;import java.sql.ResultSet;
import java.sql.SQLException;
import Login_dbutil.DBConn;public class LoginCheck
{
public static boolean Cheak(final String username,final String password)
{
//创建数据库连接类对象DBConn
DBConn dbconn = new DBConn();
//动态创建一个SQL验证登录的语句,用户名,密码,状态。
String strSQL = "select count(*) from users where username='"+username+"'and password='"+password+"'and status=0" ;
//向数据库发送SQL语句,返回的ResultSet对象
ResultSet rs = dbconn.execQuery(strSQL);
try {
rs.next();
int res = rs.getInt(1);
return res>0?true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
finally
{
dbconn.closeConn();
}
}}
DBConn:
package stusys_dbutil;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DBConn { //三属性四方法
//三个属性为JDBC中的三个核心接口声明
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
//方法1:一个私有的获取数据库连接的方法
private void getConnection(){
try {
//步骤1:加载连接驱动
Class.forName(Config.CLASS_NAME);
//步骤2:设置连接参数
String url = Config.DATABASE_URL+"://"+Config.IPADDRESS+":"+Config.SERVER_PORT+"/"+Config.DATABASE_NAME;
String user = Config.USERNAME;
String pwd = Config.PASSWORD;
conn = DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//方法2:专门执行增删改语句的方法
public int execOther(final String strSQL){
//1:获取连接
getConnection();
try {
//2:创建一个statement对象
stmt = conn.createStatement();
//在控制台输出即将执行的SQL语句,便于程序测试
System.out.println("SQL> "+strSQL);
//3:发送传入的SQL语句
int res = stmt.executeUpdate(strSQL);
//4:将结果进行返回
return res;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
} //方法3:专门执行查询的方法
public ResultSet execQuery(final String strSQL){
//1:获取连接
getConnection();
try {
//2:创建statement对象
stmt = conn.createStatement();
System.out.println("SQL> "+strSQL);
//3:发送SQL语句
rs = stmt.executeQuery(strSQL);
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
错误为:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
stusys_dbutil.DBConn.execQuery(DBConn.java:62)
stusys_util.LoginCheck.Cheak(LoginCheck.java:20)
stusys_servlet.LoginServlet.doPost(LoginServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.5 logs.
--------------------------------------------------------------------------------Apache Tomcat/7.0.5
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<link rel="stylesheet" type="text/css" href="css/LoginCss-1.css">
</head>
<body>
<img src="images/default_04.png" width="1031" height="136">
<form name="form1" method="post" action="servlet/LoginServlet";">
<h1>登录:
</h1>
<table width="304" height="148" border="1" align="center">
<tr>
<td width="101" align="right">账号:</td>
<td width="187" align="left"><label>
<input name="txtUserName" type="text" id="txtUserName" size="8" maxlength="8">
</label></td>
</tr>
<tr>
<td height="32" align="right">密码:</td>
<td align="left"><label>
<input name="txtPassword" type="password" id="txtPassword" size="6" maxlength="6">
</label></td>
</tr>
<tr>
<td height="38" colspan="2" align="center"><label>
<select name="list" id="list">
<option value="0">教师登录</option>
<option value="1" selected>学生登录</option>
</select>
</label></td>
</tr>
<tr>
<td colspan="2" align="right"><label>
<input type="submit" name="button3" id="button3" value="登录">
</label></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>LoginServlet.servlet:
package stusys_servlet;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import stusys_util.LoginCheck;public class LoginServlet extends HttpServlet
{
public LoginServlet()
{
super();
}
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
request.setCharacterEncoding("gb2312");
//接受客户端传来的数据
String username = request.getParameter("txtUserName");
String password = request.getParameter("txtPassword");
int logintype = request.getIntHeader("list");
//处理数据
String listType;
if(logintype == 0)listType = "Teacher";
else listType = "Student";
boolean flag = LoginCheck.Cheak(username,password,listType);
//响应客户端
String path = (flag==true)?listType+"Operation.jsp?username="+username:"../error.jsp";
response.sendRedirect(path);
}
public void init() throws ServletException
{
}
}
LoginCheck.class
package Login_util;import java.sql.ResultSet;
import java.sql.SQLException;
import Login_dbutil.DBConn;public class LoginCheck
{
public static boolean Cheak(final String username,final String password)
{
//创建数据库连接类对象DBConn
DBConn dbconn = new DBConn();
//动态创建一个SQL验证登录的语句,用户名,密码,状态。
String strSQL = "select count(*) from users where username='"+username+"'and password='"+password+"'and status=0" ;
//向数据库发送SQL语句,返回的ResultSet对象
ResultSet rs = dbconn.execQuery(strSQL);
try {
rs.next();
int res = rs.getInt(1);
return res>0?true:false;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
finally
{
dbconn.closeConn();
}
}}
DBConn:
package stusys_dbutil;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DBConn { //三属性四方法
//三个属性为JDBC中的三个核心接口声明
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
//方法1:一个私有的获取数据库连接的方法
private void getConnection(){
try {
//步骤1:加载连接驱动
Class.forName(Config.CLASS_NAME);
//步骤2:设置连接参数
String url = Config.DATABASE_URL+"://"+Config.IPADDRESS+":"+Config.SERVER_PORT+"/"+Config.DATABASE_NAME;
String user = Config.USERNAME;
String pwd = Config.PASSWORD;
conn = DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//方法2:专门执行增删改语句的方法
public int execOther(final String strSQL){
//1:获取连接
getConnection();
try {
//2:创建一个statement对象
stmt = conn.createStatement();
//在控制台输出即将执行的SQL语句,便于程序测试
System.out.println("SQL> "+strSQL);
//3:发送传入的SQL语句
int res = stmt.executeUpdate(strSQL);
//4:将结果进行返回
return res;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
} //方法3:专门执行查询的方法
public ResultSet execQuery(final String strSQL){
//1:获取连接
getConnection();
try {
//2:创建statement对象
stmt = conn.createStatement();
System.out.println("SQL> "+strSQL);
//3:发送SQL语句
rs = stmt.executeQuery(strSQL);
return rs;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
错误为:HTTP Status 500 - --------------------------------------------------------------------------------type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
stusys_dbutil.DBConn.execQuery(DBConn.java:62)
stusys_util.LoginCheck.Cheak(LoginCheck.java:20)
stusys_servlet.LoginServlet.doPost(LoginServlet.java:40)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.5 logs.
--------------------------------------------------------------------------------Apache Tomcat/7.0.5
自己看一下那里写错了
stusys_dbutil.DBConn.execQuery(DBConn.java:62)空指针自己断点DEBUG 找一下
1.有没有数据库驱动包。
2.连接语句错误。
3.是否取connection对象