激活账号的servlet运行报错500,代码是对的,为什么会报错?

报错

图片说明

servlet代码

package servlets;

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 bean.Check;
import bean.DB;
import bean.Tools;
import java.sql.*;

public class RegAdminServlet extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public RegAdminServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * 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 {

        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 {



        //用以建立数据库连接的必要信息
        String url="jdbc:mysql://localhost:3306/database";
        String user="root";
        String password="123456";



        String adminid = Tools.codeToChinese(request.getParameter("adminid"));
        String adminpass  = request.getParameter("adminpass");
        String adminname = Tools.codeToChinese(request.getParameter("adminname"));
        String admintele = request.getParameter("admintele");

        //省略号表示要输入具体的sql语句
        String sql="select * from administrator adminID='"+adminid+"'";



        //创建各个对象
        Connection conn=null; //建立数据库连接的对象
        Statement stmt=null; //创建用于执行SQL语句的Statement对象
        ResultSet rs=null; //创建结果集对象


        HttpSession session = request.getSession();


        try{
               //建立连接并执行语句
        conn=DriverManager.getConnection(url, user, password);
        stmt=conn.createStatement();
        rs=stmt.executeQuery(sql);



        //其它操作
        if(rs.next())
        {
            Check check = new Check();
            boolean ck=check.check(adminid, adminpass, adminname, admintele);
            if(ck)
            {
                DB db = new DB();
                StringBuffer newsql = new StringBuffer();
                newsql.append("UPDATE administrator SET adminActivated='1' WHERE adminID='"+adminid+"'");

                boolean a = db.executeUpdate(newsql.toString());
                if(a){
                    session.setAttribute("message","激活成功!");
                    response.sendRedirect("regCheckSuccess.jsp");
                }
                else{
                    session.setAttribute("message","激活失败,请重新激活!");
                    response.sendRedirect("regCheckFailure.jsp");
                }
            }
            else
            {
                session.setAttribute("message", "激活失败!用户信息输入错误,请重新输入!");
                response.sendRedirect("regCheckFailure.jsp");
            }
        }
        else
        {
            session.setAttribute("message", "账号不存在,请重新输入!");
            response.sendRedirect("regMsgAdmin.jsp");
        }

        }catch(SQLException se){
            throw new ServletException(se);
        }finally{
        try{rs.close();}catch(SQLException se){}
        try{stmt.close();}catch(SQLException se){}
        try{conn.close();}catch(SQLException se){}
        }



        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<form method=post action=PotalServlet>");
        out.println("<table><tr>");
        out.println("<td>请输入用户名</td>");
        out.println("<td><input type=text name=username /></td>");
        out.println("</tr><tr>");
        out.println("<td>请输入密码</td>");
        out.println("<td><input type=password name=userpass /></td>");
        out.println("</tr><tr>");
        out.println("<td><input type=reset value=重填 /></td>");
        out.println("<td><input type=submit value=登录 /></td>");
        out.println("</tr>");
        out.println("</table>");
        out.println("</form>");
        //out.close()不能调用,否则portal中后面的输出语句将不输出

        /*out
                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");
        out.print("    This is ");
        out.print(this.getClass());
        out.println(", using the POST method");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();*/
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        String classname="com.mysql.jdbc.Driver";
        try{
            Class.forName(classname);
        }catch(ClassNotFoundException ce){
            throw new ServletException("加载数据库驱动失败");
        }

    }

}

Check包代码

package bean;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Check {
    public static boolean check(String adminid,String adminpass,String adminname,String admintele) {
        DB db = new DB();
        Connection conn=db.createConn();

        String sql = "select * from administrator where adminID='"+adminid+"'";
        /*Statement stmt = null;
        ResultSet rs = null;*/
        try{
            Statement stmt=conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);

            while(rs.next())
            {
                String adminID = rs.getString("adminID");
                String adminPass = rs.getString("adminPass");
                String adminName = rs.getString("adminName");
                String adminTele = rs.getString("adminTele");

                if(adminID.equals(adminid)&&adminPass.equals(adminpass)&&adminName.equals(adminname)&&adminTele.equals(admintele))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }catch(SQLException se){
            return false;
        }finally{
        //try{rs.close();}catch(SQLException se){}
        //try{stmt.close();}catch(SQLException se){}
        //try{conn.close();}catch(SQLException se){}
        }

        return false;

    }

}

DB包代码

package bean;

//导入创建应用数据要用到的类
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {
    private String classname="com.mysql.jdbc.Driver";   //数据库驱动类路径
    private String url="jdbc:mysql://localhost:3306/database"; //数据库URL
    private String user="root"; //登录数据库的用户名
    private String pwd="123456";    //登录数据库的密码

    private Connection conn=null;   //申明一个Connection对象
    private Statement stmt=null; //声明一个Statement对象
    private ResultSet rs=null;

    /*通过构造方法加载数据库驱动*/
    public DB(){    //DB类的构造方法
        try{    //必须使用try catch语句捕获加载数据库驱动时可能发生的异常
            Class.forName(classname).newInstance(); //加载数据库驱动
        }catch (Exception e){
            e.printStackTrace();    //输出异常信息
            System.out.println("加载数据库驱动失败!!");
        }       
    }
    /*创建数据库连接*/
    public Connection createConn(){
        try{
            conn=DriverManager.getConnection(url, user, pwd);
        }catch(SQLException e){
            e.printStackTrace();
            System.out.println("获取数据库连接失败!!");
        }
        return conn;
    }

    /*获取Statement对象*/
    public void getStmt(){
        createConn();
        try{
            //调用Connection类实例的createStatement()方法创建一个Statement类对象
            stmt=conn.createStatement();
        }catch(SQLException e){
            e.printStackTrace();
            System.out.println("创建Statement对象失败!!");            
        }
    }

    /*创建对数据库进行操作的增加、删除和修改的executeUpdate()方法*/
    public boolean executeUpdate(String sql){
        boolean mark=false;
        try{
            getStmt();  //创建一个Statement对象
            int iCount=stmt.executeUpdate(sql); //执行操作,并获取所影响的记录数
            if(iCount>0)    //更新数据库成功
                mark=true;
            else            //更新失败
                mark=false;         
        }catch(SQLException e){
            e.printStackTrace();
        }
        return mark;
    }

    /*查询数据库*/
    public ResultSet executeQuery(String sql){

        try{
            getStmt();
            try{
                rs=stmt.executeQuery(sql);
            }catch(Exception e){
                e.printStackTrace();
                System.out.println("查询数据库失败!!");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return rs;
    }
    public void close(){
        try{if(rs!=null)rs.close();}catch(Exception e){}
        try {stmt.close();}catch(Exception e){}
        try {conn.close();}catch(Exception e){}
    }
}

jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'regAdmin.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script language="javascript">
      function isNull(str)
      {
        if (str.length==0)
           return true;
        else
           return false;
      }
      function isValidate(form)
      {
         adminid=form.adminid.value;
         adminpass=form.adminpass.value;
         adminname=form.adminname.value;
         admintele=form.admintele.value;

         if(isNull(adminid))
         {
           alert("请输入账号!");
           return false;
         }
         if(isNull(adminpass))
         {
           alert("请输入密码!");
           return false;
         }
         if(isNull(adminname))
         {
           alert("请输入姓名!");
           return false;
         }
         if(isNull(admintele))
         {
           alert("请输入联系方式!");
           return false;
         }
         return true;
      }


    </script>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  </head>

  <body>
    <form name="form" action="RegAdminServlet" method="post" onSubmit="return isValidate(form)"><br>
    <h1 align="center"> 管理员账号激活</h1><hr><p><br>
    <table width="300" border="0" align="center">
      <tr><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td></tr><tr>
         <td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td>账 号 :</td>
         <td><input type="text" name="adminid" /></td>
      </tr>
      <tr>
         <td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td>密 码 :</td>
         <td><input type="password" name="adminpass"/></td>
      </tr>
      <tr>
         <td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td>姓 名 :</td>
         <td><input type="text" name="adminname"/></td>
      </tr>

      <tr>
         <td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td valign="top"><br></td><td>联 系<br>方 式 :</td>
         <td><input type="text" name="admintele"/></td>
      </tr>

    </table><br>
    <div align="center">
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="激活"/>
         &nbsp;&nbsp;&nbsp;<input type="reset" name="reset" value="清空"/><br><br><br>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="loginAdmin.jsp">【返回】</a>
    </div>
    <p>

    </form>
  </body>
</html>

数据库

图片说明

2个回答

都报空指针异常了。。。。。

Emiry
半夏莲城 回复可以简单点: OK了,Thanks♪(・ω・)ノ
7 个月之前 回复
weixin_38860401
可以简单点 回复半夏莲城: 把你的sql语句select * from administrator adminID='"+adminid+"'"去数据库查一下,看有没有数据,没有的话rs这个对象就是空的
7 个月之前 回复
Emiry
半夏莲城 回复可以简单点: 我试过用debug,然而不知道为什么跳出来一个class.class的文件
7 个月之前 回复
Emiry
半夏莲城 回复可以简单点: 136行的代码是try{rs.close();}catch(SQLException se){}
7 个月之前 回复
weixin_38860401
可以简单点 回复半夏莲城: 看你的异常信息,是RegAdminServlet这个类的136行代码报的错误,你可以在这里打断点看跟踪一下
7 个月之前 回复
Emiry
半夏莲城 请问要怎么解决啊?我找不出来哪里错了_(:з」∠)_
7 个月之前 回复

servlet类在配置文件中配置了吗? 之后断点能够进入代码吗

Emiry
半夏莲城 配置??你指的是什么??打断点之后不知道为什么跳出来一个class.class的文件
7 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!