1

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>

DB UTILITY

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();
    }
}
}

Model Class

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;
}


}

enter image description here While running this project on server it is returning 404 on browser with a Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.I still cannot understand what is behind this.Any help would be much appreciated. From this image the path and file structure can be analysed.

nitind
  • 19,089
  • 4
  • 34
  • 43
Agnum
  • 199
  • 1
  • 1
  • 8
  • You don't have an index.html or index.jsp or welcome file specified in your web.xml. – Cheng Thao Feb 28 '23 at 13:15
  • Tomcat 10 provides Servlet API 5. If you want to use 4.0, you must go back to Tomcat 9.x. Do not put an older version of the Servlet API into your own WEB-INF/lib folder to get around this. Tomcat already provides you with its version, and that's the version of the Interfaces and base Classes it expects to be able to use to call your Servlets. Also, you have not show the 404 error at all, not what was requested, nor the server output from it. – nitind Feb 28 '23 at 16:40
  • Does this answer your question? [Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"](https://stackoverflow.com/questions/11731377/servlet-returns-http-status-404-the-requested-resource-servlet-is-not-availa) – Olaf Kock Feb 28 '23 at 18:46

0 Answers0