Java Servlet Programming and application of the four

Source: Internet
Author: User
Tags object key session id string server port

HttpServlet is inherited from Genericservlet, so it has genericservlet similar methods and objects, a package that we use with servlet programming, which supports methods such as post and get of HTTP.

Programming Idea: The following example, the operation result is the output simply returns the request line and the header information which the customer sends to the server, as well as some accessible HTTP information and so on.

Snoopservlet.java's source code is as 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 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 of 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));
}
}
}

Instructions for programming Tips:



Related Article

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.