Reputation: 4557
I need to run my servlet on a server with a running tomcat.
I create my HelloWorld
servlet from the java file(HelloWorld.java
).
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Hello World</title></head>");
out.println("<body><h1>HELLO WORLD</h1></body>");
out.println("</html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet( request, response );
}
}
Than I upload it on a server and run
javac HelloWorld.java
This command creates HelloWorld.class file which i put into WEB-INF/classes
folder
Than I add some code to the web.xml file in the WEB-INF directory, so it looks like this
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>My first Servlet</display-name>
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HW</url-pattern>
</servlet-mapping>
</web-app>
Than i run a command
touch ~WEB-INF/web.xml
Now i try to access my HelloWorld servlet by entering URL like ~\HW.
But i get an error:
type Status report
message /group05/HW
description The requested resource (/group05/HW) is not available.
What would you recommend to do to fix it?
Thanks for considering my question.
Upvotes: 0
Views: 338
Reputation: 1108537
There are in this particular case 2 potential causes:
Tomcat isn't configured to hotdeploy after a web.xml
edit. You need to restart Tomcat manually.
The particular Tomcat setup doesn't support servlet classes in the default package. You should always put the class in a package if the class is intented to be used by another classes which are by itself inside a package (such as Tomcat internal classes).
Unrelated to the concrete problem, I understand that you're just getting started with servlets, I will however point out that this isn't the "best practice" to use servlets. I suggest to take a look in our servlets wiki page to get some concrete examples, learn about the canonical approaches and find links to proper tutorials.
Upvotes: 1