Java Servlet 编程及应用之四

作者:网络 来源:佚名 更新时间:2008-11-17 15:22:33 点击:

  httpservlet 是从genericservlet 继承而来,因此它具有genericservlet 类似的方法和对象,是我们使用servlet编程经常用到的包,它支持http 的post 和 get 等方法。

  编程思路:下面的例子,运行结果是输出简单地返回客户发送给服务器的请求行和头部信息,以及一些可访问的http 信息等。

  snoopservlet.java 的源代码如下:

import java.io.ioexception;
import java.io.printwriter;
import java.util.enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
public class snoopservlet extends httpservlet {
 public void doget(httpservletrequest request, httpservletresponse response)
 throws servletexception, ioexception
 {
  printwriter out = response.getwriter();
  response.setcontenttype("text/plain");
  out.println("snoop servlet");
  out.println();
  out.println("servlet init parameters:");
  enumeration e = getinitparameternames();
  while (e.hasmoreelements()) {
   string key = (string)e.nextelement();
   string value = getinitparameter(key);
   out.println(" " + key + " = " + value);
  }
  out.println();
  out.println("context init parameters:");
  servletcontext context = getservletcontext();
  enumeration enum = context.getinitparameternames();
  while (enum.hasmoreelements()) {
   string key = (string)enum.nextelement();
   object value = context.getinitparameter(key);
   out.println(" " + key + " = " + value);
  }
  out.println();
  out.println("context attributes:");
  enum = context.getattributenames();
  while (enum.hasmoreelements()) {
   string key = (string)enum.nextelement();
   object value = context.getattribute(key);
   out.println(" " + key + " = " + value);
  }
  out.println();
  out.println("request attributes:");
  e = request.getattributenames();
  while (e.hasmoreelements()) {
   string key = (string)e.nextelement();
   object value = request.getattribute(key);
   out.println(" " + key + " = " + value);
  }
  out.println();
  out.println("servlet name: " + getservletname());
  out.println("protocol: " + request.getprotocol());
  out.println("scheme: " + request.getscheme());
  out.println("server name: " + request.getservername());
  out.println("server port: " + request.getserverport());
  out.println("server info: " + context.getserverinfo());
  out.println("remote addr: " + request.getremoteaddr());
  out.println("remote host: " + request.getremotehost());
  out.println("character encoding: " + request.getcharacterencoding());
  out.println("content length: " + request.getcontentlength());
  out.println("content type: "+ request.getcontenttype());
  out.println("locale: "+ request.getlocale());
  out.println("default response buffer: "+ response.getbuffersize());
  out.println();
  out.println("parameter names in this request:");
  e = request.getparameternames();
  while (e.hasmoreelements()) {
   string key = (string)e.nextelement();
   string[] values = request.getparametervalues(key);
   out.print(" " + key + " = ");
   for(int i = 0; i < values.length; i++) {
     out.print(values[i] + " ");
   }
   out.println();
  } 
  out.println();
  out.println("headers in this request:");
  e = request.getheadernames();
  while (e.hasmoreelements()) {
    string key = (string)e.nextelement();
    string value = request.getheader(key);
    out.println(" " + key + ": " + value);
  }
  out.println();
  out.println("cookies in this request:");
  cookie[] cookies = request.getcookies();
  if (cookies != null) {
   for (int i = 0; i < cookies.length; i++) {
    cookie cookie = cookies[i];
    out.println(" " + cookie.getname() + " = "+ cookie.getvalue());
   }
  }
  out.println();
  out.println("request is secure: " + request.issecure());
  out.println("auth type: " + request.getauthtype());
  out.println("http method: " + request.getmethod());
  out.println("remote user: " + request.getremoteuser());
  out.println("request uri: " + request.getrequesturi());
  out.println("context path: " + request.getcontextpath());
  out.println("servlet path: " + request.getservletpath());
  out.println("path info: " + request.getpathinfo());
  out.println("path trans: " + request.getpathtranslated());
  out.println("query string: " + request.getquerystring());
  out.println();
  httpsession session = request.getsession();
  out.println("requested session id: " +
  request.getrequestedsessionid());
  out.println("current session id: " + session.getid());
  out.println("session created time: " + session.getcreationtime());
  out.println("session last accessed time: " +session.getlastaccessedtime());
  out.println("session max inactive interval seconds: " + session.getmaxinactiveinterval());
  out.println();
  out.println("session values: ");
  enumeration names = session.getattributenames();
  while (names.hasmoreelements()) {
   string name = (string) names.nextelement();
   out.println(" " + name + " = " + session.getattribute(name));
  }
 }
}

  编程技巧说明:

|||

最大的网站源码资源下载站,

  程序输出init parameters(初始化参数)、attribute names in this request、parameter names in this request、headers in this request、cookies in this request 和 session information等信息。

  方法getrequesturi返回的对象uri 是作为url 的一部分,是去掉url 中用于指定机器的那部分;方法getpathinfo 返回的字符串是客户向servlet 传送的各种选项,这些选项是跟在servlet 的url 之后的,方法getpathtranslated 返回的字符串是servlet 的自己的绝对路径名,snoopservlet.class 文件位置是c:jswdk-1.0.1examplesweb-infservletssnoopservlet.class,则方法getpathtranslated 返回的字符串值就是它。

  在浏览器中输入如下的地址:

http://localhost:8080/examples/servlet/snoopservlet

  则会输出结果。