0

I'm a beginner when it comes to Java Servlets. What I'm trying to do is have a html form that has two input fields and a submit button. When the submit button is clicked, the data will go to the servlet. The URL currently appears like this `http://localhost:8080/MyApp/index.html.

Anyways that's what I want to happen but when I execute the servlet on Tomcat the html file is not being rendered. It's as if the servlet executed with no html file.

I want the user to be met with the index.html file when they open the URL.

index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>MyFirstApp</title>
</head>
<body>
    <form action="MyApp" method="GET">
        First Name: <input type="text" name="first_name">
    <br />
        Last Name: <input type="text" name="last_name" />
        <input type="submit" value="Submit" />
    </form>
</body>
</html>

MyApp.java

    package main.com.myfirstapp;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    @WebServlet("/index.html")
    public class MyApp extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException {

        res.setContentType("text/html");

        PrintWriter out = res.getWriter();

        req.getParameter("first_name");
        req.getParameter("last_name");
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1"
>

<servlet>
    <servlet-name>MyApp</servlet-name>
    <servlet-class>main.com.myfirstapp.MyApp</servlet-class>
</servlet>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>                   
TheRealRave
  • 333
  • 1
  • 2
  • 19
  • @BalusC closed but maybe your problem is your servlet listening index.html (i.e. http://localhost:8080/myapp/index.html?params...) but in your url its not contating any index.html – HRgiger Jun 23 '16 at 10:46
  • @BalusC That was a typing mistake. The question has been updated. Please explain to me how this is an exact duplicate. The duplicate question is asking how to generate a HTML response. Mine is asking why my HTML page is not showing when I execute my servlet. – TheRealRave Jun 23 '16 at 10:52
  • Because you're not generating HTML from servlet. The duplicate answers how exactly do to that (with help of JSP as HTML generator). The .html extension in servlet URL is by the way 90's, better remove it (and make sure you're reading up to date and authoritative learning resources and not some random online tutorial full of advertisements (as their primary goal is not tutoring the right way)). – BalusC Jun 23 '16 at 10:54
  • @BalusC The .html extension was my own idea to try get things to work. Are you talking about [tutorialspoint](http://www.tutorialspoint.com/servlets/index.htm)? Could you suggest some learning resources that you know of? – TheRealRave Jun 23 '16 at 11:02
  • Just follow the duplicate link. – BalusC Jun 23 '16 at 11:02

0 Answers0