2. 以字符集過濾器爲例,編碼:html
public class Servlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("你好,世界"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
<servlet> <servlet-name>servlet</servlet-name> <servlet-class>per.lc.Servlet.Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet</servlet-name> <url-pattern>/hi</url-pattern> </servlet-mapping>
結果:頁面亂碼,這裏就不貼圖了。java
處理辦法:web
1.在servlet類中添加resp.setContentType("text/html;charset=utf");app
也可以解決亂碼的問題。問題是當項目中有多個servlet時,每一個都要去增長這一句,非常麻煩,正確的姿式是使用過濾器。jsp
2.建立過濾器ide
public class CharacterEncodingFilter implements Filter { private String characterEncoding="utf-8"; public void init(FilterConfig filterConfig) throws ServletException { System.out.println("攔截開始"); if(filterConfig.getInitParameter("Encoding")!=null){ characterEncoding=filterConfig.getInitParameter("Encoding"); } } public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request=(HttpServletRequest)servletRequest; //servletRequest是HttpServletRequest的超類,這裏主要是要處理http協議,就要強轉爲HttpServletReqest類 HttpServletResponse response=(HttpServletResponse) servletResponse; request.setCharacterEncoding(characterEncoding); response.setContentType("text/html;charset="+characterEncoding); filterChain.doFilter(request,response); } public void destroy() { System.out.println("攔截結束"); characterEncoding=null; }
//ServletReqest 、HttpServletRequest只是接口,並不去實現接口的方法,方法是由第三方廠商實現的,好比Tomcat,在Catalina.jar 包由對應的方法的實現。
<filter> <filter-name>characterEncoding</filter-name> <filter-class>per.lc.FilterDemo.CharacterEncodingFilter</filter-class> <init-param> <param-name>Encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
將這段添加到web.xml
運行程序,中文成功顯示出來。ui
3.<url-pattern>的設置範圍:this
<url-pattern>/*</url-pattern> The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet.
This is thus a bad URL pattern for servlets. Usually, you'd like to use /* on a Filter only. It is able to let the request continue to any of the servlets listening on a more specific URL pattern by calling FilterChain#doFilter().
<url-pattern>/</url-pattern> The / doesn't override any other servlet. It only replaces the servletcontainer's builtin default servlet for all requests which doesn't match any other registered servlet.
This is normally only invoked on static resources (CSS/JS/image/etc) and directory listings. The servletcontainer's builtin default servlet is also capable of dealing with HTTP cache requests, media (audio/video) streaming and file download resumes.
Usually, you don't want to override the default servlet as you would otherwise have to take care of all its tasks, which is not exactly trivial (JSF utility library OmniFaces has an open source example). This is thus also a bad URL pattern for servlets.
As to why JSP pages doesn't hit this servlet, it's because the servletcontainer's builtin JSP servlet will be invoked, which is already by default mapped on the more specific URL pattern *.jsp
英文中的概念暫時不理解,先記住老師講的。/* 會攔截全部的請求,包括jsp頁面,不建議使用,通常只在過濾器中使用。/ 只會做用在映射爲根路徑的servlet默認首頁index.jsp的JSPServlet會覆蓋 / 根目錄默認的Servlet