在JSP/Servlet中从不同的类中以追加方式将输出打印到TextArea:

huangapple go评论83阅读模式
英文:

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

&lt;form action=&quot;submitClick&quot; method =&quot;post&quot;
&lt;textarea&gt; id=&quot;textarea&quot; name=&quot;textarea&quot; rows=&quot;25&quot; cols=&quot;100&quot;&gt;${result}&lt;/textarea&gt;
&lt;input type=&quot;submit&quot;&gt;


 SubmitClick.java //servlet class
    public class SubmitClick extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response){
    MainLogicClass mainLogic = new MainLogicClass(username,password); //let&#39;s suppose hardcoded
    
    request.setAttribute(&quot;result&quot;, &quot;Hello&quot;);// Hello is getting printed on textarea, but I want to print output text on textarea from MainLogicClass.
    
    getServletContext().getRequestDispatcher(&quot;/index.jsp&quot;).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(&quot;Database Connection successful&quot;);
/* I want to print &quot;Database Connection successful&quot; 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-
&quot;Database Connection successful

DB query executed

DB connection closed&quot;
*/
}
}

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 = &quot;&quot; ;
msg +=&quot;Something to return&quot;;
msg +=&quot;soemthing more&quot;;
//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(&quot;result&quot;, message );

huangapple
  • 本文由 发表于 2020年9月8日 17:26:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63790979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定