使用servlet显示sys属性

时间:2015-12-02 12:19:37

标签: java jsp servlets

我正在使用servlets作为新手,我一直在尝试获取桌面应用程序并尝试查看当我使用servlet时它们是如何出来的,使用system.getproperty方法如果我点击提交按钮我收到一条空白信息。

这是我的jsp代码。

<form action="checkservlet" method="get">
<input type="submit" value="submit"/><br/>
</form>

这是我的servlet代码

package com.check.pack;

import java.io.IOException;
import java.io.PrintWriter;

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 checkservlet
 */
@WebServlet("/checkservlet")
public class checkservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public checkservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        PrintWriter write = response.getWriter();
        Mulizwa Mcheck = new Mulizwa();
    }

}

这是我的班级代码

package com.check.pack;
import java.util.*;

public class Mulizwa {

     public static void main(String[] args) {
            Properties prop = System.getProperties();
            Set<Object> keySet = prop.keySet();
            for(Object obj : keySet){
                System.out.println("System Property: {"+obj.toString()+","+System.getProperty(obj.toString())+"}");
            }
        }
}

我没有专家,但是我在这里学习,如果我点击提交按钮我会从上面的代码中得到什么我需要在浏览器中看到有关系统属性的响应喜欢os name java version等。

3 个答案:

答案 0 :(得分:0)

首先,您需要在Mulizwa类中编写一个函数,该函数可能返回系统属性的String,而不是在Standard out上打印属性的main方法,有些像这样

 public String getPropertyString (){
            Properties prop = System.getProperties();

StringBuilder propertyString = new StringBuilder();
            Set<Object> keySet = prop.keySet();
            for(Object obj : keySet){
                propertyString.append("System Property: {"+obj.toString()+","+System.getProperty(obj.toString())+"}");
            }

return propertyString.toString();
}

然后在你的servlet的doPost方法中,

PrintWriter write = response.getWriter();
        Mulizwa mCheck = new Mulizwa();
write.write(mCheck.getPropertyString());

答案 1 :(得分:0)

不要在main中编写此代码,而是将其保存在Mcheck类中的方法中

public void writeSystemPropertiesInResponse(HttpServletResponse response)) {
            Properties prop = System.getProperties();
            Set<Object> keySet = prop.keySet();
            PrintWriter writer = response.getWriter();

            for(Object obj : keySet){
                writer.write("System Property: {"+obj.toString()+","+System.getProperty(obj.toString())+"}");
            }
        }

在servlet中调用此方法

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
        //PrintWriter write = response.getWriter();no need of this
        Mulizwa Mcheck = new Mulizwa();
        Mcheck.writeSystemPropertiesInResponse(resonse);
    }

<强>更新

如果您不希望将响应作为参数传递,则可以使用StringBuilder构建字符串,并使用其他答案中建议的PrintWriter进行编写。

答案 2 :(得分:0)

更改

package com.check.pack;
import java.util.*;

public class Mulizwa {

     public String getDetails(){
        ResourceBundle rb = ResourceBundle.getBundle("System", Locale.getDefault());
        StringBuilder str = new StringBuilder(" ");
        Enumeration<String> en = rb.getKeys();
        while (en.hasMoreElements()) {
            String key = (String) en.nextElement();
            String value = rb.getString(key);
            str.append(key+":"+value +"\n");
        }
      return sb.toString();
   }
}

并在servelet类中更改doPost,如下所示

PrintWriter out = response.getWriter();
out.println(new Mulizwa().getDetails());