Servlet常用类剖析

x33g5p2x  于2022-02-07 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(253)

通过继承HttpServlet实现Servlet程序

使用idea创建Servlet程序

Servlet的继承体系

doGet和dopost源码

servlet方法部分源码

ServletConfig类

ServletConfig类的三大作用

ServletContext类

什么是ServletContext?

什么是域对象?

对照示意表:

ServletContext类的四个作用

1、获取web.xml中的配置的上下文参数,

易错点:

获取当前工程路径

获取部署后在服务器硬盘上的绝对路径

map一样存取数据

通过继承HttpServlet实现Servlet程序

实际开发中,一般使用继承HttpServlet类的方法去实现Servlet程序。

步骤:
1、编写一个类去继承HttpServlet类

2、根据业务需要重写doGet或doPost方法

3、到web.xml中配置Servlet程序

1、编写一个类,Alt+insert快捷键重写里一些需要的方法

  1. import javax.servlet.ServletException;
  2. import javax.servlet.http.HttpServlet;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.io.IOException;
  6. public class HelloServlet2 extends HttpServlet {
  7. @Override
  8. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  9. // super.doGet(req, resp);
  10. System.out.println("HelloServlet2的 get请求");
  11. }
  12. @Override
  13. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  14. // super.doPost(req, resp);
  15. System.out.println("HelloServlet2的 post请求");
  16. }
  17. }

到web.xml文件中配置访问路径

  1. <servlet>
  2. <servlet-name>HelloServlet2</servlet-name>
  3. <servlet-class>com.servlet.HelloServlet2</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>HelloServlet2</servlet-name>
  7. <url-pattern>/hello2</url-pattern>
  8. </servlet-mapping>

将表单中的访问地址hello改变为hello2

运行提交后:

 使用idea创建Servlet程序

选择要实现的包→Servlet程序

配置信息

勾选上用的就是3.0的注解配置 

只需要在web.xml中加上路径即可(其他的已经自动生成了)

  1. <servlet-mapping>
  2. <servlet-name>HelloServlet3</servlet-name>
  3. <url-pattern>/hello3</url-pattern>
  4. </servlet-mapping>

Servlet的继承体系

 doGet和dopost源码

  1. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2. String msg = lStrings.getString("http.method_get_not_supported");
  3. this.sendMethodNotAllowed(req, resp, msg);
  4. }
  1. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2. String msg = lStrings.getString("http.method_post_not_supported");
  3. this.sendMethodNotAllowed(req, resp, msg);
  4. }

 servlet方法部分源码

  1. protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  2. String method = req.getMethod();
  3. long lastModified;
  4. if (method.equals("GET")) {
  5. lastModified = this.getLastModified(req);
  6. if (lastModified == -1L) {
  7. this.doGet(req, resp);
  8. } else {
  9. long ifModifiedSince;
  10. try {
  11. ifModifiedSince = req.getDateHeader("If-Modified-Since");
  12. } catch (IllegalArgumentException var9) {
  13. ifModifiedSince = -1L;
  14. }
  15. if (ifModifiedSince < lastModified / 1000L * 1000L) {
  16. this.maybeSetLastModified(resp, lastModified);
  17. this.doGet(req, resp);
  18. } else {
  19. resp.setStatus(304);
  20. }
  21. }
  22. }

ServletConfig类

  • ServletConfig是Servlet程序的配置信息类。
  • Servlet程序和ServletConfig对象都是有Tomcat负责创建,我们负责使用。
  • Servlet程序默认是一次访问的时候创建,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象

ServletConfig类的三大作用

1、可以获取Servlet程序的别名(servlet-name的值)

2、获取初始化参数init-param

3、获取ServletContent对象

所处位置init方法

**1、**获取servlet别名

  1. public void init(ServletConfig servletConfig) throws ServletException {
  2. System.out.println("HelloServlet 的别名是"+servletConfig.getServletName());
  3. }

**2、**获取初始化参数init-param,其他的前面谢过了

现在web.xml文件中配<init-param>

  1. <!--servlet标签给Tomcat配置Servlet程序-->
  2. <servlet>
  3. <!-- servlet-name给Servlet程序起一个别名(一般别名起为类名)-->
  4. <servlet-name>HelloServlet</servlet-name>
  5. <!--servlet-class是Servlet程序的全类名 -->
  6. <servlet-class>com.servlet.HelloServlet</servlet-class>
  7. <!--init-param时初始化参数,这是个键值对可以写很多对 -->
  8. <init-param>
  9. <!-- 参数名-->
  10. <param-name>username</param-name>
  11. <!--是参数的值 -->
  12. <param-value>root</param-value>
  13. </init-param>
  14. <init-param>
  15. <!-- 参数名-->
  16. <param-name>url</param-name>
  17. <!--是参数的值 -->
  18. <param-value>jdbc:mysql://localhost:3306/text</param-value>
  19. </init-param>
  20. </servlet>

在实现Servlet接口的类中的init()下

  1. // 2、获取初始化参数init-param
  2. System.out.println("初始化参数username的值是"+servletConfig.getInitParameter("username"));
  3. System.out.println("初始化参数url的值是"+servletConfig.getInitParameter("url"));

**3、**获取ServlertContent对象

  1. // 3、获取ServletContent对象
  2. System.out.println("servletcontent对象是"+servletConfig.getServletContext());

以上运行结果:

每一个ServletConfig程序独立 ,在web.xml 中每个类中信息不共享,

重写init方法时得加上super.init(config),得访问父类的init初始化方法,否则报错

继承体系可以得知ServletConfig在GenericServlet类中,该类中的init定义:

  1. public void init(ServletConfig config) throws ServletException {
  2. this.config = config;
  3. this.init();
  4. }

所以如果重写init方法,必须加上super.init(config),否则父类的init方法不会执行(父类中中的保存操作执行不了)

ServletContext类

什么是ServletContext?

1、ServletContent是一个接口,他表示Servlet上下文对象

2、一个web工程,只有一个ServletContext对象实例。

3、ServletContent对象是一个域对象。

4、在web工程启动后创建,在web工程结束后销毁

什么是域对象?

域对象,是可以像Map一样存取数据的对象,叫做域对象。

这里的域指的是存取数据的操作范围。

对照示意表:

| | 存数据 | 取数据 | 删除数据 |
| Map | put() | get() | remove() |
| 域对象 | setAttribute() | getAttribute() | removeAttribute() |

ServletContext类的四个作用

1、获取web.xml中的配置的上下文参数context-param

2、获取当前的工程路径你,格式:/工程路径

3、获取部署后在服务器硬盘上的绝对路径

4、像Map一样存取数据

用快捷方式创建一个类ContextServlet,在web.xml中配置路径(   其余的已经自动生成了)

  1. <servlet>
  2. <servlet-name>ContextServlet</servlet-name>
  3. <servlet-class>com.servlet.ContextServlet</servlet-class>
  4. </servlet>
  5. <!-- 上面自动生成了,下面要自己书写的路径-->
  6. <servlet-mapping>
  7. <servlet-name>ContextServlet</servlet-name>
  8. <url-pattern>/contextServlet</url-pattern>
  9. </servlet-mapping>

1、获取web.xml中的配置的上下文参数,

首先得在web.xml中配置context-param(一般写在其他servlet之上)

  1. <!--context-param是上下文参数(他属于整个web工程)-->
  2. <context-param>
  3. <!--参数名 -->
  4. <param-name>username</param-name>
  5. <!--参数值-->
  6. <param-value>context</param-value>
  7. </context-param>
  8. <!--可以写多对上下文参数-->
  9. <context-param>
  10. <param-name>password</param-name>
  11. <param-value>root</param-value>
  12. </context-param>

在ContextServlet类中doGet()方法

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. //1、获取web.xml中的配置的上下文参数context-param
  3. //获取对象
  4. ServletContext context = getServletConfig().getServletContext();
  5. String username = context.getInitParameter("username");
  6. String password = context.getInitParameter("password");
  7. System.out.println("context-param参数username的值是"+username);
  8. System.out.println("context-param参数password的值是"+password);
  9. }
  10. }

运行之后结果是: 

 易错点:

①:在配置web.xml文件中地址中的/斜杆不要忘记,否则会报地址无效

  1.  <url-pattern>/contextServlet</url-pattern>

②:在类中获取参数是应在doGet()方法中书写,否则运行之后访问地址不会显示处对应的信息

获取当前工程路径

  1. System.out.println("当前工程路径:"+context.getContextPath());

获取部署后在服务器硬盘上的绝对路径

  1. System.out.println("获取工程部署的路径"+context.getRealPath("/"));

这个斜杆/表示到当前工程的路径

map一样存取数据

创建一个Servlet类,在web.xml文件中配置好路径

  1. <servlet>
  2. <servlet-name>ContextServlet1</servlet-name>
  3. <servlet-class>com.servlet.ContextServlet1</servlet-class>
  4. </servlet>
  5. <!-- 以上自动配好-->
  6. <servlet-mapping>
  7. <servlet-name>ContextServlet1</servlet-name>
  8. <url-pattern>/contextServlet1</url-pattern>
  9. </servlet-mapping>

在类中:

  1. public class ContextServlet1 extends HttpServlet {
  2. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. //获取ServletContext对象
  4. ServletContext context = getServletContext();
  5. System.out.println("保存之前Context获取key1的值是:"+context.getAttribute("key1"));
  6. context.setAttribute("key1","value1");
  7. System.out.println("context获取数据key1的值为:"+context.getAttribute("key1"));
  8. }
  9. }

我们直接可以用getServletContext()获取对象,这样简单些,而不用getServletConfig().getServletContext();

我们ctrl+b看源码可知:

  1. public ServletContext getServletContext() {
  2. return this.getServletConfig().getServletContext();
  3. }

源代码已经做好了那步,所以我们直接返回即可。

我们继续定义一个类叫ContextServlet2

  1. public class ContextServlet2 extends HttpServlet {
  2. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. ServletContext context = getServletContext();
  4. System.out.println("ContextServlet2中 context获取数据key1的值为:"+context.getAttribute("key1"));
  5. }
  6. }

部署就省略了,运行之后

不难发现当ContextServlet1中的有数据存入,ContextServlet2中就可以查到该数据。也可以context下的数据是共享数据

ContextServlet对象在web工程启动后创建,在web工程结束后销毁,且共享

重启和重新部署都会导致web工程销毁

ContextServlet1、2的地址也是一样的

相关文章