0

The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Hi, I know this type of question has been answered already but I have tried everything but still the problem persist. One thing is as soon as I enter the servlet name manually then it will work as supposed to but when I run it on the server it shows that error and I have to manually add "/TestServlet" to make it work.

Here are my file, kindly have a look.

This is my context.xml file I am not using web.xml for servlet mapping I am doing that by using @WebServlet annotation

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource name="jdbc/web_student_tracker"
        auth="Container" type="javax.sql.DataSource"
        maxActive="20" maxidle="5" maxWait="10000"
        username="webstudent" password="webstudent"
        driverClassName="com.mysql.cj.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/web_student_tracker?useSSL=false&amp;serverTimezone=UTC"/>

</Context>

And here is my TestServlet.java file.

package com.luv2code.web.jdbc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
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;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    //Define dataSource/connection pool for Resource Injection
    @Resource(name="jdbc/web_student_tracker")
    private DataSource dataSource;
    
    
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //step 1 : Set up the printWriter
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        
        //step 2 get a connection to the database
        try {
            Statement myStmt=null;
            ResultSet myRs = null;
        
            Connection myConn = dataSource.getConnection();
            //step 3 create  a sql query
            
            String sql="select * from student";
            myStmt = myConn.createStatement();
            //step 4 execute the query
            myRs = myStmt.executeQuery(sql); 
            
            //step 5 process the result set
            while(myRs.next())
            {
                String email = myRs.getString("email");
                out.println(email);
            }
        
        }

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

}

It is already inside a package. My code is fairly simple and it works only when I manually add the given URL but I want it to run as soon as I click on run on server.

Kindly help me in knowing what I am missing here.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

0 Answers0