案例一:tomcat9、jdk1.8
1.eclipse创建web项目
1)创建一个Dynamic web project,名字为Servlet_hjh
2)在src下创建一个包,为com.hjh.day0306
3)在包下创建一个servlet类,为MyServlet
4)在WEB-INF目录下,创建一个web.xml文文件
2.MyServlet类代码如下
1 package com.hjh.day0306;
2
3 import java.io.IOException;
4 import javax.servlet.ServletException;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 //1、创建一个servlet,继承HttpServlet
10 public class MyServlet extends HttpServlet{
11
12 //2、实现doGet()方法,如果以get方式提交的话,调用这个方法来处理请求
13 public void doGet(HttpServletRequest req,HttpServletResponse rep) throws ServletException,IOException{
14 System.out.println("get");
15 }
16
17 //2.实现doPost()方法,如果以post方式提交的话,调用这个方法来处理请求
18 public void doPost(HttpServletRequest req,HttpServletResponse rep) {
19 System.out.println("post");
20 }
21
22 }
3.web.xml文件配置如下:
<?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_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet_hjh</display-name> <servlet> <!-- 给servlet取一个名字,一般和我们写的servlet类名一样 ,注意没有斜杠"/" --> <servlet-name>MyServlet</servlet-name> <!-- 给出当前servelt类的权限类名,用于搜索到具体的servlet类, 注意是用".",不是斜杠"/" --> <servlet-class>com.hjh.day0306.MyServlet</servlet-class> </servlet> <servlet-mapping> <!-- 这里的servlet-name和上面的要保持一致,注意没有斜杠"/" -->
<servlet-name>MyServlet</servlet-name>
<!-- 浏览器中通过url-pattern,找到servlet-name,然后找到具体的类 -->
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
4、启动服务器,在浏览器中输入以下url,按下回车键
title显示为
5、查看eclipse的输出结果
到此,案例一结束。
案例二:ServletConfig对象及其方法
1.eclipse创建web项目
1)创建一个Dynamic web project,名字为Servlet_hjh
2)在src下创建一个包,为com.hjh.day0307
3)在包下创建一个servlet类,为ServletTest01.java
4)在WEB-INF目录下,创建一个web.xml文文件
2.ServletTest01.java代码如下:
1 package com.hjh.day0307; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 11 public class ServletTest01 extends HttpServlet { 12 private static final long serialVersionUID = 1L; 13 14 public void doGet(HttpServletRequest req,HttpServletResponse rep) throws IOException,ServletException{ 15 System.out.println("doGet");//doGet 16 17 ////获取servlet的名称,也就是我们在web.xml中配置的servlet-name 18 System.out.println(this.getServletConfig().getServletName());//ServletTest01 19 System.out.println(this.getServletName());//ServletTest01 20 21 //getServletContext(); //获取ServletContext对象,该对象的作用看下面讲解 22 System.out.println(this.getServletConfig().getServletContext());//org.apache.catalina.core.ApplicationContextFacade@c5b11 23 System.out.println(this.getServletContext());//org.apache.catalina.core.ApplicationContextFacade@c5b11 24 25 //getInitParameterNames(); //获取在Servlet中所有初始化参数的名字,也就是key值,可以通过key值, 26 //来找到各个初始化参数的value值。注意返回的是枚举类型 27 System.out.println(this.getInitParameterNames());//java.util.Collections$3@1695784 28 System.out.println(this.getServletConfig().getInitParameterNames());//java.util.Collections$3@6bcd8d 29 30 Enumeration<String> initParamName = this.getServletConfig().getInitParameterNames(); 31 while(initParamName.hasMoreElements()) { 32 String name = initParamName.nextElement(); 33 System.out.println(name);//name age 34 //getInitParameter(name)方法,通过给定的参数key,获取对应的value 35 System.out.println(this.getInitParameter(name));//tom 18 36 System.out.println(this.getServletConfig().getInitParameter(name));//tom 18 37 } 38 } 39 40 public void doPost(HttpServletRequest req,HttpServletResponse rep) throws IOException,ServletException{ 41 System.out.println("doPost"); 42 } 43 }
3.web.xml文件配置如下:
<?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_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet_hjh</display-name> <!-- 第二个案例配置开始 --> <servlet> <!-- 给servlet取一个名字,一般和我们写的servlet类名一样 ,注意没有斜杠"/" --> <servlet-name>ServletTest01</servlet-name> <!-- 给出当前servelt类的权限类名,用于搜索到具体的servlet类, 注意是用".",不是斜杠"/" --> <servlet-class>com.hjh.day0307.ServletTest01</servlet-class> <!-- 给servlet类的参数设置初始值 ,注意这个标签放的位置,是放在Servlet标签中的--> <init-param> <!-- 参数名1 --> <param-name>name</param-name> <!-- 参数值1 --> <param-value>tom</param-value> </init-param> <init-param> <!-- 参数名2 --> <param-name>age</param-name> <!-- 参数值12 --> <param-value>18</param-value> </init-param> </servlet> <servlet-mapping> <!-- 这里的servlet-name和上面的要保持一致,注意没有斜杠"/" --> <servlet-name>ServletTest01</servlet-name> <!-- 浏览器中通过url-pattern,找到servlet-name,然后找到具体的类 --> <url-pattern>/test01</url-pattern> </servlet-mapping> <!-- 第二个案例配置完毕 --> </web-app>
4、启动服务器,浏览器中输入,按下回车键访问,
然后到eclipse中查看输入结果如下
案例三:ServletContext对象
web.xml:
<?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_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Servlet_hjh</display-name> <servlet> <servlet-name>ServletTest02</servlet-name> <servlet-class>com.hjh.day0307.ServletTest02</servlet-class> </servlet> <!-- 4、记住context-param标签的放置位置,为web-app标签对中 --> <context-param> <!-- 可以在servlet类中通过getServletContext().getInitParameter("name"),获取对应的值hjh --> <param-name>name</param-name> <param-value>hjh</param-value> </context-param> <context-param> <!--4、 可以在servlet类中通过getServletContext().getInitParameter("age"),获取对应的值22 --> <param-name>age</param-name> <param-value>25</param-value> </context-param> <servlet-mapping> <servlet-name>ServletTest02</servlet-name> <url-pattern>/test02</url-pattern> </servlet-mapping> </web-app>
java代码:
package com.hjh.day0307; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Enumeration; import java.util.Iterator; import java.util.Set; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletTest02 extends HttpServlet{ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest req,HttpServletResponse rep) throws ServletException,IOException{ //1、获取ServletContext对象,并在公共区域存放了一个键值对数据(key:name;) /*getServletContext().setAttribute("name", "hjh"); System.out.println(getServletContext().getAttribute("name"));//hjh System.out.println("---------------------------setAttribute()与getAttribute()------------------------------------------------"); */ //2、getServletContext().getInitParameter("name")获取web.xml文件中配置的name对应的value值置 /* System.out.println(getServletContext().getInitParameter("name"));//hjh System.out.println(getServletContext().getInitParameter("age"));//22 */ //3、通过指定的key,来删除内容,需配合中的方法使用,先设置后删除 /* getServletContext().removeAttribute("name"); System.out.println(getServletContext().getAttribute("name"));//null */ //4、通过getInitParameterNames()来获取在web.xml中配置的参数key,返回一个枚举类型,通过遍历枚举来获取对应的value置 /*Enumeration<String> en = getServletContext().getInitParameterNames(); while(en.hasMoreElements()) { String name = en.nextElement(); System.out.println(getServletContext().getInitParameter(name));//hjh 25 } */ //5、获取web项目指定资源的绝对路径,getServletContext().getRealPath("资源文件相对路径"); 输出为D: omcat-9.0webappsServlet_hjhWEB-INFweb.xml //System.out.println(getServletContext().getRealPath("/WEB-INF/web.xml")); //6、获取web项目下指定资源的内容,返回的是字节输入流。InputStream getResourceAsStream(java.lang.String path) /* InputStream ins = getServletContext().getResourceAsStream("/WEB-INF/web.xml"); //字节流转换成字符流,并指定编码格式为utf-8 InputStreamReader isr = new InputStreamReader(ins,"utf-8"); //使用字符缓冲流进行输出,有readine()方法,输出为web.xml文件内容 BufferedReader br = new BufferedReader(isr); String str = null; while((str = br.readLine())!=null) { System.out.println(str); } */ //getResourcePaths(相对路径),获取指定路径下的所有内容(文件和目录)。 Set set = getServletContext().getResourcePaths("/WEB-INF"); Iterator it = set.iterator(); while(it.hasNext()) { System.out.println(it.next()); } /*输出如下: * /WEB-INF/lib/ /WEB-INF/web1.xml /WEB-INF/classes/ /WEB-INF/web.xml * */ } public void doPost(HttpServletRequest req,HttpServletResponse rep) throws ServletException,IOException { System.out.println("doPost"); } }