Apache TomCat 10.1 在运行这个小项目时返回 404

问题描述 投票:0回答:0

Servlet

package com.crud.app;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import jakarta.annotation.Resource;

/**
 * Servlet implementation class ServletUtil
 */
@WebServlet("/ServletUtil")
public class ServletUtil extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Resource(name="jdbc/web_student_tracker")//db name
    public StudentDbUtil studentDB;
    private DataSource datasource;//connection pool
         
    public ServletUtil() {
        super();
        // TODO Auto-generated constructor stub
    }

    @Override
    public void init() throws ServletException {//will be intialised when the server is called
        super.init();
        try {
            studentDB=new StudentDbUtil(datasource);//passing the connection pool
        }
        catch(Exception es) {//if db has an error
            es.printStackTrace();
        }
        
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        listStudent(request,response);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

    private void listStudent(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        // TODO Auto-generated method stub
        List<StudentModel> studentData=studentDB.getStudent();//get student data
        request.setAttribute("STUDENT_DATA", studentData);//setting an attribute so that can be iterated
        RequestDispatcher dispatcher=request.getRequestDispatcher("/listStudent.jsp");//dispatching to jsp
        dispatcher.forward(request, response);
    }



}

Web.xml

<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">
         
  <display-name>Student Tracker App</display-name>

  <servlet>
    <servlet-name>ServletUtil</servlet-name>
    <servlet-class>com.crud.app.ServletUtil</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletUtil</servlet-name>
    <url-pattern>/ServletUtil</url-pattern>
  </servlet-mapping>

  <error-page>
    <error-code>404</error-code>
    <location>/error.jsp</location>
  </error-page>

  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>jdbc/web_student_tracker</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

数据库实用程序

package com.crud.app;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

public class StudentDbUtil {
public DataSource datasource;

public StudentDbUtil(DataSource thedatasource) {
    datasource=thedatasource;
}
public List<StudentModel>getStudent() throws SQLException{
    
    List<StudentModel> newList=new ArrayList<>();
    Connection mycnt=null;
    Statement mystatement=null;
    ResultSet result=null;
    try {
        mycnt=datasource.getConnection();
        String sqlQuery="select * from student order by last_name";
        mystatement=mycnt.createStatement();
        result=mystatement.executeQuery(sqlQuery);
        
        while(result.next()) {
            //retrieve dsta
            int id=result.getInt("id");
            String firstName=result.getString("firstName");
            String lastName=result.getString("lastName");
            String email=result.getString("email");
            //create an object
            StudentModel studentDataObj=new StudentModel(id,firstName,lastName,email);
        System.out.println(studentDataObj + "obj");
        newList.add(studentDataObj);
        }
        return newList;

    }
    finally {
        close(mycnt,mystatement,result);
    }
    
}
private void close(Connection mycnt, Statement mystatement, ResultSet result) {
//to prevent memory leak
    try {
        if(mycnt!=null) {
            mycnt.close();
        }
        if(mystatement!=null) {
            mystatement.close();
        }
        if(result!=null) {
            mystatement.close();
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
}
}

模特班

package com.crud.app;

public class StudentModel {
public int id;

public String firstName;
public String lastName;
public String email;
public StudentModel(String firstName, String lastName, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}

public StudentModel(int id, String firstName, String lastName, String email) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}


}

在服务器上运行此项目时,它在浏览器上返回 404 并带有描述原始服务器未找到目标资源的当前表示或不愿意透露存在的表示。我仍然无法理解这背后的原因。任何帮助都会不胜感激。 从这张图片可以分析路径和文件结构。

java eclipse tomcat jdbc
© www.soinside.com 2019 - 2024. All rights reserved.