Patrick Lorio
Patrick Lorio

Reputation: 5668

tomcat request servlet

How do I map a url to a HttpServlet class in tomcat.

Example I want requests /calc to be handled by Calc.java

so a request to 127.0.0.1:800/calc would call:

public class Calc extends HttpServlet {
    /* ... */
}

Upvotes: 0

Views: 89

Answers (4)

kosa
kosa

Reputation: 66637

I think this configuration should be in web.xml in your war file, not in tomcat.

If you are using Servlet3.0 then you may use annotations also.

@WebServlet(urlPatterns="/yoururl")

Upvotes: 0

Shivan Dragon
Shivan Dragon

Reputation: 15219

If you're on tomcat7 it's as easy as adding the annotation:

@WebServlet(urlPatterns="/calc")
public class Calc extends HttpServlet {
    /* ... */
}

Upvotes: 2

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

Add following configuration in your web.xml

  <servlet>
    <servlet-name>calcServlet</servlet-name>
    <servlet-class>packagename.Calc</servlet-class> <!-- change the name of package according to your class -->
  </servlet>
  <servlet-mapping>
    <servlet-name>calcServlet</servlet-name>
    <url-pattern>/calc</url-pattern>
  </servlet-mapping>

Upvotes: 0

matt b
matt b

Reputation: 139921

I think you would benefit greatly from reading the Tomcat manual section on "your first webapp", which covers this.

Upvotes: 0

Related Questions