For CGI variables

Source: Internet
Author: User
Tags java keywords
[ArticleInformation]
Author: Pragmatic
Time: 2004-03-17
Source: Yesky
Edit responsibility: Ark
[Document Introduction]
Httpservlet is inherited from genericservlet, so it has methods and objects similar to genericservlet
[Body]

Java keywords
Network j2s mobile game javacard struts game analyzer jaas ejb javamail design mode J2EE
Xml ejb JavaBean servlet JDBC

Httpservlet is inherited from genericservlet, so it has similar methods and objects like genericservlet. It is a frequently used package for servlet programming. It supports methods such as http post and get.

Programming logic: In the following example, the running result is to simply return the request line and header information sent from the client to the server, as well as some accessible HTTP information.

Snoopservlet. JavaSource codeAs follows:

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 = 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 ));
}
}
}

Programming Skills:

ProgramOutput information such as init parameters, attribute names in this request, parameter names in this request, headers in this request, cookies in this request, and session information.
The object URI returned by the getrequesturi method is a part of the URL, which removes the part of the URL used to specify the machine. The string returned by the getpathinfo method is various options that the client sends to the servlet, these options follow the servlet URL. The string returned by the getpathtranslated method is the Servlet's own absolute path name, snoopservlet. class file location is c: \ jswdk-1.0.1 \ examples \ WEB-INF \ servlets \ snoopservlet. class, the string value returned by the getpathtranslated method is it.

Enter the following address in the browser:

HTTP: /localhost: 8080/examples/servlet/snoopservlet

The result is output.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.