package coreservlets; 
import java.io.*; 
 import javax.servlet.*; 
 import javax.servlet.http.*; 
 import java.util.*; 
/** creates a table showing the current value of each 
 * of the standard cgi variables. 
 * <p> 
 * taken from core servlets and javaserver pages 
 * from prentice hall and sun microsystems press, 
 * http://www.coreservlets.com/. 
 * © 2000 marty hall; may be freely used or adapted. 
 */ 
public class showcgivariables extends httpservlet { 
 public void doget(httpservletrequest request, 
 httpservletresponse response) 
 throws servletexception, ioexception { 
 response.setcontenttype("text/html"); 
 printwriter out = response.getwriter(); 
 string[][] variables = 
 { { "auth_type", request.getauthtype() }, 
 { "content_length", 
 string.valueof(request.getcontentlength()) }, 
 { "content_type", request.getcontenttype() }, 
 { "document_root", 
 getservletcontext().getrealpath("/") }, 
 { "path_info", request.getpathinfo() }, 
 { "path_translated", request.getpathtranslated() }, 
 { "query_string", request.getquerystring() }, 
 { "remote_addr", request.getremoteaddr() }, 
 { "remote_host", request.getremotehost() }, 
 { "remote_user", request.getremoteuser() }, 
 { "request_method", request.getmethod() }, 
 { "script_name", request.getservletpath() }, 
 { "server_name", request.getservername() }, 
 { "server_port", 
 string.valueof(request.getserverport()) }, 
 { "server_protocol", request.getprotocol() }, 
 { "server_software", 
 getservletcontext().getserverinfo() } 
 }; 
 string title = "servlet example: showing cgi variables"; 
 out.println(servletutilities.headwithtitle(title) + 
 "<body bgcolor=/"#fdf5e6/">/n" + 
 "<h1 align=/"center/">" + title + "</h1>/n" + 
 "<table border=1 align=/"center/">/n" + 
 "<tr bgcolor=/"#ffad00/">/n" + 
 "<th>cgi variable name<th>value"); 
 for(int i=0; i<variables.length; i++) { 
 string varname = variables[i][0]; 
 string varvalue = variables[i][1]; 
 if (varvalue == null) 
 varvalue = "<i>not specified</i>"; 
 out.println("<tr><td>" + varname + "<td>" + varvalue); 
 } 
 out.println("</table></body></html>"); 
 } 
/** post and get requests handled identically. */
public void dopost(httpservletrequest request, 
 httpservletresponse response) 
 throws servletexception, ioexception { 
 doget(request, response); 
 } 
 } 
新聞熱點(diǎn)
疑難解答
圖片精選