I'm trying to make a web application using Java Servlets, Tomcat and HTML on Eclipse. My problem is that after I created my Dynamic Web Project, along with my web.xml and my index.html files, the index.html page doesn't show anything.
Servlet:
@WebServlet("/index.html")
public class AppServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String HTML_START = "<html><body>";
private static final String HTML_END = "</body></html>";
public AppServlet() {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" type="text/css" href="styling.css">
<title>Test page</title>
</head>
<body>
<h1 style="text-align:center;">Example text</h1>
<div align="center">
<textarea rows="12" cols="120" style="border-radius: 5px"></textarea>
</div>
</body>
</html>
The html page shows up just fine when I open it directly (when I double click on the file in a directory), but when I start up the server from Eclipse and it redirects me to localhost:8080/MyProject/index.html, nothing shows up.
Each URL can be processed by a single component: Either your URL is resolved to the file index.html or to a servlet. In your case servlet has higher priority and this servlet (not the file index.html) creates the result. Your servlet doesn't create any contents in the doGet method. SO it is naturally that the response is empty.
If you want that this URL is resolved to the file index.html, then use some other URL mapping in the servlet, like @WebServlet("/bla"). Then when you call .../MyProject/index.html, you will get the contents of the file index.html.