2

When I try to run my web app project on Tomcat server I'm getting the following error.

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

I have really no idea what is wrong with my code below. I've read similar questions on StackOverflow but I can't find any answer that I could implement into my project. I appreciate any help.

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>$Title$</title>
  </head>
  <body>
    <h1>Hello</h1>
    <ul>
      <li><a href="/register">Register</a></li>
      <li><a href="/login">Login</a></li>
      <li><a href="/panel">Panel</a> </li>
      <li><a href="/logout">Logout</a> </li>
    </ul>
    <c:forEach items="${posts}" var="post">
      <p>
      <h4><c:out value="${post.title} ${post.author}"/><br /></h4>
        <c:out value="${post.text}"/>
        <a href="/post/${post.id}">Read more</a>
      </p>
    </c:forEach>

  </body>
</html>

HomePage.java

import Database.DBAdminConnector; import Database.DBUserConnector;

import javax.servlet.*; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; import java.sql.*; import java.util.ArrayList; import java.util.List;

@WebServlet(name = "HomePage", urlPatterns = "/") public class HomePage extends HttpServlet {
    Statement statement = null;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DBUserConnector dbConnector = DBUserConnector.INSTANCE;
        Connection connection = dbConnector.getConnection();
        resp.setContentType("text/html");
        try {
            statement = connection.createStatement();
            String getPosts = "SELECT * FROM latest LIMIT 10";
            ResultSet posts = statement.executeQuery(getPosts);

            List<Post> postList = new ArrayList<>();
            while(posts.next()) {
                int id = posts.getInt("id");
                String title = posts.getString("title");
                String author = posts.getString("nickname");
                Date date = posts.getDate("time_created");
                String text = posts.getString("text");
                Post p = new Post(id, title, author, date, text);
                //System.out.println(p);
                postList.add(p);
            }
            req.setAttribute("posts", postList);
            RequestDispatcher view = req.getRequestDispatcher("index.jsp");
            view.forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    } }

web.xml

<web-app version="3.1"
         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_3_1.xsd"
         metadata-complete="false">


    <servlet-mapping>
        <servlet-name>HomePage</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>PostPage</servlet-name>
        <url-pattern>/post/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>RegisterPage</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>LoginPage</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>PanelPage</servlet-name>
        <url-pattern>/panel</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Logout</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
    <servlet-name>BackupRestoreDB</servlet-name>
    <url-pattern>/backup</url-pattern>
    </servlet-mapping>
</web-app>
markspace
  • 10,621
  • 3
  • 25
  • 39
brecho
  • 65
  • 1
  • 1
  • 8
  • What's the URL of the request? how is the app deployed, as ROOT.war? – LMC Mar 30 '22 at 15:58
  • @LMC the URL is 'http://localhost:8080/blog_db-1.0-SNAPSHOT/' and app is deployed on Tomcat server as 'blog_db-1.0-SNAPSHOT.war' – brecho Mar 30 '22 at 16:04

3 Answers3

1

This is a common problem with application servers across the full spectrum. If you are encountering 404s or other issues, the way to debug the issue is to incrementally step backwards and forwards. The first thing you should do is look at your server logs to make sure the server has started right. If it has, you should try creating a index.html file and testing that. And so on and so on. On a failed test, you step back to see what's wrong in the previous steps. On a successful test, you step forward to see if the next thing works

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
1

I faced the same issue, I was on eclipse version 4.25.0 which was using JDK 17. I was using Tomcat 8. The server was starting fine (as shown in Eclipse) but when url was hit, it gave this error. Then I checked the server log.

Checked the server log following this- Where can I view Tomcat log files in Eclipse? Found the following error in server log -

java.lang.ClassNotFoundException: jakarta.servlet.Filter at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1420)

See - How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat

Tomcat 10.x was the first version to be Jakartified, not Tomcat 9.x. So I installed Tomcat 10.x, used that server to run my application, got no error in the server log-- and application ran fine!

Good luck.

0

After creating a new dynamic web project on Eclipse and try to run it, I encountered the same error message. I couldn't find the solution on similar questions of StackOverflow. Projects I have created two years ago using an older version of Eclipse were imported to the newest version of Eclipse I'm using nowadays and they did not present the error message. That gave the hint that the problem was related to the project itself rather than the IDE. After comparing an older project with the new one, I changed the build class path order of the build path (on "Order and Export" tab) moving the src/main/java up to the first place. It worked.

ValGarcia
  • 1
  • 2