一、摘要

  本文主要简单介绍开发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);
}

  

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

  1. Servlet基本用法二接口和类

    转自:http://www.cnblogs.com/xujian2014/p/4536168.html 一.摘要 本文主要简单介绍开发Servlet需要用到的接口和类. 二.ServletReques ...

  2. Java Servlet DAO实践(二)

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

  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. c++ 常用数据类型,命名规则, 不常有数据类型

    1. 常用数据类型 最大值0111111111111111 = 32767最小值1000000000000000 = -32768 short 最低16位 2**7 - 1 负值:反码 int 至少和 ...

  2. 让你的MyEclipse像Visual Studio 2008一样拥有强大功能智能感知功能

    Windows→Preferences→Java→Editor→Content Assist 我们看到其中的AutoActivation Delay默认值为200(单位是毫秒)也就是说在打“.”之后停 ...

  3. 于erlang依赖的linux调优

    [皇室]杭州-sunface(61087682) 上午 9:42:02 http://docs.basho.com/riak/latest/ops/tuning/linux/ 这篇文章对于erlang ...

  4. Business vs Technology

    Business 只是 Technology的子集. Business只是体现在Code中. 比如说是HTML页面中内容的一部分. 或者说业务是对HTML组成的内容的分类. 比如说Smallbusin ...

  5. asp.net网页中添加年月日时分秒星期。

    html代码如下: 现在是<span id="TimeSpan"></span> <script type="text/javascript ...

  6. [AngularJS] tips技巧收集

     单个参数调用angular.module(),用来取得该模块而不是定义新的 这样就可以在不同的地方定义app的controller而不用通过全局变量 angular.module('myapp') ...

  7. 【WP8】扩展CM的INavigationService方法

    CM支持通过ViewModel进行导航,并通过支持参数传递,但是内部只是通过反射的方式构造Uri的参数进行导航,所以只支持简单类型的参数传递,下面对其进行扩展,在页面导航时支持复杂类型的参数传递,并扩 ...

  8. 《构建高性能web站点》读书笔记:CPU/IO并发策略

    服务器并发处理能力:单位时间内处理的请求数,吞吐率,reqs/s apache的mod_status,显示的 requests/sec,从启动开始的平均计算值.lighttpd的mod_status显 ...

  9. Unity3D热更新LuaFramework入门实战

    http://blog.sina.com.cn/s/blog_6788cd880102w8qy.html http://www.manew.com/thread-91845-1-1.html http ...

  10. [Err] 1231 - Variable 'sql_mode' can't be set to the value of 'NULL

    在MYSQL还原语句的时候,报: [Err] - Variable 'sql_mode' can't be set to the value of 'NULL 解决办法:打开SQL语句,把里面的注释给 ...