第九章 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. 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);

}

}

 

  1. 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);

}

}

}

访问

第九章 Servlet API的更多相关文章

  1. 第九章 Servlet工作原理解析

    9.1 从Servlet容器说起    Servlet容器:Jetty, Tomcat等. 这里以Tomcat为例,  真正管理Servlet的容器是Context容器,一个Context对应一个WE ...

  2. 第九章 Servlet工作原理解析(待续)

    从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern

  3. Servlet工作原理解析 《深入分析java web 技术内幕》第九章

    参考关于servblet的相关文章 侧重概况:https://blog.csdn.net/levycc/article/details/50728921 ibm的相关:https://www.ibm. ...

  4. 精通Web Analytics 2.0 (11) 第九章: 新兴分析—社交,移动和视频

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 第九章: 新兴分析-社交,移动和视频 网络在过去几年中发生了不可思议的发展变化:从单向对话到双向对话的转变; 由视频,Ajax和 ...

  5. [Effective Java]第九章 异常

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 第九章 C语言在嵌入式中的应用

    上章回顾 编码的规范和程序版式 版权管理和申明 头文件结构和作用 程序命名 程序注释和代码布局规范 assert断言函数的应用 与0或NULL值的比较 内存的分配和释放细节,避免内存泄露 常量特性 g ...

  7. Laxcus大数据管理系统2.0(11)- 第九章 容错

    第九章 容错 在当前,由于集群庞大的组织体系和复杂性,以及用户普遍要求低成本硬件,使得集群在运行过程中发生的错误概率,远远高于单一且性能稳定的小型机服务器,并且集群在运行过程中几乎是不允许停止的,这就 ...

  8. [转]Windows Shell 编程 第九章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987969】

    第九章 图标与Windows任务条 如果问一个非程序人员Windows最好的特色是什么,得到的答案应该是系统最有吸引力的图标.无论是Windows98现在支持的通用串行总线(USB)还是WDM(看上去 ...

  9. jQuery第九章

    第九章 jQuery Mobile 一.HTML5.0简介 谈到Web设计,我们经常把Web分为三个层: (1)结构层:(2)表现层:(3)行为层. 对应的技术分别是: (1)HTML:(2)CSS: ...

随机推荐

  1. MySQL 与 MongoDB的操作对比

    MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方 ...

  2. ExtJs之Ext.XTemplate:数组填充,访问父对象

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  3. smartctl----硬盘状态监控

    smartmontools介绍 smartmontools是一款开源的磁盘控制,监视工具,可以运行在Linux,Unix,BSD,Solaris,Mac OS,OS/2,Cygwin和Windows上 ...

  4. BA-Honeywell R300系统

  5. centos编译ffmpeg x264

    1.安装汇编编译器(一般系统自带吧).假设没有依照以下的命令安装吧 yum install yasm 2.使用最新x264源代码编译(仅仅支持编码)    在x264官网下载最新的代码http://w ...

  6. 通达OA 小飞鱼工作流在线培训教程(一)HTML基础介绍

    应一些刚接触工作流设计朋友的要求,这里开设一个系列教程,对通达OA工作流设计相关的内容做个介绍.方便解决一些日常经常出现的问题,希望对刚刚接触这部分工作的朋友能够有些帮助. 工作流设计须要多方面的知识 ...

  7. XCODE插件 之 Code Pilot 无鼠标化

    什么是Code Pilot? Code Pilot 是一个 Xcode 5 插件.同意你不许使用鼠标就能高速地查找项目内的文件.方法和标识符. 它使用模糊查询匹配(fuzzy query matchi ...

  8. Gzip压缩优化网站

    网站常使用GZIP压缩算法对网页内容进行压缩,然后传给浏览器,以减小数据传输量,提高响应速度.浏览器接收到GZIP压缩数据后会自动解压并正确显示.GZIP加速常用于解决网速慢的瓶颈. 压缩Filter ...

  9. 1、libgdx简单介绍

    Libgdx 是一个跨平台和可视化的的开发框架.它当前支持Windows.Linux,Mac OS X.Android,IOS和HTML5作为目标平台. Libgdx同意你一次编写代码不经改动部署到多 ...

  10. ftk学习记(首篇)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 非常早之前就知道ftk了,当时主要是由于买了李先静的书,所以知道了这么一个项目.由于对这样的g ...