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.