首先建立java八个类
public class OneServlet implements Servlet {} public class TwoServlet extends GenericServlet{} public class ThreeServlet extends HttpServlet{} /** * Servlet implementation class fourServlect * @xxxxx:java注解用少量 */ @WebServlet(urlPatterns="/d") public class fourServlect extends HttpServlet { @WebServlet("/hellow") public class FiveServlect extends HttpServlet {} @WebServlet("/abc/*") public class SixServlect2 extends HttpServlet { /** * Servlet implementation class SevenServlect * 3.后缀名匹配 以"*"开始 以字符结尾 例如:*.jsp *.do *.action */ @WebServlet("*.abc")
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /* * 当浏览器没有明确使用get还是post去访问服务器的时候,默认是get请求 url-pattern详解 1.完全匹配 必须以"/"开始 例如:/hello /a/b/c 2.目录匹配 必须以"/"开始 以"*"结束 例如:/a/* /* 3.后缀名匹配 以"*"开始 以字符结尾 例如:*.jsp *.do *.action 优先级:完全匹配>目录匹配>后缀名匹配 */ public class OneServlet implements Servlet { public OneServlet() { // TODO Auto-generated constructor stub /* * System.out.println("当浏览器第一次访问servlet 构造方法---int方法-service方法"); * System.out.println("当多次访问同一个servlet,只会执行service方法"); * System.out.println("说明浏览器每一次访问servlet就是第一次访问创建同一个对象"); * System.out.println(" Servlet创建构造方法"); */ } @Override public void destroy() { // dectroy 删除select,cpu释放内存 System.err.println("当servlet不在实现该方法"); } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub //获得 get servlet 小程序 confign 设置信息 //getServletConfig()作用用于servlet在/web/html文件中设置信息 return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } @Override public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub //初始数据 System.out.println("加载信........息"); System.out.println("说明浏览器每一次访问servlet就是第一次访问创建同一个对象"); System.out.println("加载信息。。。。。2"); System.out.println("这是第一个oneservlect "); } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub //等待浏览器访问 System.out.println("servlect处于运行状态"); System.out.println("public class OneServlet implements Servlet {"); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SevenServlect * 3.后缀名匹配 以"*"开始 以字符结尾 例如:*.jsp *.do *.action */ @WebServlet("*.abc") public class SevenServlect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SevenServlect() { super(); // TODO Auto-generated constructor stub System.out.println("3.后缀名匹配 以\"*\"开始 以字符结尾 运行结果 "); System.out.println("servlect处于运行状态\r\n" + "public class OneServlet implements Servlet {\r\n" + "当前servlet处于运行\r\n" + "public class TwoServlet extends GenericServlet {\r\n" + "我收到浏览器的get请求访问 get---doget\r\n" + "这是第三个oneservlect \r\n" + "public class ThreeServlet extends HttpServlet{\r\n" + "这是第四个servlect \r\n" + "这是第五个servlect \r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:30:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "十月 06, 2021 1:30:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "这是第五servlect 1.完全匹配 必须以\"/\"开始 \r\n" + "例如:/hello /a/b/c\r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:31:39 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "十月 06, 2021 1:31:39 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "这是第六个servlect 2.目录匹配 必须以\"/\"开始 以\"*\"结束 \r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:32:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "十月 06, 2021 1:32:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "3.后缀名匹配 以\"*\"开始 以字符结尾 \r\n" + ""); System.out.println("完全匹配>目录匹配>后缀名匹配"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ThreeServlet extends HttpServlet{ //http:网络协议 TCP/LP 被动协议 需要用户去访问服务器 ,服务器材会发送数据。 //有四种发送数据方式:get 发送请求 post发送请求 public ThreeServlet() { // TODO Auto-generated constructor stub System.out.println("tree对象在创建"); } //UDP:主动协议,不管用户要不要 ,服务器都会发送数据 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("我收到浏览器的get请求访问 get---doget"); System.out.println("这是第三个oneservlect "); System.out.println("public class ThreeServlet extends HttpServlet{"); /* * System.out.println("http://localhost:8080/Day02jsp/"); * System.out.println("get 如果浏览器发送多个数据以后与字符连接"); * System.out.println("登录功能一律不使用post的请求,"); System.out.println("get post "); * System.out.println("get请求数据拼接在地址上,地址长度有限 ,lkb=1024 一个字母二个字节 是2个字母"); * System.out.println("post 数据是不受限制的 get获得"); */ } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("我收到浏览器的post请求访问 post----dopost"); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class fourServlect * @xxxxx:java注解用少量 */ @WebServlet(urlPatterns="/d") public class fourServlect extends HttpServlet { private static final long serialVersionUID = 1L; //定义一个变量 int countt=0; String hdrt="L LOVE YOU hellow world"; /** * @see HttpServlet#HttpServlet() */ public fourServlect() { System.out.println("这是第四个servlect "); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub countt++; response.getWriter().append("Served at:get countt"+countt+"l love you "+countt+""); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at:get "); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class FiveServlect * 1.完全匹配 必须以"/"开始 例如:/hello /a/b/c */ @WebServlet("/hellow") public class FiveServlect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FiveServlect() { System.out.println("这是第五servlect 1.完全匹配 必须以\"/\"开始 \r\n" + "例如:/hello /a/b/c"); System.out.println("http://localhost:8080/Day02jsp/bbb?money=&name="); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class FiveServlect * 2.目录匹配 必须以"/"开始 以"*"结束 例如:/a/* /* */ @WebServlet("/abc/*") public class SixServlect2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SixServlect2() { System.out.println("这是第六个servlect 2.目录匹配 必须以\"/\"开始 以\"*\"结束 "); System.out.println("http://localhost:8080/Day02jsp/bbb?money=&name="); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class SevenServlect * 3.后缀名匹配 以"*"开始 以字符结尾 例如:*.jsp *.do *.action */ @WebServlet("*.abc") public class SevenServlect extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SevenServlect() { super(); // TODO Auto-generated constructor stub System.out.println("3.后缀名匹配 以\"*\"开始 以字符结尾 运行结果 "); System.out.println("servlect处于运行状态\r\n" + "public class OneServlet implements Servlet {\r\n" + "当前servlet处于运行\r\n" + "public class TwoServlet extends GenericServlet {\r\n" + "我收到浏览器的get请求访问 get---doget\r\n" + "这是第三个oneservlect \r\n" + "public class ThreeServlet extends HttpServlet{\r\n" + "这是第四个servlect \r\n" + "这是第五个servlect \r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:30:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "十月 06, 2021 1:30:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "这是第五servlect 1.完全匹配 必须以\"/\"开始 \r\n" + "例如:/hello /a/b/c\r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:31:39 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "十月 06, 2021 1:31:39 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "这是第六个servlect 2.目录匹配 必须以\"/\"开始 以\"*\"结束 \r\n" + "http://localhost:8080/Day02jsp/bbb?money=&name=\r\n" + "十月 06, 2021 1:32:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已开始重新加载名为[/Day02jsp]的上下文\r\n" + "当servlet不在实现该方法\r\n" + "Twoservlet 在运行 过程中哦哦哦\r\n" + "这是第二个oneservlect \r\n" + "十月 06, 2021 1:32:49 下午 org.apache.catalina.core.StandardContext reload\r\n" + "信息: 已完成重新加载名为/Day02jsp的上下文\r\n" + "tree对象在创建\r\n" + "加载信........息\r\n" + "说明浏览器每一次访问servlet就是第一次访问创建同一个对象\r\n" + "加载信息。。。。。2\r\n" + "这是第一个oneservlect \r\n" + "3.后缀名匹配 以\"*\"开始 以字符结尾 \r\n" + ""); System.out.println("完全匹配>目录匹配>后缀名匹配"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ten */ @WebServlet("/ten") public class ten extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ten() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
web.xlm文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0"> <display-name>Day02jsp</display-name> <welcome-file-list> <!-- <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> --> <welcome-file>index.jsp</welcome-file> <!-- <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> --> </welcome-file-list> <!-- 注册oneServlet --> <!-- 第一部分 注册OneServlet--> <servlet> <!-- srvlet 增加名字 --> <servlet-name>OneServlet</servlet-name> <!--注册servlet地址 --> <servlet-class>com.servlet.OneServlet</servlet-class> <load-on-startup>5</load-on-startup> </servlet> <!--第二部分 :设置一个浏览器可以访问的servlet的地址 --> <servlet-mapping> <servlet-name>OneServlet</servlet-name> <!--设置浏览器地址 --> <url-pattern>/a</url-pattern> <!-- 设置地址访问一定加/asd --> </servlet-mapping> <!-- 注册twoServlet --> <!-- 第一部分 注册OneServlet--> <servlet> <!-- srvlet 增加名字 --> <servlet-name>TwoServlet</servlet-name> <!--注册servlet地址 --> <servlet-class>com.servlet.TwoServlet</servlet-class> <load-on-startup>0</load-on-startup> </servlet> <!--第二部分 :设置一个浏览器可以访问的servlet的地址 --> <servlet-mapping> <servlet-name>TwoServlet</servlet-name> <!--设置浏览器地址 --> <url-pattern>/b</url-pattern> <!-- 设置地址访问一定加/asd --> </servlet-mapping> <!-- 注册treeServlet --> <!-- 第一部分 注册OneServlet--> <servlet> <!-- srvlet 增加名字 --> <servlet-name>ThreeServlet</servlet-name> <!--注册servlet地址 --> <servlet-class>com.servlet.ThreeServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!--第二部分 :设置一个浏览器可以访问的servlet的地址 --> <servlet-mapping> <servlet-name>ThreeServlet</servlet-name> <!--设置浏览器地址 --> <!-- *:表示0个或任意字符--> <url-pattern>/c</url-pattern> <!-- 设置地址访问一定加/asd --> </servlet-mapping> </web-app>
运行结果
第二章完毕