-1

I have a servlet called DiceRollServlet that I try to run in a browser. However, I can't make it work and can't figure out why. I have the name mapping in a web.xml file and tried all kinds of paths:

http://localhost:8080/AppName/Lottery
http://localhost:8080/AppName/Lottery.do
http://localhost:8080/AppName/src/servlets/Lottery
http://localhost:8080/AppName/src/servlets/Lottery.do
http://localhost:8080/AppName/target/classes/Lottery
http://localhost:8080/AppName/target/classes/Lottery.do

but neither works.

web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

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

    <display-name>HelloWorld Application</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>DiceRoll</servlet-name>
        <servlet-class>servlets.DiceRollServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DiceRoll</servlet-name>
        <url-pattern>/Lottery</url-pattern>
    </servlet-mapping>

</web-app>

DiceRollServlet.java:

package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;


public class DiceRollServlet extends HttpServlet  {
            public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException  {
      PrintWriter out = response.getWriter();

      Random rand = new Random();

      out.println("<p>Your random numer is: " + rand.nextInt(100) + "/p>");
            }

}

folder structure:

tomcat
    webapps
        AppName
            src
                servlets
                      DiceRollServlet.java
            pom.xml
            web.xml

EDIT:

I changed my folder structure to

tomcat
    webapps
        AppName
            pom.xml
            WEB-INF
                web.xml
                classes
                    servlets
                        DiceRollServlet.java

and web.xml to

<?xml version="1.0" encoding="UTF-8" ?>

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

    <display-name>AppName</display-name>
    <description>
        This is a simple web application with a source code organization
        based on the recommendations of the Application Developer's Guide.
    </description>

    <servlet>
        <servlet-name>DiceRoll</servlet-name>
        <servlet-class>servlets.DiceRollServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DiceRoll</servlet-name>
        <url-pattern>/Lottery</url-pattern>
    </servlet-mapping>

</web-app>

But it still doesn't work...

Should I move the compiled .class file somewhere? Right now it's located in tomcat/webapps/AppName/target/classes/servlets folder.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
parsecer
  • 4,758
  • 13
  • 71
  • 140
  • Just to get the obvious out of the way: you did run maven to build the app, did you? If you did, what does the folder structure look like after that? – jingx Sep 28 '17 at 20:21
  • Your tomcat folder structure looks suspicious. Are you sure you have `src` and `.java` files? It looks your app is not compiled at all to be deployed to a tomcat server. In your folder structure you should have something like `webapps\AppName\WEB-INF\classes` and there all the `.class` files of your app including `DiceRollServlet.class` – pleft Sep 28 '17 at 20:32
  • @pleft, I don't have `WEB-INF` folder, though one of the testing classes located in `main/java` get compiled into `target/classes/main/java`, but there is not `DiceRollServlet.class` there. Also, not that I look closely, I see the file in `servlets` folder is not recognized somewhy (I use intellij) and there is not compile option when I right click it. – parsecer Sep 28 '17 at 20:46
  • @jingx I have several folders, what folder should I check for changes after running the maven? – parsecer Sep 28 '17 at 20:47
  • ok you have not built the application. You should first run `mvn clean package`, this will/should create a `war` file in your `target` directory. Then you should copy this `war` file to the `tomcat\webapps` folder and start tomcat. – pleft Sep 28 '17 at 20:48
  • @pleft Isn't intellij supposed to do the copying itself? – parsecer Sep 28 '17 at 21:01
  • if you don't see the war file in your tomcat's `webapps` folder, no. – pleft Sep 28 '17 at 21:03
  • You must deploy complies classes, jar files, or war files rather than source files. – Basil Bourque Sep 28 '17 at 21:58

1 Answers1

0

All the servlet files must reside at this path.---> (YOUR-DRIVE-PATH)\apache-tomcat-7.0.34(YOUR-VERSION)\webapps\AppName\WEB-INF\classes

web.xml path --------> (YOUR-DRIVE-PATH)\apache-tomcat-7.0.34(YOUR-VERSION)\webapps\AppName\WEB-INF

your web.xml should like this

<servlet>
    <servlet-name>DiceRoll</servlet-name>
    <servlet-class>DiceRollServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DiceRoll</servlet-name>
    <url-pattern>/Lottery</url-pattern>
</servlet-mapping>
yatinsingla
  • 434
  • 6
  • 13
  • Where should my `src` folder be located?? Under `WEB-INF`? – parsecer Sep 28 '17 at 20:56
  • 1
    What files you want to keep in src folder? You can keep you src folder both outside and inside of WEB-INF. But all your .html files will be in your AppName/YOUR-FILENAME.html .java files will be in your AppName/WEB-INF/classes/FILENAME.java web.xml will be in AppName/WEB-INF/web.xml. – yatinsingla Sep 28 '17 at 21:12
  • Could you please take a look at my edit in the question? – parsecer Sep 30 '17 at 12:37
  • You said "Right now it's located in tomcat/webapps/AppName/target/classes/servlets folder." It must be "tomcat/webapps/AppName/classes/servlets folder." And are you calling this java file from index.html file, or you trying to open this DiceRollServlet whe you start localhost? – yatinsingla Oct 04 '17 at 05:07