0

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

enter image description here

  • I pointed it to JDK15 (JRE 1.8 was default as that is also on the machine)

enter image description here

2) Add the server to Apache Netbeans IDE

enter image description here enter image description here

3) Create Maven Web App using Tomcat Server

enter image description here enter image description here

  1. Add Servlet and edit index.html

enter image description here

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):

enter image description here

Second - When I attempt to do the conversion, I get an error: enter image description here enter image description here

I honestly have zero clue why this is the way it is. In theory, everything should work?

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Eitel Dagnin
  • 959
  • 4
  • 24
  • 61
  • 1
    Probably you are using `javax.servlet` instead of `jakarta.servlet`: that's the main change between Tomcat 9 and Tomcat 10. – Piotr P. Karwasz Mar 09 '21 at 11:18
  • 1
    Please, add the source code as text, not images and include a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): out of your very long question composed mostly by images, only the servlet code (with import statements), HTML file and server version are relevant to the issue. – Piotr P. Karwasz Mar 09 '21 at 11:21
  • @PiotrP.Karwasz Alright, thank you. Just give me a moment to update the question please. I will check the link you provided now. Thank you for your assistance. – Eitel Dagnin Mar 09 '21 at 11:25
  • @PiotrP.Karwasz Thank you for the help on this. The link you provided (along with your comment on the import) is what the issue was. I don't believe I had the correct context in my search criteria when I Googled. Also, being that the tutorial was only done in December, I did not think something would be broken in the basic setup of it. Nevertheless, thank you again! :) – Eitel Dagnin Mar 09 '21 at 11:42
  • @PiotrP.Karwasz Do you perhaps know why 2 Chrome tabs open when I run the code? – Eitel Dagnin Mar 09 '21 at 11:43

0 Answers0