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

时间:2022-09-13 15:09:15

一、摘要

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

  输出结果为:

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

四、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);
}