简体   繁体   English

tomcat请求servlet

[英]tomcat request servlet

How do I map a url to a HttpServlet class in tomcat. 如何将网址映射到tomcat中的HttpServlet类。

Example I want requests /calc to be handled by Calc.java 示例我希望Calc.java处理请求/计算

so a request to 127.0.0.1:800/calc would call: 所以对127.0.0.1:800/calc的请求会调用:

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

If you're on tomcat7 it's as easy as adding the annotation: 如果你在tomcat7上,它就像添加注释一样简单:

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

I think this configuration should be in web.xml in your war file, not in tomcat. 我认为这个配置应该在你的war文件中的web.xml中,而不是在tomcat中。

If you are using Servlet3.0 then you may use annotations also. 如果您使用的是Servlet3.0,那么您也可以使用注释。

@WebServlet(urlPatterns="/yoururl")

我想你会从阅读有关“你的第一个webapp”的Tomcat手册部分中获益匪浅,其中包含了这个内容。

Add following configuration in your web.xml 在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM