时长:大小8.16M
作者回复: web.xml 和注解可以同时工作的。 例子里的web.xml和注解不能同时工作的原因是web.xml中的配置metadata-complete="true", 你需要把它设置成metadata-complete="false"。 metadata-complete为true的意思是,告诉Tomcat不要去扫描Servlet注解了。
作者回复: 调下顺序,像下面这样: response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); getWrite的源码如下: ------ public PrintWriter getWriter() throws IOException { if (usingOutputStream) { throw new IllegalStateException (sm.getString("coyoteResponse.getWriter.ise")); } if (ENFORCE_ENCODING_IN_GET_WRITER) { /* * If the response's character encoding has not been specified as * described in <code>getCharacterEncoding</code> (i.e., the method * just returns the default value <code>ISO-8859-1</code>), * <code>getWriter</code> updates it to <code>ISO-8859-1</code> * (with the effect that a subsequent call to getContentType() will * include a charset=ISO-8859-1 component which will also be * reflected in the Content-Type response header, thereby satisfying * the Servlet spec requirement that containers must communicate the * character encoding used for the servlet response's writer to the * client). */ setCharacterEncoding(getCharacterEncoding()); } usingWriter = true; outputBuffer.checkConverter(); if (writer == null) { writer = new CoyoteWriter(outputBuffer); } return writer; } ----- 你看注释里它说:如果调这个方法之前没有指定Response的字符编码,就用默认的ISO-8859-1,ISO-8859-1不包括中文字符。
作者回复: 1. 你可以向Servlet容器注册多个Servlet。 2. 你看这个有没有帮助 https://github.com/spring-projects/spring-framework/issues/14296
作者回复: get和post的重要区别是,前者不能改变服务端的数据,是幂等;而Post可以改变服务端数据。
作者回复: super.doGet(req, resp); 调的是HttpServlet的doGet方法,但是这个doGet需要你去实现的。 HttpServlet的service方法会调doXXX方法,并且HttpServlet里的各种doXXX方法的默认实现都是直接返回错误。 为什么HttpServlet要这样设计呢?这是因为它需要做一个限制:程序员要么重写HttpServlet的service方法,要么重写HttpServlet的doXXX方法。 web.xml和注解可以同时工作,你需要把web.xml中的metadata-complete="true"设置成false。 Tomcat启动时会扫描注解,同时记下Servlet的名字和映射路径,如果你设置了延迟记载Servlet,通过浏览器访问时Tomcat才会加载和实例化Servlet。
作者回复: 在Tomcat中CoyoteAdapter类的service方法里
作者回复: 你说的没错,具体是这样的 Tomcat的Wrapper组件-Filter-DispatcherServlet-Controller
作者回复: Servlet3.0开始支持
作者回复: JDK不带Servlet Jar包。
作者回复: 😑