英文:
Print output in TextArea(append-way) from different class in JSP/Servlet
问题
我有以下动态网页项目设置:
Index.jsp
<form action="submitClick" method="post">
<textarea id="textarea" name="textarea" rows="25" cols="100">${result}</textarea>
<input type="submit">
</form>
SubmitClick.java //servlet 类
public class SubmitClick extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response) {
MainLogicClass mainLogic = new MainLogicClass(username, password); // 假设已硬编码
request.setAttribute("result", "Hello"); // "Hello" 会显示在文本区域中,但我想从 MainLogicClass 在文本区域中打印输出文本。
getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
}
}
MainLogicClass // 不同的类,位于相同的包中
public class MainLogicClass {
public MainLogicClass(String username, String password) {
// 数据库连接逻辑
System.out.println("数据库连接成功");
/* 我想要在 index.jsp 上显示 "数据库连接成功",
然后,我需要打印更多的输出,以便将文本追加到文本区域中,如下所示 -
"数据库连接成功
执行数据库查询
关闭数据库连接" */
}
}
如何使用 request.setAttribute 方法从 MainLogicClass 打印文本到 Servlet,或者是否有其他方法可以实现。
英文:
I have below Dynamic web project setup
Index.jsp
<form action="submitClick" method ="post"
<textarea> id="textarea" name="textarea" rows="25" cols="100">${result}</textarea>
<input type="submit">
SubmitClick.java //servlet class
public class SubmitClick extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response){
MainLogicClass mainLogic = new MainLogicClass(username,password); //let's suppose hardcoded
request.setAttribute("result", "Hello");// Hello is getting printed on textarea, but I want to print output text on textarea from MainLogicClass.
getServletContext().getRequestDispatcher("/index.jsp").forward(request,response);
}
}
MainLogicClass//different class, present in same package
public class MainLogicClass{
public MainLogicClass(String username, String password){
//DB Connection logic
System.out.println("Database Connection successful");
/* I want to print "Database Connection successful" on textarea which presents on index.jsp
And after that, I need to print few more output so that the text gets appended to textarea like-
"Database Connection successful
DB query executed
DB connection closed"
*/
}
}
How can I print text from MainLogicClass to Servlet using request.setAttribute method or any other workaround.
答案1
得分: 0
//Constructor没有任何返回类型,所以您可以创建一个方法,将逻辑代码放在其中,并从其中返回一些值。因此,您在`MainLogicClass`内部的方法将如下所示:
public String Something(String username, String password){
String msg = "";
msg += "需要返回的内容";
msg += "更多内容";
//您的逻辑代码
return msg; //返回
}
然后在您的servlet的`doPost`方法中,像下面这样操作:
MainLogicClass mainLogic = new MainLogicClass();
String message = mainLogic.Something(username, password); //调用该函数
request.setAttribute("result", message);
英文:
Constructor doesn't have any return type so instead of constructor you can create a method and put your logic code there and return some value from there . So , your method inside MainLogicClass
will look like somewhat below :
public String Something(String username, String password){
String msg = "" ;
msg +="Something to return";
msg +="soemthing more";
//your logic code
return msg;//return back
}
And then in your servlets doPost
method do like below :
MainLogicClass mainLogic = new MainLogicClass();
String message = mainLogic.Something(String username, String password);//call that function
request.setAttribute("result", message );
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论