转自:http://www.cnblogs.com/xujian2014/p/4536168.html

一、摘要

  本文主要简单介绍开发Servlet需要用到的接口和类。

二、ServletRequest和ServletResponse接口

  当客户请求到来时,由容器创建一个ServletRequest对象,封装请求数据,同时创建一个ServletResponse对象,封装响应数据。这两个对象作为参数传递给service方法。

  这两个接口都用很多方法,这里就不逐一介绍。

  HttpServletRequest和HttpServletResponse分别继承自ServletRequest和ServletResponse接口。

三、ServletConfig接口

  Servlet容器使用ServletConfig对象在Servlet初始化期间向它传递配置信息,一个Servlet对象只有一个|ServletConfig对象。该接口中定义下述四个方法:

      //返回名字为name的初始化参数的值
String getInitParameter(String name);
//返回所有初始化参数的的名字的枚举集合
Enumeration getInitParameterNames() ;
//返回Servlet上下文对象的引用
ServletContext getServletContext();
//返回Servlet实例的名字
String getServletName() ;

  该几个方法的简单例子代码如下:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//返回名字为name的初始化参数的值
System.out.println(getServletConfig().getInitParameter("name"));
//返回所有初始化参数的的名字的枚举集合
Enumeration<String> paraNames=this.getServletConfig().getInitParameterNames();
for(Enumeration e=paraNames;e.hasMoreElements();)
{
String name=e.nextElement().toString();
String value=getServletConfig().getInitParameter(name);
System.out.println(name+"-----"+value);
}
//返回Servlet上下文对象的引用
System.out.println(getServletConfig().getServletContext().getContextPath());
//返回Servlet实例的名字
System.out.println(getServletConfig().getServletName()) ;
}

  输出结果为:

  

四、ServletContext接口

  ServletContext接口用来表示上下文,该接口定义一组方法,Servlet可以使用这些方法与它的Servlet容器进行通信。ServletContext对象是Web服务器中一个已知路径的根。我们可以通过ServletConfig对象的getServletContext方法来得到ServletContext对象,也可以调用GenericServlet类的getServletContext方法得到ServletContext对象。

  一个Web应用程序只有一个ServletContext对象,该对象可以被Web应用程序的所有Servlet所访问,因此通常使用Servlet对象保存一些需要在Web应用程序中共享的信息。eg,统计页面访问量的小例子,代码如下:

public class ContextDemo extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
ServletContext context=getServletContext();
Integer count=null;
synchronized(context)
{
count=(Integer)context.getAttribute("counter");
if(count==null)
{
count=new Integer(1);
}
else
{
count=new Integer(count.intValue()+1);
}
context.setAttribute("counter", count);
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.println("<html><head><title>页面访问统计</title></head><body>");
out.println("该页面已被访问了"+"<b>"+count+"</b>"+"次");
out.println("</body></html>");
out.close();
}

五、RequestDispatcher接口

  RequestDispatcher对象用于封装一个由路径所标识的服务器资源,利用RequestDispatcher对象可以将请求转发给其他的Servlet或者JSP页面。

  • 获取RequestDispatcher对象

  两种方法可以获取该对象,一种是从ServletRequest接口的getRequestDispatcher方法获取,另一种是从ServletContext接口的getRequestDispatcher获取。两者的区别是参数的资源的路径名,前者不但可以是相对于上下文根的路径名,而且可以是相对于当前Servlet的路径,而后者只能是是相对于上下文根的路径名。

  • 请求转发方法

  在RequestDispatcher接口中定义了两种方法:

  forward(req,resp)和include(req,resp),两者的区别是前者将请求转发给其他的Servlet,将由被调用的Servlet负责对请求作出相应,而原先Servlet的执行则终止。而后者将调用的Servlet对请求作出的响应并入原先的响应对象中,原先的Servlet还可以继续输出响应信息。示例代码如下:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//该方法用于在响应中包含其他资源的内容并从ServletContext接口得到RequestDispatcher对象
/*RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/hello.html");
rd.include(request, response);*/ //该方法用于将请求从一个Servlet传递给服务器上另外的Servlet、JSP页面或者是HTML文件。
/*RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/hello.html");
rd.forward(request, response);*/ //从ServletRequest接口得到RequestDispatcher对象
RequestDispatcher rd=request.getRequestDispatcher("hello.html");
rd.include(request, response);
}

  转自:http://www.cnblogs.com/xujian2014/p/4536168.html

Servlet基本用法二接口和类的更多相关文章

  1. Java Servlet DAO实践(二)

    Java Servlet DAO实践(二) DAO连接类 package com.seller.servlets.dao; import java.sql.*; public class DataBa ...

  2. Servlet基本用法(二)接口和类

    一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletRequest和ServletResponse接口 当客户请求到来时,由容器创建一个ServletRequest对象 ...

  3. javaWeb学习总结(3)- Servlet总结(servlet的主要接口、类)

    Servlet总结01——servlet的主要接口.类 (一)servlet类 Servlet主要类.接口的结构如下图所示: 要编写一个Servlet需要实现javax.servlet.Servlet ...

  4. Servlet常用的接口和类

    使用接口和类的作用:Servlet也是依靠继承父类和实现接口来实现的.使用Servlet必须要引入两个包:javax.servlet和javax.servlet.http.所有的Servlet应用都是 ...

  5. Servlet API遍程常用接口和类

    本文主要总结Servlet  API遍程常用接口和类 Servlet API http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html ...

  6. Servlet基础之一:Servlet基本接口与类

    1.概述 Servlet API中共有5个包,约113个接口和类: javax.servlet javax.servlet.http javax.servlet.jsp javax.servlet.a ...

  7. JavaWeb学习——Servlet相关的接口和类

    JavaWeb学习——Servlet相关的接口和类 摘要:本文主要学习了Servlet相关的接口和类. Servlet的接口和类 三种方式 实现Servlet有三种方式: 实现javax.servle ...

  8. Spring常用的接口和类(二)

    七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...

  9. Servlet的接口实现类

    Sun公司提供了两个默认实现类 GenericServlet和HttpServlet HttpServlet指能够处理Http请求的Servlet,它在原有的Servlet基础上添加了与HTTp相关的 ...

随机推荐

  1. 通过OpenGL ES在iOS平台实践增强现实

    http://www.cnblogs.com/elvisyzhao/p/3398250.html 本文采用OpenGL ES 1固定渲染管线实现,目标为在设备拍摄到的现实世界中,绘制世界坐标轴,并根据 ...

  2. Linux内存管理图解【转】

    转自:http://www.360doc.com/content/13/0505/15/12218157_283128759.shtml Linux内存管理图解 2013-05-05  果儿的百科   ...

  3. Linux内核驱动之延时---内核超时处理【转】

    转自:http://blog.chinaunix.net/uid-24219701-id-3288103.html 内核超时处理 jiffies 计数器 定时器中断由系统定时硬件以规律地间隔产生; 这 ...

  4. (10)ubuntu内核源码树

    ubuntu内核源码树目录: root@ubuntu:/lib/modules/3.13.0-32-generic/build#

  5. linux下java.io.IOException: Cannot run program "/opt/jdk/jre/bin/java": error=13, Permission denied

    linux下启动jetty时报: [root@mv01 jetty-distribution-9.2.14.v20151106]# java -jar start.jar java.io.IOExce ...

  6. 用原生javascript实现最简单的发布者-订阅者模式

    http://www.cnblogs.com/surahe/p/6065778.html 发布—订阅模式可以广泛应用于异步编程中,这是一种替代传递回调函数的方案.比如,我们可以订阅 ajax 请求的 ...

  7. [BZOJ2084][Poi2010]Antisymmetry 二分+hash

    2084: [Poi2010]Antisymmetry Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 812  Solved: 503[Submit] ...

  8. 计蒜客 28437.Big brother said the calculation-线段树+二分-当前第k个位置的数 ( ACM训练联盟周赛 M)

    M. Big brother said the calculation 通过线段树维护. 这个题和杭电的一道题几乎就是一样的题目.HDU5649.DZY Loves Sorting 题意就是一个n的排 ...

  9. Go的50坑:新Golang开发者要注意的陷阱、技巧和常见错误[2]

    初级篇 开大括号不能放在单独的一行 未使用的变量 未使用的Imports 简式的变量声明仅可以在函数内部使用 使用简式声明重复声明变量 偶然的变量隐藏Accidental Variable Shado ...

  10. lua的table.sort

    local aa = {{a=11},{a=44},{a=33},{a=2} } table.sort(aa,function(a,b) return a.a>b.a end) for k, v ...