为什么一直报错空指针啊,有没有人可以帮我解决一下,花了十几个小时找不出错误,我寻思代码真的没错呀,谢谢
用servlet先访问,然后跳转,一直报404的错误
package servlets;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentDao extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
Connection conn=null;
String url="jdbc:mysql://localhost:3306/school?useSSL = false&serverTimezone = CST&characterEncoding=UTF-8";
String user="root";
String password="102245";
ArrayList<String> STUNO=new ArrayList<>();
ArrayList<String> STUNAME=new ArrayList<>();
try {//连接数据库驱动
Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(Exception e){
System.out.println(e);
}
try {
conn=DriverManager.getConnection(url,user,password);
//运行SQL语句
String sql="SELECT STUNO,STUNAME from t_student";
Statement stat= conn.createStatement();
ResultSet rs= stat.executeQuery(sql);
while (rs.next()){
STUNO.add(rs.getString("STUNO"));
STUNAME.add(rs.getString("STUNAME"));
}
rs.close();
stat.close();
}catch (SQLException e){
e.printStackTrace();
}finally {
try {//关闭连接
if (conn!=null){
conn.close();
conn=null;
}
}catch (Exception ex){
}
}
request.setAttribute("STUNO",STUNO);
request.setAttribute("STUNAME",STUNAME);
String msg="lz";
request.setAttribute("msg",msg);
ServletContext application=this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher("/index.jsp");
rd.forward(request,response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>studentDao</servlet-name>
<servlet-class>servlets.StudentDao</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>studentDao</servlet-name>
<url-pattern>/StudentDao</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%--
Created by IntelliJ IDEA.
User: 86157
Date: 2023/10/19
Time: 8:33
To change this template use File | Settings | File Templates.
--%>
import java.sql.*;
<%@ page language="java" pageEncoding="gb2312" %>
<%@ page import="java.util.ArrayList" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<table border="2">
<tr>
<td>学号</td>
<td>姓名</td>
</tr>
<%
String msg = (String) request.getAttribute("msg");
ArrayList<String> STUNO = (ArrayList) request.getAttribute("STUNO");
ArrayList<String> STUNAME = (ArrayList) request.getAttribute("StuNAME");
System.out.println(msg);
for(int i= 0 ; i< STUNO.size();i++){
String Stuno = (String)STUNO.get(i);
String Stuname = (String)STUNAME.get(i);
%>
<tr>
<td><%=Stuno%></td>
<td><%=Stuname%></td>
</tr>
<% } %>
</table>
</body>
</html>