'Snoop' Sample Code
This page contains the venerable snoop servlet code (provided with most servlet
containers as an example), broken into fragments.
Servlet init parameters
|
|
out.println("<h3>Servlet init parameters</h3>");
java.util.Enumeration e = getInitParameterNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String value = getInitParameter(key);
out.println(" " + key + " = " + value);
}
|
Sample output:
No output by default.
Servlet Context Initialization Parameters
|
|
out.println("<h3>Context init parameters</h3>");
ServletContext context = getServletContext();
java.util.Enumeration enum = context.getInitParameterNames();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
Object value = context.getInitParameter(key);
out.println(" " + key + " = " + value);
}
|
Sample output:
No output by default.
Servlet Context Attributes (Application Scope Variables)
|
|
out.println("<h3>Context attributes</h3>");
ServletContext context = getServletContext();
java.util.Enumeration enum = context.getAttributeNames();
while (enum.hasMoreElements()) {
String key = (String)enum.nextElement();
Object value = context.getAttribute(key);
out.println(" " + key + " = " + value);
}
|
Sample output:
javax.servlet.context.tempdir = D:\Java\jakarta-tomcat-3.2.1\work\127.0.0.2_8080
sun.servlet.workdir = D:\Java\jakarta-tomcat-3.2.1\work\127.0.0.2_8080
Request Attributes (Page Scope Variables)
|
|
out.println("Request attributes:");
java.util.Enumeration e = request.getAttributeNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
Object value = request.getAttribute(key);
out.println(" " + key + " = " + value);
}
|
Sample output:Request attributes:
javax.servlet.include.servlet_path = /sessions/9597eukak1.jsp
javax.servlet.include.context_path =
javax.servlet.include.request_uri = /sessions/9597eukak1.jsp
Request Headers
|
Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String value = request.getHeader(key);
out.println(" <b>" + key + "</b>: " + value);
} |
Sample output (long lines are folded for clarity):
Cookie: JSESSIONID=9597eukak1
Cache-Control: no-cache
Host: 127.0.0.2:8080
Accept: application/vnd.ms-powerpoint, application/vnd.ms-excel,
application/msword, application/pdf, image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, */*
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)
Content-Length: 363
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Referer: http://127.0.0.2:8080/JspIn.jsp
Mostly Request Information
|
|
out.println("Servlet Name: " + getServletName());
out.println("Protocol: " + request.getProtocol().trim());
out.println("Scheme: " + request.getScheme());
out.println("Server Name: " + request.getServerName());
out.println("Server Port: " + request.getServerPort());
out.println("Server Info: " + getServletContext().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("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());
|
Sample Output (long lines are folded for clarity):
Servlet Name: jsp
Protocol: HTTP/1.1
Scheme: http
Server Name: 127.0.0.2
Server Port: 8080
Server Info: Tomcat Web Server/3.2.1 (JSP 1.1; Servlet 2.2; Java 1.3.0;
Windows NT 4.0 x86; java.vendor=Sun Microsystems Inc.)
Remote Addr: 127.0.0.2
Remote Host: 127.0.0.2
Character Encoding: null
Content Length: 1968
Content Type: application/x-www-form-urlencoded
Locale: en_US
Default Response Buffer: 8192
Request Is Secure: false
Auth Type: null
HTTP Method: POST
Remote User: null
Request URI: /servlet/com.mslinn.JspExplorer
Context Path:
Servlet Path: /servlet/com.mslinn.JspExplorer
Path Info: null
Path Trans: null
Query String: null
Request Parameters
|
|
out.println("Parameter names in this request:");
java.util.Enumeration 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();
}
| Sample Output
This output can be a bit confusing until you realize that Zamples uses request parameters to perform its magic.
Parameter names in this request:
imports =
stylesheet = on
cmd = out.println("Parameter names in this request:");
java.util.Enumeration 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();
}
applet =
format = Java
pre = on
Request Headers
|
|
out.println("<h3>Request Headers</h3>");
java.util.Enumeration e = request.getHeaderNames();
while (e.hasMoreElements()) {
String key = (String)e.nextElement();
String value = request.getHeader(key);
out.println(" " + key + ": " + value);
}
|
Sample Output (long lines are folded for clarity):
Cookie: JSESSIONID=9597eukak1
Cache-Control: no-cache
Host: 127.0.0.2:8080
Accept: application/vnd.ms-powerpoint, application/vnd.ms-excel,
application/msword, application/pdf, image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, */*
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)
Content-Length: 418
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Connection: Keep-Alive
Referer: http://127.0.0.2:8080/JspIn.jsp
Request Cookies
|
|
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
out.println(" " + cookie.getName() + " = " + cookie.getValue());
}
|
Sample Output
JSESSIONID = 9597eukak1
Session Information
|
out.println("Requested Session Id: " + request.getRequestedSessionId());
HttpSession session = request.getSession();
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("Session scoped attributes:");
java.util.Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
out.println(" " + name + " = " + session.getAttribute(name));
} |
Sample Output
Requested Session Id: 9597eukak1
Current Session Id: 9597eukak1
Session Created Time: 985054073972
Session Last Accessed Time: 985057437578
Session Max Inactive Interval Seconds: 1800
Session scoped attributes:
javax.security.auth.subject = Subject:
Add New Code Samples Here
If you have code samples that don't fit into any of the topics above which you would like to contribute,
please put them here.
We'll sort them out into their own sections as appropriate later.
|
| to add a new posting or reply to an existing posting. |
|
|