问题描述
- java web无法调用含有@WebServlet("")的java方法
- 我想问一下,我这样要怎么才能调用src里包里的java方法?
web.xml:
<?xml version=""1.0"" encoding=""UTF-8""?><web-app xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://java.sun.com/xml/ns/javaee"" xmlns:web=""http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"" xsi:schemaLocation=""http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"" version=""2.5"" metadata-complete=""true""> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>(省略包名).utils.StatisticsContextListener</listener-class> </listener> <filter> <filter-name>charsetfileter</filter-name> <filter-class>(省略包名).utils.CharSetFilter</filter-class> </filter> <filter-mapping> <filter-name>charsetfileter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
charsetfilter.java:
public class CharSetFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request ServletResponse response FilterChain chain) throws IOException ServletException { request.setCharacterEncoding(""utf-8""); response.setCharacterEncoding(""utf-8""); response.setContentType(""text/html;charset=UTF-8""); chain.doFilter(request response); } public void init(FilterConfig arg0) throws ServletException { System.out.println(""init""); }}
想调用的方法:
AskShopInfo.java:@WebServlet(""/AskShopInfo"")public class AskShopInfo extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { System.out.println(""AskShopInfo""); } protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException { System.out.println(""AskShopInfo""); doGet(request response); }}
解决方案
正常来说你直接用@WebServlet注解的话,直接访问这个注解的地址即http://localhost:8080/ProjectName/AskShopInfo就能访问这个servlet了。
如果不能访问,检查下tomcate的版本,@WebServlet注解是Servlet3.0的新特性,只有tomcat 7.0.X 支持Servlet 3.0。
注解的详解,参考:http://blog.csdn.net/xiazdong/article/details/7208316
解决方案二:
java 调用web服务的方法
JAVA 调用Web Service的方法
JAVA 调用Web Service的方法
时间: 2024-10-29 05:01:11