servlet是什么?
百度百科解释一堆:
servlet 是在服务器上运行的小程序。这个词是在 Java applet 的环境中创造的
看着有点绕,个人认为servlet是java对rfc2616协议的封装,既是http协议的java封装.一个java类附rfc2616文档
根据红薯的文章,在myeclipse下测试,
1,新建web project;
2,xml添加servelt配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>ServletTest</servlet-name> <servlet-class>TestS</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletTest</servlet-name> <url-pattern>/ServletTest</url-pattern> </servlet-mapping> </web-app>
凡符合url-pattern中/ServletTest的请求,转由servlet-class对应的类TestS处理
servlet下servlet-name和servlet-mapping下servlet-name必须相同
3,TestS类:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestS extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub //super.doGet(req, resp); resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.print("你好,HELLO"); out.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doPost(req, resp); } }
开启tomcat,访问http://localhost:8080/工程名/ServletTest
页面返回:
你好,HELLO
filter:
web.xml中servlet前添加:
<filter> <filter-name>helloFilter</filter-name> <filter-class>HelloFilter</filter-class> </filter> <filter-mapping> <filter-name>helloFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
创建HelloFilter.java文件
import java.io.IOException; import javafx.collections.ListChangeListener.Change; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class HelloFilter implements Filter { public void destroy() { // TODO Auto-generated method stub System.out.println("destory filter"); } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("do filter"); //arg2.doFilter(arg0, arg1); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("init filter"); } }
重启tomcat,
控制台在启动tomcat过程打印:init filter
控制台在关闭tomcat过程打印:destory filter
通过浏览器访问http://localhost:8080/工程名/ServletTest,页面没有任何输出,http请求被过滤器拦截,(这也就是过滤器的含义,过滤器也拦截操作HttpServletRequest和HttpServletResponse)控制台打印:do filter
反注释//arg2.doFilter(arg0, arg1);
通过浏览器访问http://localhost:8080/工程名/ServletTest,页面输出"你好,HELLO",http请求被过滤器拦截控制台打印:do filter
----------------------分割线-----------------------------------
修改HelloServlet类,
dopost方法内部调用doget,
doget方法内部同时调用dopost,代码如下:
package demo; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub resp.setContentType("text/html;charset=utf-8"); PrintWriter out = resp.getWriter(); out.print("你好,HELLO"); out.close(); System.out.println("doGet function execute!"); this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(req, resp); } }
做个死循环,是否会报错?测试下哈:
访问正常,控制台打印:"do filter",页面正常显示"你好,HELLO",
意料之外的是控制台没有打印"doGet function execute!",死循环也不产生作用,没有报错.