这篇文章主要介绍了java获取服务器基本信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
实现步骤:
(1)创建servlet browserserver
(2)调用httpservletrequest对象的getservername()方法获取服务器名称
(3)调用httpservletrequest对象的getserverport()方法获取服务器端口
(4)首先调用getservletcontext()方法获取servletcontext对象,然后调用servletcontext对象的getserverinfo()方法获取服务器环境信息名称、版本信息
(5)利用httpservletresponse对象的printwriter将信息显示到页面package example.servlet;
import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletcontext;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
/**
* servlet implementation class browserserver
*/
@webservlet("/browserserver")
public class browserserver extends httpservlet {
private static final long serialversionuid = 1l;
protected void processrequest(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype("text/html;charset=utf-8");
printwriter out=response.getwriter();
servletcontext context=getservletcontext();
out.println("<html>");
out.println("<head>");
out.println("<title>服务器信息</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>服务器名称:"+request.getservername()+"</h3>");
out.println("<h3>服务器端口:"+request.getserverport()+"</h3>");
out.println("<h3>"+context.getserverinfo()+"</h3>");
out.println("</body>");
out.println("<html>");
out.close();
}
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
processrequest(request,response);
}
protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
processrequest(request,response);
}
} 以上所述是小编给大家介绍的java获取服务器基本信息详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对CodeAE代码之家网站的支持!
原文链接:https://www.cnblogs.com/xiaoyingzhanchi/p/9088836.html
|