熟悉又陌生的JavaWeb 第3天

迷路的哨兵甲發表於2024-03-08

傳送門

Servlet程式設計

第2天的開頭部分已經看過Servlet
這裡再介紹一下註解方式,可以不用配置web.xml
使用註解的前提是web.xml的version是4.0(3.0以上應該就行)

@WebServlet(name = "MyNewServlet", value = "/MyNewServlet")
public class MyNewServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body>");
        out.println("<h1>new Servlet</h1>");
        out.println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

認識Servlet

編寫Servlet

Servlet的生命週期

Servlet與JSP內建物件

設定歡迎頁面

不配置預設就是index.jsp
web.xml

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

在Servlet中讀取引數

    <context-param>
        <param-name>allServletCanRead</param-name>
        <param-value>全域性變數所有Servlet都可以取到</param-value>
    </context-param>
    <servlet>
        <servlet-name>MyCustomServlet</servlet-name>
        <servlet-class>com.lazyking.MyCustomServlet</servlet-class>
        <init-param>
            <param-name>onlyMeCanRead</param-name>
            <param-value>區域性變數僅限當前物件可以取到</param-value>
        </init-param>
    </servlet>
        String onlyMeCanRead = this.getInitParameter("onlyMeCanRead");

        //這個其實就是內建的application物件 ServletContext類
        ServletContext servletContext = this.getServletContext();
        String allServletCanRead = servletContext.getInitParameter("allServletCanRead");

使用過濾器

註解或者xml配置,建議註解

xml配置和註解是一一對應的,選一種

<filter>  
    <filter-name>CustomFilter</filter-name>
    <filter-class>com.lazyking.CustomFilter</filter-class>
<init-param>
    <param-name>yourParam</param-name>
    <param-value>yourValue</param-value>  
</init-pamm>  
</filter> 
 
<filter-mapping>
    <filter-name>CustomFilter</filter-name>  
<url-pattern>/*</url-pattern>

@WebFilter(filterName = "CustomFilter",urlPatterns = {"/*"},initParams = {@WebInitParam(name = "yourParam", value = "yourValue")})
public class CustomFilter implements Filter {

    private String yourParam;
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init CustomFilter");
        this.yourParam = config.getInitParameter("yourParam");
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("請求進入Servlet前");
        System.out.println("request param is " + request.getParameterMap());
        chain.doFilter(request, response);
        System.out.println("請求執行完成響應至於客戶端前");
        System.out.println();
        if (response instanceof HttpServletResponse response1){
            if (response1.getStatus() == 200){
                response1.getWriter().println("<br>過濾器追加" + yourParam);
            }
        }
    }
}

異常處理

古早年代的統一異常處理方式
web.xml

    <error-page>
        <exception-type>java.lang.Throwable</exception-type>
        <location>/error/error.jsp</location>
    </error-page>

課後習題

相關文章