Servlet注解式开发

servlet注解式开发:对web.xml文件进行了瘦身处理。

                      ----大道至简

/**
 * @author: Mr.Wu
 * @description Servlet注解开发
 * 在webServlet注解中配置了urlpatterns,配置了initparams,配置了自启动
 * @date: 2021/12/15 17:04
 */
@WebServlet(urlPatterns = "/anno.do",initParams = {@WebInitParam(name = "name1",value = "value1"),@WebInitParam(name = "name2",value = "value2")},loadOnStartup = 1)
public class AnnotationServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        ServletConfig servletConfig = this.getServletConfig();
        System.out.println(servletConfig.getInitParameter("name1"));
        System.out.println(servletConfig.getInitParameter("name2"));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = this.getServletConfig();
        PrintWriter pw = resp.getWriter();
        pw.println(servletConfig.getInitParameter("name1"));
        pw.println(servletConfig.getInitParameter("name2"));
        pw.flush();
        pw.close();
    }
}

通过注解式开发,简化了代码,也更加方便阅读

posted @ 2021-12-15 17:13  (HelloWorld!)  阅读(200)  评论(0编辑  收藏  举报