error nullpointerexception connection db mysql through webapplication(jsp)

sqserrrh  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(239)

我遇到的问题与接收数字作为输入的表单有关,通过jsp页面(调用javabeans)应该连接到db并打印结果。
当我使用执行连接及其查询的类时,在一个简单的java应用程序中一切正常,当我通过jsp页面执行它时(不修改student.java类),我得到以下错误:
org.apache.jasper.jasperexception:java.lang.nullpointerexception
我正在使用glassfish5、netbeans、jdk8和mysql
我将代码的重要部分整合如下:
结果.jsp

<%@page import="TEST.Student"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <jsp:useBean id="RT" scope="application" class="TEST.Student" />
        <jsp:setProperty name="RT" property="freshman" param="freshman"/>
    </head>
    <body>
        <h1>The student's name is: <% out.print(RT.getStudentByFreshman()); %></h1>
    </body>
</html>

学生.java

package test; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;

    public class Student implements Serializable{
        private int freshman;
        private static final String DBNAME="example30_lp";
        private static final String USERNAME="root";
        private static final String PWD="********";
        private static final String JDBC_URL="jdbc:mysql://localhost:3306/"+DBNAME+"?user="+USERNAME+"&password="+PWD+"&serverTimezone=Europe/Rome";
        private Connection con;

        public Student() {       

        } 

        public void setConnection(){
            try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            this.con= DriverManager.getConnection(JDBC_URL);
            } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (InstantiationException ex) {
                        ex.printStackTrace();
                    } catch (IllegalAccessException ex) {
                        ex.printStackTrace();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }

        }

        public void setFreshman(int freshman) {
            this.freshman= freshman;
        }

        public String getMessTest(){
           return ("The BEAN found (param"+freshman+")");    }

        public String getStudentByFreshman(){
             setConnection();
             String result = null;

             try {
                Statement stat = con.createStatement();
                ResultSet rs = stat.executeQuery(
                        "SELECT * FROM `students` WHERE `freshman` = '" + 
                                freshman+ "'");
                while (rs.next()) {
                    result=rs.getString("name");                
                }

            } catch (SQLException ex) {
                ex.printStackTrace();
            }

             return result;
        }
           /*TEST FOR JavaApplication */   
/*  public static void main(String args[]){

            Student RT = new Student();
            RT.setFreshman(10120);

            System.out.println("Nome "+RT.getStudentByFreshman());

        }*/ }

索引.html

<!DOCTYPE html>
<html>
    <head>
        <title>Search Student</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

    </head>
    <body>
        <div>Search Student from freshman number</div>
        <form action="result.jsp" method="POST">
            <input type="text" name="freshman" value="" size="50" />
            <input type="submit" value="Search"
                               style="font-family: Verdana; font-size: large;
                               font-weight: bold; padding: 8px;">
        </form>

    </body>
</html>

下面是glassfish服务器的stacktrace

[glassfish 5.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=42 _ThreadName=http-listener-1(5)] [timeMillis: 1606757551047] [levelValue: 900] [[
  StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at TEST.Student.getStudentByFreshman(Student.java:54)
    at org.apache.jsp.result_jsp._jspService(result_jsp.java:68)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1540)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
]]

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题