Charles
Charles

Reputation:

Tomcat configuration for servlets

I've installed Tomcat and I've been testing it: I wrote some .html and .jsp files and tried then in the server. They seem 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.

Upvotes: 3

Views: 7210

Answers (2)

Peter Štibran&#253;
Peter Štibran&#253;

Reputation: 32911

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">

<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).

Upvotes: 4

geofflane
geofflane

Reputation: 2691

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">
    <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>

Upvotes: 1

Related Questions