Servlet获取服务器的运行参数
package web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetServerInfoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>display server info</TITLE></HEAD>");
out.println(" <BODY>");
printValue(request,out);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void printValue(HttpServletRequest request,PrintWriter out) throws IOException{
out.println("<table align='center' border='1'>");
out.println("<tr><th>参数名</tg><th>数值</th></tr>");
out.println("<tr><td>服务器IP</td><td>"+request.getRemoteAddr()+"</td></tr>");
out.println("<tr><td>服务器名</td><td>"+request.getServerName()+"</td></tr>");
out.println("<tr><td>服务器端口号</td><td>"+request.getServerPort()+"</td></tr>");
out.println("<tr><td>协议名</td><td>"+request.getScheme()+"</td></tr>");
out.println("<tr><td>协议版本</td><td>"+request.getProtocol()+"</td></tr>");
out.println("<tr><td>请求的方法</td><td>"+request.getMethod()+"</td></tr>");
out.println("<tr><td>请求的url</td><td>"+request.getRequestURI()+"</td></tr>");
out.println("<tr><td>上下文路径</td><td>"+request.getContextPath()+"</td></tr>");
out.println("<tr><td>Servlet路径</td><td>"+request.getServletPath()+"</td></tr>");
out.println("</table>");
}
}