简体   繁体   English

您如何将 servlet 中的属性转发到请求它的同一 JSP

[英]How do you forward attributes from a servlet to the same JSP that requested it

For context I have a jsp called "withdraw.jsp" that calls a servlet that processes a withdrawal.对于上下文,我有一个名为“withdraw.jsp”的 jsp,它调用一个处理提款的 servlet。 In this servlet I would like to pass a string message if the transaction is successful or not and I would like to send this message to the same jsp that called the servlet (which is still "withdraw.jsp". Here is what I did so far在这个 servlet 中,如果事务成功与否,我想传递一个字符串消息,并且我想将此消息发送到调用 servlet 的同一个 jsp(它仍然是“withdraw.jsp”。这就是我所做的远的

//In the servlet
RequestDispatcher view = request.getRequestDispatcher("registeredUser/withdraw.jsp");
String error = (String)ex.getMessage();
request.setAttribute("errorMessage", error);
view.include(request,response); // i also tried forward here

//In the jsp
<% if(request.getParameter("errorMessage") != null){ %>
<p> <%=(String)request.getParameter("errorMessage")%> </p>
<% } %>

If I run this code, the jsp wouldn't retrieve the errorMessage since it is null despite being set as an attribute by the servlet.如果我运行此代码,jsp 将不会检索 errorMessage,因为它是 null 尽管被 servlet 设置为属性。 Any help?有什么帮助吗?

You are confusing attributes with parameters .您将属性参数混淆了。

The message is always null in your JSP because you have set an attribute in you servlet, but in your JSP you are looking for a parameter.该消息在您的 JSP 中始终为 null 因为您在 servlet 中设置了一个属性,但是在您的 JSP 中您正在寻找一个参数。 They are different things . 它们是不同的东西

Things to do:要做的事情:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM