欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JSP和JSTL获取服务器参数示例

程序员文章站 2023-11-21 19:15:22
复制代码 代码如下: <%@ page language="java" import="java.util.*" pageencoding="utf-8"%>...
复制代码 代码如下:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<title>jsp和jstl获取服务器参数</title>
</head>
<body>
jsp方式<br/>
<%

string server_name = request.getservername();

string server_software = getservletcontext().getserverinfo();

string server_protocol = request.getprotocol();

integer server_port = request.getserverport();

string request_method = request.getmethod();

string path_info = request.getpathinfo();

string path_translated = request.getpathtranslated();

string script_name = request.getservletpath();

string document_root = request.getrealpath("/");

string query_string = request.getquerystring();

string remote_host = request.getremotehost();

string remote_addr = request.getremoteaddr();

string auth_type = request.getauthtype();

string remote_user = request.getremoteuser();

string content_type = request.getcontenttype();

integer content_length = request.getcontentlength();

string http_accept = request.getheader("accept");

string http_user_agent = request.getheader("user-agent");

string http_referer = request.getheader("referer");

hashmap infomap = new hashmap();

infomap.put("server_name", server_name);

infomap.put("server_software", server_software);

infomap.put("server_protocol", server_protocol);

infomap.put("server_port", server_port);
infomap.put("request_method", request_method);

infomap.put("path_info", path_info);

infomap.put("path_translated", path_translated);

infomap.put("script_name", script_name);

infomap.put("document_root", document_root);

infomap.put("query_string", query_string);

infomap.put("remote_host", remote_host);

infomap.put("remote_addr", remote_addr);

infomap.put("auth_type", auth_type);

infomap.put("remote_user", remote_user);

infomap.put("content_type", content_type);

infomap.put("content_length", content_length);

infomap.put("http_accept", http_accept);

infomap.put("http_user_agent", http_user_agent);

infomap.put("http_referer", http_referer);

iterator it = infomap.keyset().iterator();

%>

<%
while (it.hasnext()) {
object o = it.next();
%>
  <%=o%>
  <%=infomap.get(o)%>
  <% out.println("<br>"); }%>
<br/>

jstl方式<br/>
${pagecontext.request} |取得请求对象<br>
${pagecontext.session} |取得session对象<br>
${pagecontext.request.querystring} |取得请求的参数字符串<br>
${pagecontext.request.requesturl} |取得请求的url,但不包括请求之参数字符串<br>
${pagecontext.request.contextpath} |服务的web application的名称<br>
${pagecontext.request.method} |取得http的方法(get、post)<br>
${pagecontext.request.protocol} |取得使用的协议(http/1.1、http/1.0)<br>
${pagecontext.request.remoteuser} |取得用户名称<br>
${pagecontext.session.new} |判断session是否为新的,所谓新的session,表示刚由server产生而client尚未使用<br>
${pagecontext.session.id} |取得session的id<br>

${header["user-agent"]}|用户浏览器的版本<br/>
${header["host"]}|ip<br/>
${pagecontext.request.remoteaddr } |取得用户的ip地址<br>
${pagecontext.servletcontext.serverinfo}|取得主机端的服务信息<br>
${pagecontext.request.serverport}|端口信息<br>
${pagecontext.request.servername}|服务器名称<br>
${pagecontext.request.remotehost}|客户机名称<br>
</body>
</html>