获取各种CGI环境变量

这两天有朋友问我怎么取环境变量,想想以前用PHP,很简单就可以用phpinfo()取到了,JAVA会麻烦一点。找了一下,找到以前收藏过一个例程。


<%@ page session="false" import="java.util.*" %>

<%

String strEnvs[][] =

{

{"CONTENT_LENGTH",String.valueOf(request.getContentLength())},

{"CONTENT_TYPE",request.getContentType()},

{"SERVER_PROTOCOL",request.getProtocol()},

{"SERVER_SOFTWARE",getServletContext().getServerInfo()},

{"REMOTE_ADDR",request.getRemoteAddr()},

{"REMOTE_HOST",request.getRemoteHost()},

{"REMOTE_USER",request.getRemoteUser()},

{"SERVER_NAME",request.getServerName()},

{"SERVER_PORT",String.valueOf(request.getServerPort())},

{"AUTH_TYPE",request.getAuthType()},

{"REQUEST_METHOD",request.getMethod()},

{"PATH_INFO",request.getPathInfo()},

{"PATH_TRANSLATED",request.getPathTranslated()},

{"QUERY_STRING",request.getQueryString()},

{"REQUEST_URI",request.getRequestURI()},

{"SCRIPT_NAME",request.getServletPath()},

{"DOCUMENT_ROOT",getServletContext().getRealPath("/")}

};

Enumeration enumNames;

String strName,strValue;

int i;

%>

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>JSP例程 - 获取各种CGI环境变量</title>

</head>

<body>

<table border=1 cellspacing=0 cellpadding=0 align=center>

<tr>

<th>Name</th>

<th>Value</th>

</tr>

<%

enumNames = request.getHeaderNames();

while(enumNames.hasMoreElements()){

strName = (String)enumNames.nextElement();

strValue = request.getHeader(strName);

%>

<tr>

<td> <%=strName%></td>

<td> <%=strValue%></td>

</tr>

<%

}

%>

<tr>

<th>Name</th>

<th>Value</th>

</tr>

<%

for(i=0;i<strEnvs.length;i++){

%>

<tr>

<td> <%=strEnvs[i][0]%></td>

<td> <%=strEnvs[i][1]%></td>

</tr>

<%

}

%>

</table>

</body>

</html>
<% String strEnvs[][] = { {"CONTENT_LENGTH",String.valueOf(request.getContentLength())}, {"CONTENT_TYPE",request.getContentType()}, {"SERVER_PROTOCOL",request.getProtocol()}, {"SERVER_SOFTWARE",getServletContext().getServerInfo()}, {"REMOTE_ADDR",request.getRemoteAddr()}, {"REMOTE_HOST",request.getRemoteHost()}, {"REMOTE_USER",request.getRemoteUser()}, {"SERVER_NAME",request.getServerName()}, {"SERVER_PORT",String.valueOf(request.getServerPort())}, {"AUTH_TYPE",request.getAuthType()}, {"REQUEST_METHOD",request.getMethod()}, {"PATH_INFO",request.getPathInfo()}, {"PATH_TRANSLATED",request.getPathTranslated()}, {"QUERY_STRING",request.getQueryString()}, {"REQUEST_URI",request.getRequestURI()}, {"SCRIPT_NAME",request.getServletPath()}, {"DOCUMENT_ROOT",getServletContext().getRealPath("/")} }; Enumeration enumNames; String strName,strValue; int i; %>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值