浅谈java获取服务器基本信息

时间:2021-11-06 03:36:14

实现步骤:

(1)创建servlet browserserver
(2)调用httpservletrequest对象的getservername()方法获取服务器名称
(3)调用httpservletrequest对象的getserverport()方法获取服务器端口
(4)首先调用getservletcontext()方法获取servletcontext对象,然后调用servletcontext对象的getserverinfo()方法获取服务器环境信息名称、版本信息
(5)利用httpservletresponse对象的printwriter将信息显示到页面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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获取服务器基本信息详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/xiaoyingzhanchi/p/9088836.html