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>