-1

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.

  • Nothing as in the page is there but blank in the browser, no content is returned at all, or you only get the headers? – nitind Jan 27 '20 at 20:18
  • The page is there but it's blank, and when I view the source code, there is nothing there either. – Bruno Correia Jan 28 '20 at 04:25

2 Answers2

0

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.

nitind
  • 19,089
  • 4
  • 34
  • 43
mentallurg
  • 4,967
  • 5
  • 28
  • 36
0

You grab that PrintWriter but you don't do anything with it. With the first line of your snippet, you've told the Server that you are going to manage /index.html with code, and therefore it shouldn't go get it from the static content. Then your code produces nothing, so you are correctly seeing what your code produces.

Try adding this after PrintWriter out = response.getWriter();

out.println(HTML_START);
out.println("Hello World!");
out.println(HTML_END);
Zag
  • 638
  • 4
  • 8
  • Yeah, I had some stuff there before, but I really just want to put my html code in the index.html file and not in the PrintWriter – Bruno Correia Jan 28 '20 at 04:26