脚本中的JSP-Calling函数

时间:2018-04-23 17:21:13

标签: java mysql jsp-tags

所以,我试图通过检查用户是否已注册来验证用户。我正在使用MySQL来达到这个目的 我的java文件 Register.java 位于包 com.aayush.jsp 中。这是文件的片段

Register.java

package com.aayush.jsp;
import java.sql.*;

public class Register {
    public static int check_if_already_exists(String id) {
        ConnectToMySQL connection = new ConnectToMySQL();
        connection.connect();
        try {
            ResultSet rs = connection.stmnt.executeQuery("SELECT id FROM login");
            while(rs.next()) {
                if(id==rs.getString("coumn_name")) {
                    return -1;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public static void main(String[] args) {
        System.out.println(check_if_already_exists("101"));
    }
}

这是 ConnectToMySQL.java 文件:

package com.aayush.jsp;
import java.sql.*;
public class ConnectToMySQL {
    Statement stmnt = null;
    public void connect() {
        try {
            Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/un","root","hellomoto");
            stmnt = myConn.createStatement();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ConnectToMySQL conn = new ConnectToMySQL();
        conn.connect();
    }
}

如您所见,此文件使用类 ConnectToMySQL ,该类在同一个包中定义。当作为Java应用程序运行时,此代码运行正常。

当我从JSP文件中调用方法 check_if_already_exists()时,问题就出现了,如下所示:

<html>
<body>
<% int checkMail = com.aayush.jsp.Register.check_if_already_exists("101");%>
</body>
</html>

Apache Tomcat出现此错误:

type Exception report

message An exception occurred processing JSP page /RegisterOrLogin.jsp at line 9

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /RegisterOrLogin.jsp at line 4

1: 
2: <html>
3: <body>
4:  <% int checkMail = com.aayush.jsp.Register.check_if_already_exists("101");%>



Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:579)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
java.lang.NullPointerException
    com.aayush.jsp.Register.check_if_already_exists(Register.java:9)
    org.apache.jsp.RegisterOrLogin_jsp._jspService(RegisterOrLogin_jsp.java:120)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

0 个答案:

没有答案