Servlet註解式開發
阿新 • • 發佈:2021-12-15
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(); } }
通過註解式開發,簡化了程式碼,也更加方便閱讀
本文來自部落格園,作者:(HelloWorld!),轉載請註明原文連結:https://www.cnblogs.com/Simon-s/p/15693706.html --有志者,事竟成