显示Servlet API主要版本,次要版本以及服务器系统信息

 1 package com.mhb;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 public class ServletContextDemo extends HttpServlet {
13 
14 public void init() throws ServletException {
15 }
16 
17 public void doGet(HttpServletRequest request, HttpServletResponse response)
18 throws ServletException, IOException {
19 //设置输出内容格式和编码
20 response.setContentType("text/html;charset=gb2312");
21 PrintWriter out = response.getWriter();
22 //获得ServletContext实例
23 ServletContext application = getServletContext();
24 //主要的Servlet API版本
25 int magorVersion = application.getMajorVersion();
26 //次要的Servlet api版本
27 int minorVersion = application.getMinorVersion();
28 //服务器版本
29 String info = application.getServerInfo();
30 
31 out.println("<html>");
32 out.println("<body>");
33 out.println("主要Servlet API版本:"+magorVersion+"<br />");
34 out.println("次要Servlet API版本:"+minorVersion+"<br />");
35 out.println("服务器版本:"+info+"<br />");
36 out.println("</body>");
37 out.println("</html>");
38 
39 }
40 
41 public void doPost(HttpServletRequest request, HttpServletResponse response)
42 throws ServletException, IOException {
43 }
44 
45 public void destroy() {
46 super.destroy(); 
47 }
48 }

浏览器显示:

posted @ 2015-08-25 16:36  北海悟空  阅读(816)  评论(0编辑  收藏  举报