error nullpointerexception connection db mysql through webapplication(jsp)

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

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

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

学生.java

  1. 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;
  2. public class Student implements Serializable{
  3. private int freshman;
  4. private static final String DBNAME="example30_lp";
  5. private static final String USERNAME="root";
  6. private static final String PWD="********";
  7. private static final String JDBC_URL="jdbc:mysql://localhost:3306/"+DBNAME+"?user="+USERNAME+"&password="+PWD+"&serverTimezone=Europe/Rome";
  8. private Connection con;
  9. public Student() {
  10. }
  11. public void setConnection(){
  12. try {
  13. Class.forName("com.mysql.jdbc.Driver").newInstance();
  14. this.con= DriverManager.getConnection(JDBC_URL);
  15. } catch (ClassNotFoundException ex) {
  16. ex.printStackTrace();
  17. } catch (InstantiationException ex) {
  18. ex.printStackTrace();
  19. } catch (IllegalAccessException ex) {
  20. ex.printStackTrace();
  21. } catch (SQLException ex) {
  22. ex.printStackTrace();
  23. }
  24. }
  25. public void setFreshman(int freshman) {
  26. this.freshman= freshman;
  27. }
  28. public String getMessTest(){
  29. return ("The BEAN found (param"+freshman+")"); }
  30. public String getStudentByFreshman(){
  31. setConnection();
  32. String result = null;
  33. try {
  34. Statement stat = con.createStatement();
  35. ResultSet rs = stat.executeQuery(
  36. "SELECT * FROM `students` WHERE `freshman` = '" +
  37. freshman+ "'");
  38. while (rs.next()) {
  39. result=rs.getString("name");
  40. }
  41. } catch (SQLException ex) {
  42. ex.printStackTrace();
  43. }
  44. return result;
  45. }
  46. /*TEST FOR JavaApplication */
  47. /* public static void main(String args[]){
  48. Student RT = new Student();
  49. RT.setFreshman(10120);
  50. System.out.println("Nome "+RT.getStudentByFreshman());
  51. }*/ }

索引.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Search Student</title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. </head>
  8. <body>
  9. <div>Search Student from freshman number</div>
  10. <form action="result.jsp" method="POST">
  11. <input type="text" name="freshman" value="" size="50" />
  12. <input type="submit" value="Search"
  13. style="font-family: Verdana; font-size: large;
  14. font-weight: bold; padding: 8px;">
  15. </form>
  16. </body>
  17. </html>

下面是glassfish服务器的stacktrace

  1. [glassfish 5.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=42 _ThreadName=http-listener-1(5)] [timeMillis: 1606757551047] [levelValue: 900] [[
  2. StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
  3. java.lang.NullPointerException
  4. at TEST.Student.getStudentByFreshman(Student.java:54)
  5. at org.apache.jsp.result_jsp._jspService(result_jsp.java:68)
  6. at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
  7. at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
  8. at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
  9. at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
  10. at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
  11. at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
  12. at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1540)
  13. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
  14. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:119)
  15. at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:611)
  16. at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:550)
  17. at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
  18. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:114)
  19. at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
  20. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:199)
  21. at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463)
  22. at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168)
  23. at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
  24. at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
  25. at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242)
  26. at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
  27. at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
  28. at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
  29. at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
  30. at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
  31. at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
  32. at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
  33. at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
  34. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
  35. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
  36. at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
  37. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
  38. at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
  39. at java.lang.Thread.run(Thread.java:748)
  40. ]]

暂无答案!

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

相关问题