封装的ServLet及servletContext

说明

 原生的ServLet在实际生活中很少使用,可当作了解其发展历程使用。

步骤

封装的ServLet:

  1. 新建Dynamic Web Project
  2. 在src上,右键 new ——> Servlet
  3. 根据实际情况配置
  4. 之后我们会得到一个封装好的ServLet
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    package com.rexyan.servlet_auto;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    /**
    * Servlet implementation class servlet_auto_create
    */
    public class servlet_auto_create extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public servlet_auto_create() {
    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);
    }

    }

创建自己的ServLet模版

 我们可以创建自己的ServLet模版
window ——> Preferences ——> 搜索templates ———> 找到Java、Editor下在templates,右边选择new一个,名称为“severlet”,描述为“自定义severlet模版”,内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package ${enclosing_package};
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 ${primary_type_name} extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Rex");
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

用ServLet模版生成

 这是我们可以删除之前自动生成的ServLet,使用我们自己的模版创建的ServLet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.rexyan.servlet2;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServLet2 extends HttpServlet {
public void init(){
// 服务器创建一个ServLet的实例,init就会执行
ServletContext servletContext = this.getServletContext();
servletContext.setAttribute("test_name", 3);
//往servletContext中设置一个名为test_name,值为3的属性
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Rex");
// 返回Rex
ServletContext servletContext = this.getServletContext();
// 获取servletContext对象【此对象是全局的,只有一个,针对整个Web项目的,不像ServLet是可以有很多个】
System.out.println(servletContext.getInitParameter("Driver"));
// 获取web.xml配置,需要在web.xml中配置context-param
System.out.println(servletContext.getRealPath("/1.txt"));
// 获取文件的绝对路径,传入参数为相对路径
System.out.println(servletContext.getAttribute("test_name"));
// 输出servletContext中属性为test_name的值
servletContext.setAttribute("test_name", 1);
// 设置新的值
System.out.println(servletContext.getAttribute("test_name"));
// 再次输出servletContext中属性为test_name的值
System.out.println("getServerInfo" + servletContext.getServerInfo());
// 输出servletContext的一些信息
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 在POST中可以调用GET方法
doGet(request, response);
}
}

配置web.xml

 web.xml的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServerLet2</display-name>

<context-param>
<param-name>Driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>

<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>
<servlet>
<description></description>
<display-name>ServLet2</display-name>
<servlet-name>ServLet2</servlet-name>
<servlet-class>com.rexyan.servlet2.ServLet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServLet2</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
</web-app>