时间:2023-05-25 04:06:01 | 来源:网站运营
时间:2023-05-25 04:06:01 来源:网站运营
2018-6-13 JSP_Web_MVC开发(29)-基于Annotation配置Servlet -利用注解替代web.xml:package cn.mytest.servlet;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class HelloServlet */@WebServlet(//配置WebServlet程序 name="helloServlet",//Servlet名称,与<servlet-name>相同,此名称内部起作用 urlPatterns= {"/hello","/demo/hello/*"}, //配置映射路径,与<url-mapping>相同,支持多个路径 initParams= {//初始化参数 @WebInitParam(value="Haha",name="p1"),//初始化参数的内容和名称 @WebInitParam(value="MySql",name="p2")}, loadOnStartup=1)//配置容器加载Servlet,当值大于0时候容器会在启动的时候就加载这个Servletpublic class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().println("<h1>Hello World!</h1>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override public void init(ServletConfig config) throws ServletException { System.out.println("*** Servlet初始化:"+config.getInitParameter("p1")); System.out.println("*** Servlet初始化:"+config.getInitParameter("p2")); }}
访问链接http://localhost/AWProject/demo/hello/fsdfds,或者http://localhost/AWProject/hello都可以看到页面显示"Hello World!"。package cn.mytest.filter;import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.annotation.WebFilter;import javax.servlet.annotation.WebInitParam;@WebFilter(filterName="encodingFilter", urlPatterns="/*", initParams= {//初始化参数 @WebInitParam(value="UTF-8",name="charSet")//初始化参数的内容和名称 })public class EncodingFilter implements Filter { private String charSet; @Override public void destroy() { } @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { System.out.println("*******************"); arg0.setCharacterEncoding(this.charSet); arg2.doFilter(arg0, arg1); } @Override public void init(FilterConfig arg0) throws ServletException { this.charSet=arg0.getInitParameter("charSet"); }}
package cn.mytest.listener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListenerpublic class MyListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // TODO Auto-generated method stub } @Override public void contextInitialized(ServletContextEvent arg0) { System.out.println("**********上下文初始化************"); }}
重启动Tomcat就可以看到效果。关键词:配置,利用,替代,注解