Issue
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>
Solution
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
Answered By - ControlAltDel
Answer Checked By - Robin (JavaFixing Admin)