Posted in 面试题 onDecember 22, 2015
Servlet可以使用如下四种方法来得到server的name, port和info
public String ServletRequest.getServerName()
public String ServletRequest.getServerPort()
public String ServletContext.getServerInfo()
public String ServletRequest.getAttributes(String name)
如下代码实现用servlet取得server的信息并输出到客户端浏览器:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServerSnoop extends GenericServlet{
public void service(ServletRequest req , ServletResponse res)
throws ServletException,IOException{
res.setContentType("text/plain");
PrintWriter out= res.getWriter();
out.println("req.getServerName()" + req.getServerName());
out.println("req.getServerPort()" + req.getServerPort());
out.println("ServletContext().getServerInfo()" +
getServletContext().getServerInfo());
out.println("getServerInfo() name:" +
getServerInfoName(getServletContext().getServerInfo()));
out.println("getServerInfo() version:" +
getServerInfoVersion(getServletContext().getServerInfo()));
out.println("getServerContext().getAttribute(\"attribute\")" +
getServletContext().getAttribute("attribute"));
}
private String getServerInfoName(String serverInfo){
int slash = serverInfo.indexOf(/);
if(slash==-1)
return serverInfo;
else
return (String) serverInfo.subSequence(0,slash);
}
private String getServerInfoVersion(String serverInfo){
int slash = serverInfo.indexOf(/);
if(slash==-1)
return null;
else
return serverInfo.substring(slash + 1);
}}
public String ServletRequest.getServerName()
public String ServletRequest.getServerPort()
public String ServletContext.getServerInfo()
public String ServletRequest.getAttributes(String name)
如下代码实现用servlet取得server的信息并输出到客户端浏览器:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServerSnoop extends GenericServlet{
public void service(ServletRequest req , ServletResponse res)
throws ServletException,IOException{
res.setContentType("text/plain");
PrintWriter out= res.getWriter();
out.println("req.getServerName()" + req.getServerName());
out.println("req.getServerPort()" + req.getServerPort());
out.println("ServletContext().getServerInfo()" +
getServletContext().getServerInfo());
out.println("getServerInfo() name:" +
getServerInfoName(getServletContext().getServerInfo()));
out.println("getServerInfo() version:" +
getServerInfoVersion(getServletContext().getServerInfo()));
out.println("getServerContext().getAttribute(\"attribute\")" +
getServletContext().getAttribute("attribute"));
}
private String getServerInfoName(String serverInfo){
int slash = serverInfo.indexOf(/);
if(slash==-1)
return serverInfo;
else
return (String) serverInfo.subSequence(0,slash);
}
private String getServerInfoVersion(String serverInfo){
int slash = serverInfo.indexOf(/);
if(slash==-1)
return null;
else
return serverInfo.substring(slash + 1);
}}
Servlet如何得到服务器的信息
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@