Servlet 学习
Posted jyy599
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet 学习相关的知识,希望对你有一定的参考价值。
ServletConfig讲解
1.1、配置Servlet初始化参数
在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
Posted jyy599
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet 学习相关的知识,希望对你有一定的参考价值。
在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
<servlet>
2 <servlet-name>ServletConfigDemo1</servlet-name>
3 <servlet-class>gacl.servlet.study.ServletConfigDemo1</servlet-class>
4 <!--配置ServletConfigDemo1的初始化参数 -->
5 <init-param>
6 <param-name>name</param-name>
7 <param-value>gacl</param-value>
8 </init-param>
9 <init-param>
10 <param-name>password</param-name>
11 <param-value>123</param-value>
12 </init-param>
13 <init-param>
14 <param-name>charset</param-name>
15 <param-value>UTF-8</param-value>
16 </init-param>
17 </servlet>
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,我们通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
例如:
package gacl.servlet.study;
2
3 import java.io.IOException;
4 import java.util.Enumeration;
5 import javax.servlet.ServletConfig;
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 public class ServletConfigDemo1 extends HttpServlet {
12
13 /**
14 * 定义ServletConfig对象来接收配置的初始化参数
15 */
16 private ServletConfig config;
17
18 /**
19 * 当servlet配置了初始化参数后,web容器在创建servlet实例对象时,
20 * 会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,
21 * 将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以
22 * 得到当前servlet的初始化参数信息。
23 */
24 @Override
25 public void init(ServletConfig config) throws ServletException {
26 this.config = config;
27 }
28
29 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
31 //获取在web.xml中配置的初始化参数
32 String paramVal = this.config.getInitParameter("name");//获取指定的初始化参数
33 response.getWriter().print(paramVal);
34
35 response.getWriter().print("<hr/>");
36 //获取所有的初始化参数
37 Enumeration<String> e = config.getInitParameterNames();
38 while(e.hasMoreElements()){
39 String name = e.nextElement();
40 String value = config.getInitParameter(name);
41 response.getWriter().print(name + "=" + value + "<br/>");
42 }
43 }
44
45 public void doPost(HttpServletRequest request, HttpServletResponse response)
46 throws ServletException, IOException {
47 this.doGet(request, response);
48 }
49
50 }
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LifeServlet extends HttpServlet{
public LifeServlet() {
System.out.println("LifeServlet‘s constructor");
}
@Override
public void init() throws ServletException {
System.out.println("LifeServlet‘s init()");
}
@Override
public void destroy() {
System.out.println("LifeServlet‘s destroy()");
}
public void doGet(HttpServletRequest request,HttpServletResponse response){
System.out.println("LifeServet‘s service");
/*
* 通过继承自GenericServlet的方法获得ServletConfig对象.
*/
ServletConfig config=getServletConfig();
/*
* 读取初始化参数
*/
String company=config.getInitParameter("company");
System.out.println("公司:"+company);
}
}
运行结果如下:
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
范例:ServletContextDemo1和ServletContextDemo2通过ServletContext对象实现数据共享
package gacl.servlet.study;
2
3 import java.io.IOException;
4 import javax.servlet.ServletContext;
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 public class ServletContextDemo1 extends HttpServlet {
11
12 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14 String data = "xdp_gacl";
15 /**
16 * ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,
17 * 可以通过ServletConfig.getServletContext方法获得ServletContext对象。
18 */
19 ServletContext context = this.getServletConfig().getServletContext();//获得ServletContext对象
20 context.setAttribute("data", data); //将data存储到ServletContext对象中
21 }
22
23 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
25 doGet(request, response);
26 }
27 }
package gacl.servlet.study; 2 3 import java.io.IOException; 4 import javax.servlet.ServletContext; 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 public class ServletContextDemo2 extends HttpServlet { 11 12 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { 14 ServletContext context = this.getServletContext(); 15 String data = (String) context.getAttribute("data");//从ServletContext对象中取出数据 16 response.getWriter().print("data="+data); 17 } 18 19 public void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 doGet(request, response); 22 } 23 } 复制代码
先运行ServletContextDemo1,将数据data存储到ServletContext对象中,然后运行ServletContextDemo2就可以从ServletContext对象中取出数据了,这样就实现了数据共享,如下图所示:
在web.xml文件中使用<context-param>标签配置WEB应用的初始化参数,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
2 <web-app version="3.0" 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
3 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
4 <display-name></display-name>
5 <!-- 配置WEB应用的初始化参数 -->
6 <context-param>
7 <param-name>url</param-name>
8 <param-value>jdbc:mysql://localhost:3306/test</param-value>
9 </context-param>
10
11 <welcome-file-list>
12 <welcome-file>index.jsp</welcome-file>
13 </welcome-file-list>
14 </web-app>
获取Web应用的初始化参数,代码如下:
package gacl.servlet.study;
2
3 import java.io.IOException;
4 import javax.servlet.ServletContext;
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 ServletContextDemo3 extends HttpServlet {
12
13 public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
15
16 ServletContext context = this.getServletContext();
17 //获取整个web站点的初始化参数
18 String contextInitParam = context.getInitParameter("url");
19 response.getWriter().print(contextInitParam);
20 }
21
22 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
24 doGet(request, response);
25 }
26
27 }
以上是关于Servlet 学习的主要内容,如果未能解决你的问题,请参考以下文章