user4324611
user4324611

Reputation:

Page wont load, Apache Tomcat server error

I'm trying to take in a group of numbers from a webpage in the form of a lotto simulator. I run the server, open the page through the local host in the browser, input the numbers and submit it but then nothing displays on the next page, not even a 404 error. Could be a mapping issue but I'm lost. Any ideas?

Lotto.class

 import java.io.*;   
 import java.util.Arrays;
 import java.util.Comparator;

 import javax.servlet.*;
 import javax.servlet.http.*;

 public class Lotto extends HttpServlet {

 public void doGet(HttpServletRequest servletRequest, HttpServletResponse   
 servletResponse)
                           throws ServletException, IOException {

  servletResponse.setContentType("text/html");
  PrintWriter out = servletResponse.getWriter();

  String userInput1 = servletRequest.getParameter("number1");
  int userNum = Integer.parseInt(userInput1);

  String userInput2 = servletRequest.getParameter("number2");
  int userNum2 = Integer.parseInt(userInput2);

  String userInput3 = servletRequest.getParameter("number3");
  int userNum3 = Integer.parseInt(userInput3);

  String userInput4 = servletRequest.getParameter("number4");
  int userNum4 = Integer.parseInt(userInput4);

  String userInput5 = servletRequest.getParameter("number5");
  int userNum5 = Integer.parseInt(userInput5);

  String userInput6 = servletRequest.getParameter("number6");
  int userNum6 = Integer.parseInt(userInput6);


  if(
   userNum != userNum2 || userNum != userNum3 || userNum != userNum4 || 
   userNum != userNum5 || userNum != userNum6 &&
   userNum2 != userNum || userNum2 != userNum3 || userNum2 != userNum4 || 
   userNum2 != userNum5 || userNum2 != userNum6 &&
   userNum3 != userNum || userNum3 != userNum2 || userNum3 != userNum4 || 
  userNum3 != userNum5 || userNum != userNum6 &&
   userNum4 != userNum || userNum4 != userNum2 || userNum4 != userNum3 ||   
  userNum4 != userNum5 || userNum5 != userNum6 &&
   userNum5 != userNum || userNum5 != userNum2 || userNum5 != userNum3 ||   
  userNum5 != userNum4 || userNum5 != userNum6 &&
   userNum6 != userNum || userNum6 != userNum2 || userNum6 != userNum3 || 
  userNum6 != userNum4 || userNum6 != userNum5){

   int[] numberArray = {userNum, userNum2,    
  userNum3,userNum4,userNum5,userNum6,};

    out.println("<html><body>Your numbers selection in order, " +     
  Arrays.toString(numberArray) + "  valid </body></html>");
   }
   else{
    out.println("<html><body> Error! </body></html>");
   }

   }
  }

index.html

<!DOCTYPE html>
<html>
<body>

 <h1 > Lotto Servlet Game </h1 >
 <h2 > Please choose your numbers below </h2 >

 <form action="Lotto">
 <fieldset>
 Please enter your name: <br><input type="text" name="username"><br>
 #1:<br>
 <input type="text" name="number1" min="1" max="42">
 <br>
 #2:<br>
 <input type="text" name="number1" min="1" max="42">
 <br>
 #3:<br>
 <input type="text" name="number1" min="1" max="42">
 <br>
 #4:<br>
 <input type="text" name="number1" min="1" max="42">
 <br>
 #5:<br>
 <input type="text" name="number1" min="1" max="42">
 <br>
 #6:<br>
 <input type="text" name="number1" min="1" max="42">
 <br><br>
 <input type="submit" min="Submit">
 </fieldset>
 </form>



</body>
</html>

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                                       http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd" xmlns:xsi="
                                       http://www.w3.org/2001/XMLSchema-   
instance" xmlns="
                                       http://java.sun.com/xml/ns/j2ee">


 <display-name>Sample Application</display-name>
 <description> Sample Application. </description>


 <servlet>
   <servlet-name>LottoServlet</servlet-name> 
   <servlet-class>Lotto</servlet-class> 
 </servlet>

 <servlet-mapping>
      <servlet- name>LottoServlet</servlet-name> 
      <url-pattern>/Lotto</url-pattern> 
 </servlet-mapping>
</web-app>

The three files have the following directories:

Lotto.class Z:\apache-tomcat-8.5.5\webapps\sample\WEB-INF\classes\Lotto.class

index.html Z:\apache-tomcat-8.5.5\webapps\sample\index.html

web.xml Z:\apache-tomcat-8.5.5\webapps\sample\WEB-INF\web.xml

Upvotes: 0

Views: 467

Answers (3)

user5762337
user5762337

Reputation:

Have you checked if your .war file has the correct structure? I.e in IntelliJ if you "add Framework Support --> Web" it creates the web.xml, but if you look at your artifacts the structure is not correct...

Build a artifact with the following structure:

name.war
    -> META-INF
        ->MANIFEST.MF
    ->WEB-INF
        ->lib
            ->required .jars if needed
    ->compile-output (thats the name in IntelliJ)

Check your run configurations if your .war file is selected for the deployment. Maybe this helps, I had a lot of problems with deployment on tomcat, because the console gave no information that the .war files are not correct.

Upvotes: 0

stdunbar
stdunbar

Reputation: 17435

When in doubt, remove code. For example, changing your servlet to just be:

public class Lotto extends HttpServlet {
    public void doGet(HttpServletRequest servletRequest,
                      HttpServletResponse servletResponse)
            throws ServletException, IOException {

        servletResponse.setContentType("text/html");
        PrintWriter out = servletResponse.getWriter();

        out.println("<html><body>You are in the servlet</body></html>");
    }
}

can show you that it is working. If you get that far, you can then debug further. At that point you'll learn that your compare logic is a bit messed up. But first, get things running. As @adamM mentioned, you can try to hit, for example http://localhost:8080/sample/Lotto in your browser first also.

Upvotes: 0

Lyons Huang
Lyons Huang

Reputation: 106

In index.html, all the field name are number1.

When you pass these params to the servlet. It will throw Exception where you call Integer.parseInt(userInput2).

Upvotes: 0

Related Questions