第九章 Servlet API
Servlet API 定义了Servlet和服务器之间的一个标准接口,这使得Servlet具有跨应用服务器的特性,通过使用Servlet API,开发人员不必关心服务器的内部运作方式。
Servlet API由两个软件包组成:Javax.servlet包和Javax.servlet.http包
一个存放与HTTP协议无关的一般性Servlet类,另一个存放与HTTP协议相关的功能的类。这两个软件包位于Tomcat的servlet-api.jar中,在myeclipse中创建项目的时候引入Java EE 6也是可以的。
- 1. servletConfig接口
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,程序员通过ServletConfig对象就可以得到当前servlet的初始化参数信息
从一个servlet实例化后,对任何客户端任何时候访问都有效,只对本servlet有效,一个servlet的ServletConfig不能被其他sevlet访问
案例:
配置:通过web.xml文件中的<init-param>标签进行设置,
<servlet> <servlet-name>ServletConfigDemo1</servlet-name> <servlet-class>com.silvan.servlet.ServletConfigDemo1</servlet-class> <init-param> <param-name>name</param-name> <param-value>ross</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ServletConfigDemo1</servlet-name> <url-pattern>/ServletConfigDemo1</url-pattern> </servlet-mapping> |
获取:在servlet实现类里面通过getServletConfig().getInitParameter("name")获取;
package com.silvan.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletConfigDemo1 extends HttpServlet { /** * Constructor of the object. */ public ServletDemo1() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = getServletConfig().getInitParameter("name"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(name); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { String name = getServletConfig().getInitParameter("name"); System.out.println("this is "+name); } } |
- 2. ServletContext接口
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
ServletContext对任何servlet,任何人在任何时间都有效,是真正的全局对象
案例:
配置:通过web.xml文件中的<context-param>标签设置
<context-param> <param-name>name</param-name> <param-value> ross</param-value> </context-param> <servlet> <servlet-name>ServletContextDemo1</servlet-name> <servlet-class>com.silvan.servlet.ServletContextDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletContextDemo1</servlet-name> <url-pattern>/ServletContextDemo1</url-pattern> </servlet-mapping> |
获取: getServletContext().getInitParameter(“name”);
getServletConfig().getServletContext().getInitParameter(“name”);
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name1 = getServletContext().getInitParameter("name"); String name2 = getServletConfig().getServletContext().getInitParameter("name"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print("直接getServletContext()取得name为:" +name1); out.println("<br/>"); out.print("getServletConfig()对象中取得name为:"+name2); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } |
显示
案例:
统计站点访问次数
配置web.xml
<servlet> <servlet-name>ServletContextDemo21</servlet-name> <servlet-class>com.silvan.servlet.ServletContextDemo21</servlet-class> </servlet> <servlet> <servlet-name>ServletContextDemo22</servlet-name> <servlet-class>com.silvan.servlet.ServletContextDemo22</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletContextDemo21</servlet-name> <url-pattern>/ServletContextDemo21</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ServletContextDemo22</servlet-name> <url-pattern>/ServletContextDemo22</url-pattern> </servlet-mapping> |
统计访问次数
ServletContextDemo21.java
package com.silvan.servlet; import java.io.IOException; import java.io.PrintWriter; 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 ServletContextDemo21 extends HttpServlet { /** * Constructor of the object. */ public ServletContextDemo21() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 每次访问获得原来访问次数+1 ServletContext context = getServletContext(); // 获得访问次数 int count = (Integer) context.getAttribute("count"); // 次数+1保存 context.setAttribute("count", ++count); System.out.println("context对象地址:"+context); System.out.println("网站被访问次数为:" + count); // 浏览器显示中文 response.setContentType("text/html;charset=utf-8"); response.getWriter().println("context对象地址:" + context+"<br/>"); response.getWriter().println("Demo1网站被访问次数为:" + count); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet( request, response); } public void init() throws ServletException { // 在ServletContext对象中保存一个访问次数 ServletContext context = getServletContext(); if(context.getAttribute("count")==null){ // 保存一个数据 context.setAttribute("count", 0); } } } |
ServletContextDemo22.java
package com.silvan.servlet; import java.io.IOException; import java.io.PrintWriter; 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 ServletContextDemo22 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 每次访问获得原来访问次数+1 ServletContext context = getServletContext(); // 获得访问次数 int count = (Integer) context.getAttribute("count"); // 次数+1保存 context.setAttribute("count", ++count); System.out.println("context对象地址:"+context); System.out.println("网站被访问次数为:" + count); // 浏览器显示中文 response.setContentType("text/html;charset=utf-8"); response.getWriter().println("context对象地址:" + context+"<br/>"); response.getWriter().println("Demo1网站被访问次数为:" + count); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet( request, response); } public void init() throws ServletException { // 在ServletContext对象中保存一个访问次数 ServletContext context = getServletContext(); if(context.getAttribute("count")==null){ // 保存一个数据 context.setAttribute("count", 0); } } } |
访问