您好,登錄后才能下訂單哦!
實現步驟:
(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("<h4>服務器名稱:"+request.getServerName()+"</h4>"); out.println("<h4>服務器端口:"+request.getServerPort()+"</h4>"); out.println("<h4>"+context.getServerInfo()+"</h4>"); 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獲取服務器基本信息詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。