views:

236

answers:

2

Hi, I´ve installed Tomcat and I've been testing it: I wrote some .html and .jsp files and tried then in the server. They semm to work correctly together. For example: these files I'm trying allow me to upload a file to the server and writes its name in a database (mysql). Once this is done I have a button that allows me to upload another file or I can consult the name of the files stored in the database.

My problem comes when I need to run a servlet. I'm trying a basic one:

package HelloWorldServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class HelloWorldServlet extends HttpServlet {

  public void init(ServletConfig conf)
    throws ServletException
  {
    super.init(conf);
  }
  public void service(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<html>");
    out.println("<body>");
    out.println("<h1>Hello World</h1;>");
    out.println("</body>");
    out.println("</html>");
  }
}

From that I get a .class file. I put this file in: webapps/HelloWord/web-inf/classes

i really don't know how to modify the web.xml file and how to call this servlet from an .html or .jsp page

Thanks to all who can help me.

+1  A: 

First of all, your web-inf directory must be in upper-case (WEB-INF).

Basic web.xml looks like this:

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

<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"&gt;

<web-app>
    <servlet>
      <!-- 
        This is arbitrary name for your servlet,
        used in servlet-mapping below
       -->
      <servlet-name>HelloWorld</servlet-name>

      <!-- Name of your servlet class -->
      <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>

    <servlet-mapping>
      <servlet-name>HelloWorld</servlet-name>

      <!--
         Here you say location (under context) where your servlet
         can receive requests.
       -->
      <url-pattern>/hello-world</url-pattern>
    </servlet-mapping>
</web-app>

Your web.xml must be saved in <your-app>/WEB-INF/web.xml path.

Now whenever browser will access http://localhost/HelloWorld/hello-world on your server, your servlet we be called, because it is mapped to /hello-world, and because your application is deployed in HelloWorld directory (thus mapped to /HelloWorld context).

Peter Štibraný
Hi. I did everything as you said, now I'm getting a different error. When I pasted my code to this page I forog the first line : "package HelloWorldServlet;"Does it make things different? If not I'll post the erros I'm getting. Thanks
@Charles - Adding a package changes the fully qualified class name of the servlet to HelloWorldServlet.HelloWorldServlet which needs to be in the file WEB-INF/classes/HelloWorldServlet/HelloWorldServlet.class
McDowell
It worked, thank you!!
Imagine that the servlet described uses a method that belongs to a class (writeLog) included in the HelloWorldServlet package. Shold I put the writeLog.class in the same folder than HelloWorldServlet.class? Do I need to modify anything else? Thanks
Yes. Put it into same folder. You don't need to modify anything else.
Peter Štibraný
Is it possible to have several classes which extends HttpServlet in the same package? I tried that but I think I'm getting an error when I introduce that in web.xml.I don't know if I can do that in the same package (how should I modify web.xml then?) or if I define this class in a new package (web.xml is still a problem for me). Thanks
Yes, it is possible. If you add more servlets (classes that extend HttpServlet), you need to also add `servlet` and `servlet-mapping` elements into web.xml for each new servlet. Btw, it would be better to ask new questions not via comments, but by creating new question directly. Only *I* am notified that new comment was added to my answer, but new *real questions* are prominently displayed on main page ... you would get answer faster ;-)
Peter Štibraný
It doesn't matter that new servlets are in same package. What matters is full name of class -- you always need to write that name into `servlet/servlet-class` element. Full name of class includes package name, so if your `Servlet1` and `Servlet2` are in package `myservlets`, you will need to add new `servlet` element twice, first time with `<servlet-class>myservlets.Servlet1</servlet-class>`, and once with `<servlet-class>myservlets.Servlet2</servlet-class>`
Peter Štibraný
+1  A: 

First you map the Servlet class to a name. Then you map the name to a url-pattern. The url pattern can be a single path or it can be a "globbing" pattern like /path/* or just /*

Something like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="TestApp" version="2.4" 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"&gt;
    <display-name>Test App</display-name>
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>HelloWorldServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>
geofflane