我想問一下,我這樣要怎麼才能調用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