gpt4 book ai didi

java - 如何在java中使用 session 显示方法的输出

转载 作者:行者123 更新时间:2023-12-02 05:30:01 24 4
gpt4 key购买 nike

我有一个带有方法 serverstate 的操作类,我在其中解析一些服务器详细信息和 weblogic 命令以获取 weblogic 服务器的当前状态。下面的代码在 eclipse 控制台中提供所需的输出,而不是在 GUI 上提供有人可以帮忙吗关于这一点,我想在我使用 session 编码的GUI(JSP)页面上显示输出。

public void serverstate(HttpServletRequest request,HttpServletResponse response) throws IOException, Exception

{
String appname;
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String selapp = " ";
System.out.println("enter appname:");
selapp = dataIn.readLine();
System.out.println("selected app is:" + selapp );
{
try
{

File apps = new File("/WEB-INF/appdetails.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(apps);
doc.getDocumentElement().normalize();

System.out.println("server status of " + selapp);
NodeList nodes = doc.getElementsByTagName("app");
System.out.println("==========================");

for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);

if (node.getNodeType() == Node.ELEMENT_NODE);
{
Element element = (Element) node;
appname = getValue("name", element);
System.out.println(appname);
String user = getValue("uname", element);
System.out.println(user);
String server = getValue("server", element);
System.out.println(server);
String command = getValue("cmd", element);
System.out.println(command);
String passwd = getValue("passwd",element);
System.out.println(passwd);
String cmd= getValue("cmd",element);
System.out.println(cmd);
String port=getValue("port",element);
String clu=getValue("cluster",element);
String admin=getValue("admstate",element);
System.out.println(admin);
if (selapp.equalsIgnoreCase(appname))
{
try {

Runtime rt = Runtime.getRuntime();
//Process pr = rt.exec("cmd /c dir");
{
Process pr = rt.exec(""+ command +" && "+ admin +" && java weblogic.Admin -url "+ server +":"+ port +" -username "+ user +" -password "+ passwd +" CLUSTERSTATE -clusterName "+ clu +"");

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

String line=null;

while((line=input.readLine()) != null) {
System.out.println(line);
// int exitVal = pr.waitFor();
//System.out.println("Exited with error code "+exitVal);
}}}finally{
}}else {
System.out.println("no app selected");
}}}}catch(Exception e) {


System.out.println(e.toString());
e.printStackTrace();
}}
RequestDispatcher dispatcher = request.getRequestDispatcher("status.jsp");
dispatcher.forward(request,response);
}

此方法为我提供了 Eclipse 控制台中的输出,我想在我使用 session 创建的 Jsp 页面中显示相同的输出。请帮助

最佳答案

在您的 Servlet 中,您可以设置想要/需要显示为请求属性的必要数据:

public void serverstate(HttpServletRequest request,HttpServletResponse response)
throws IOException, Exception {
/*
...
*/
//just a small example:
request.setAttribute("name", "Luiggi Mendoza");
RequestDispatcher dispatcher = request.getRequestDispatcher("status.jsp");
dispatcher.forward(request,response);
}

然后在您的 View (JSP)中,您通过表达式语言检索它:

<!DOCTYPE html>
<html>
<body>
Name: ${name}
</body>
</html>

如果您想要/需要附加的数据来自数据库,并且您希望防止网站上的 XSS 攻击,那么最好使用第三方库来显示它,例如 JSTL:

<!DOCTYPE html>
<html>
<body>
Name: <c:out value="${name}" />
</body>
</html>

更多信息:

关于java - 如何在java中使用 session 显示方法的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25648492/

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