I followed a very basic project creation tutorial here:
https://www.youtube.com/watch?v=eP9oz6ZKUXM
Here's what I did:
1) Install Apache Tomcat Server
Download and install Apache Tomcat: https://downloads.apache.org/tomcat/tomcat-10/v10.0.2/bin/apache-tomcat-10.0.2.exe
I set the Server Shutdown Port as "8005"
- I pointed it to JDK15 (JRE 1.8 was default as that is also on the machine)
2) Add the server to Apache Netbeans IDE
3) Create Maven Web App using Tomcat Server
- Add Servlet and edit index.html
Servlet Code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fourc.group.mavenwebautomation;
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;
/**
*
* @author DagninE
*/
@WebServlet(name = "UnitConverterServlet", urlPatterns =
{
"/MavenWebAutomation/convert"
})
public class UnitConverterServlet extends HttpServlet
{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
float miles = Float.parseFloat(request.getParameter("miles"));
float km = miles * 1.6f;
response.setContentType("text/html;charset=UTF-8");
try ( PrintWriter out = response.getWriter())
{
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Unit Conversion Result</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Conversion Result</h1>");
out.println("<p>" + miles + " miles = " + km + " kilometers </p>");
out.println("</body>");
out.println("</html>");
}
}
}
Index.html Code:
<!DOCTYPE html>
<html>
<head>
<title>Maven Project</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div align="center">
<h1>Unit Converter</h1>
<form action="convert" method="post">
Miles : <input type="number" name="miles" required/>
<input type="submit" value="Convert to km"/>
</form>
</div>
</body>
</html>
6) Run the app
No here is where 2 things happen:
First - Two Chrome tabs open (I cannot understand WHY):
Second - When I attempt to do the conversion, I get an error:
I honestly have zero clue why this is the way it is. In theory, everything should work?