gpt4 book ai didi

java - 使用 servlet 显示系统属性

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:13 26 4
gpt4 key购买 nike

我正在使用 servlet 作为新手,我一直在尝试获取桌面应用程序,并尝试看看当我使用 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();
}

}

这是我的类(class)代码

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())+"}");
}
}
}

我不是专家,但我来这里是为了学习,如果我点击提交按钮,我对上面的代码有何期望,我需要在浏览器中查看有关系统属性(如操作系统名称、java 版本等)的响应。

最佳答案

首先,您需要在 Mulizwa 类中编写一个函数,该函数可能返回系统属性的字符串,而不是在标准输出上打印属性的 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());

关于java - 使用 servlet 显示系统属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34042599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com