文章目录
概要
web.xml
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Servlet01</servlet-name>
<servlet-class>com.hailuo.servlet.Servlet01</servlet-class>
<init-param>
<param-name>webSite</param-name>
<param-value>www.baidu.com</param-value>
</init-param>
<init-param>
<param-name>email</param-name>
<param-value>hr@163.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Servlet01</servlet-name>
<url-pattern>/servlet01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet02</servlet-name>
<servlet-class>com.hailuo.servlet.Servlet02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet02</servlet-name>
<url-pattern>/servlet02</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet03</servlet-name>
<servlet-class>com.hailuo.servlet.Servlet03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet03</servlet-name>
<url-pattern>/servlet03</url-pattern>
</servlet-mapping>
<context-param>
<param-name>webSite</param-name>
<param-value>www.itshixun.com</param-value>
</context-param>
<context-param>
<param-name>email</param-name>
<param-value>admin@itshixun.com</param-value>
</context-param>
</web-app>
ServletConfig介绍
Servlet容器初始化一个Servlet类型的对象时,会为这个Servlet对象创建一个ServletConfig对象。在ServletConfig对象中包含了Servlet的初始化参数信息。此外,ServletConfig对象还与ServletContext对象关联。Jsp/Servlet容器在调用Servlet对象的init(ServletConfig config)方法时,会把ServletConfig类型的对象当做参数传递给servlet对象。Init(ServletConfig config)方法会使得当前servlet对象与ServletConfig类型的对象建立关联关系。
ServletConfig实例
java
package com.hailuo.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Servlet01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
ServletConfig servletConfig = getServletConfig();
String webSite = servletConfig.getInitParameter("webSite");
String email = servletConfig.getInitParameter("email");
PrintWriter writer = response.getWriter();
writer.println("<h1>"+webSite+"</h1>");
writer.println("<h1>"+email+"</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
访问结果
ServletConfig细节
1、每当前台请求时获取一个servlet则将该ServletConfig对象装载到servlet父类中去(在init方法获取),它有一个config属性,用来存储该对象。
2、ServletContext可以从ServletConfig对象来获取。
ServletContext介绍
ServletContext 是 servlet 与 servlet 容器之间直接通信的接口。当 servlet 容器启动一个 Web 工程时,会为该工程创建一个 ServletContext 对象,即 servlet 的上下文环境。每个 Web 工程都有一个唯一的 ServletContext 对象。同一个 Web 工程中的所有 servlet 实例共享同一个 ServletContext 对象,通过这个对象,servlet 可以访问容器中的各种资源。
ServletContext实例
java
package com.hailuo.servlet;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
public class Servlet02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html;charset=UTF-8");
ServletContext servletContext = getServletContext();
Enumeration<String> names=super.getServletContext().getInitParameterNames();
String webSite = servletContext.getInitParameter("webSite");
String email = servletContext.getInitParameter("email");
System.out.println(webSite+"-"+email);
PrintWriter out=response.getWriter();
while(names.hasMoreElements())
{
String name=names.nextElement();
out.print(name+"<br>");
}
out.println(webSite+"<br>");
out.println(email+"<br>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
访问结果
ServletContext细节
1、 ServletContext 是一个接口, 它表示 Servlet 上下文对象
2、一个 web 工程,只有一个 ServletContext 对象实例
3、ServletContext 对象 是在 web 工程启动的时候创建,在 web 工程停止的时销毁
4、 ServletContext 对象可以通过 ServletConfig.getServletContext 方法获得对 ServletContext对象的引用,也可以通过 this.getServletContext()来获得其对象的引用。
5、由于一个 WEB 应用中的所有 Servlet 共享同一个 ServletContext 对象,因此 Servlet 对象之间可以通过 ServletContext 对象来实现多个 Servlet 间通讯(比如用来统计服务被访问次数)。ServletContext 对象通常也被称之为域对象。
ServletContext获得服务访问次数(可拓展)
java
package com.hailuo.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Servlet03 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
Object visit_count = servletContext.getAttribute("visit_count");
if (visit_count == null) {//说明是第 1 次访问网站
servletContext.setAttribute("visit_count", 1);
visit_count = 1;
} else { //是第二次或以后
//取出 visit_count 属性的值+1
visit_count = Integer.parseInt(visit_count + "") + 1;
//放回到 servletContext
servletContext.setAttribute("visit_count", visit_count);
}
//另一种方式Integer visit_count = WebUtils.visitCount(servletContext);
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.print("<h1>新网站被访问的次数是" + visit_count + "</h1>");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
总结
我亦无他,惟手熟尔