简体   繁体   English

如何使用response.sendRedirect将参数传递给JSP?

[英]How to pass parameters to JSP using response.sendRedirect?

I really need to use response.sendRedirect() method because my JSP(search.jsp) page is actually calling for a servlet <jsp:include page="/searchServlet" /> and in my searchServlet if I use the forward method (to the same page) I get a weird error (maybe because I'm calling it and forwarding all the time, I din't find the reason yet..) however I need to read an attribute in my JSP(search.jsp) page with a simple message like "Please provide valid data" 我真的需要使用response.sendRedirect()方法,因为我的JSP(search.jsp)页面实际上正在调用servlet <jsp:include page="/searchServlet" />并在searchServlet调用(如果我使用forward方法(同一页)我收到一个奇怪的错误(也许是因为我一直在调用它并一直转发,但我还没有找到原因..)但是我需要在我的JSP(search.jsp)页中读取一个属性带有“请提供有效数据”之类的简单消息

However since I'm using my response to redirect to a page, and set this attribute in my request scope, when I try to read it nothing will as expected. 但是,由于我使用响应将其重定向到页面,并在请求范围内设置此属性,因此当我尝试读取该属性时,不会出现任何预期的情况。 I tried to put this attribute in my Session Scope, read and then removing it, it worked (passing wrong data), but only for the first time, if I try to run it providing all data correctly again I'll get the error message since I dind't close my browser to actually remove it from my Session. 我尝试将此属性放入“会话范围”中,然后读取然后将其删除,它起作用了(传递了错误的数据),但这只是第一次,如果我尝试运行该属性以再次正确提供所有数据,则会收到错误消息因为我没有关闭浏览器才能将其实际上从会话中删除。

So my question is: there is a way to use sendRedirect() and set an attribute to my JSP page together? 所以我的问题是:有一种方法可以使用sendRedirect()并一起为我的JSP页面设置属性? If not, how to solve this problem? 如果没有,该如何解决?

Thank you! 谢谢!

EDIT 1: 编辑1:

searchServlet: searchServlet:

if (errorMessage != null) {
    /*sendError will generate an HTML error page using the message that you supplied and will override the content type to text/html.
    response.sendError(1, errorMessage);    
    */
    request.getSession().setAttribute("errorMessage",errorMessage);
    response.sendRedirect("charts/search.jsp");
}

search.JSP 搜索

<jsp:include page="/searchServlet" /> 
<c:if test="${sessionScope.errorMessage != null}">
    <div id="error-Message">
        <c:out value="${sessionScope.errorMessage}" />
        <c:remove var="errorMessage" scope="session" />
    </div>
</c:if>

EDIT 2: The error I get if I use the forward() instead of sendRedirect and I never stops, keep looping: 编辑2:如果我使用forward()而不是sendRedirect而得到的错误,并且我从未停止,请继续循环:

at javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:390)
// the error above repeats for more than a thousand times   

at javax.servlet.ServletRequestWrapper.isAsyncStarted(ServletRequestWrapper.java:390)
at org.apache.catalina.core.ApplicationDispatcher.unwrapRequest(ApplicationDispatcher.java:846)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:822)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:486)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:411)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:338)
at control.SearchCriteriaServlet.doPost(SearchCriteriaServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:604)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:543)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
at org.apache.jsp.charts.searchChart_jsp._jspService(searchChart_jsp.java:183)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

EDIT 3: 编辑3:

My doGet() method: 我的doGet()方法:

ArrayList<Machine> foundMachines = MachineDB.getAllMachines();
request.setAttribute("foundMachineList", foundMachines);
RequestDispatcher rd = request.getRequestDispatcher("/searchServlet");
rd.forward(request, response);

My doPost() that process all entries and have inside it my code from edit1: 我的doPost()处理所有条目,并在其中包含来自edit1的代码:

Ok I got the REASON why you are facing the problem. 好的,我明白为什么您要面对这个问题。 That because infinite call loop. 那是因为无限调用循环。 See the code you have written. 查看您编写的代码。 In your JSP <jsp:include page="/searchServlet" /> this line causes to call your servlet searchServlet and again in the servlet you sending the request to search.JSP . 在您的JSP <jsp:include page="/searchServlet" />此行导致调用servlet searchServlet并再次在servlet中将请求发送到search.JSP So correct the issue now. 因此,请立即更正此问题。

As you have found, you can't pass an attribute in the request if you are doing a redirect, as the redirect causes the browser to issue a new request. 如您所知,如果您要进行重定向,则无法在请求中传递属性,因为重定向会导致浏览器发出新请求。 So the request (object) you'll have in your JSP page will be a new one- not the one in which you stored the attribute. 因此,您在JSP页面中拥有的请求(对象)将是一个新请求-而不是您存储属性的请求。

You can store attributes in the session, but as you have found this can cause problems if you don't reset/remove them between requests. 您可以将属性存储在会话中,但是如您所知,如果不在两次请求之间重设/删除它们,可能会导致问题。 Storing state in the session can also cause problems if a user uses their browser's 'back' button, or has two or more tabs open at once. 如果用户使用其浏览器的“后退”按钮,或者一次打开两个或多个选项卡,则在会话中存储状态也会引起问题。

You can pass information in a request parameter using a redirect- eg by constructing the URL you are redirecting to so that it contains the parameters. 您可以使用重定向在请求参数中传递信息,例如,通过构造要重定向到的URL,使其包含参数。 For example blahblah/search?message=whatever 例如blahblah / search?message = whatever

However, this can look weird to the user (they will see the parameters in their browser's address bar) and you may run into problems of the URLs become large. 但是,这对于用户来说似乎很奇怪(他们将在其浏览器的地址栏中看到参数),并且可能会遇到URL变大的问题。

In your case I would look at why the forward is not working. 在您的情况下,我将看看为什么前锋不起作用。

As stated by mahesh, you have a general design problem : 正如mahesh所说,您有一个一般性的设计问题:

  • search.jsp includes searchServlet (hmm ...) search.jsp包含searchServlet (hmm ...)
  • searchServlet forwards (or redirect as a workaround) to search.jsp searchServlet转发(或作为解决方法重定向)到search.jsp

That design is pretty uncommon. 这种设计很少见。 Of course using a redirect instead or a forward in you servlet will break the infinite loop, but your servlet sends the redirect to the browser so that the browser calls the view ... and of course you cannot pass anything in the request between the servlet and its view since the view will be called in another request ... IMHO the redirect here is only a quick workaround. 当然,在您的Servlet中使用重定向代替或转发将打破无限循环,但是您的Servlet将重定向发送到浏览器,以便浏览器调用视图...当然,您不能在Servlet之间的请求中传递任何内容及其视图,因为该视图将在另一个请求中调用。恕我直言,此处的重定向只是一种快速的解决方法。

What would be better (according to common practices) 哪个会更好(根据常规做法)

  • browser initially calls the servlet (ie : the link to search page points to an url directly processed by the servlet) 浏览器最初调用servlet(即:搜索页面的链接指向servlet直接处理的url)
  • the servlet does the heavy work (programmed in java, simpler to write and test) servlet完成繁重的工作(用Java编程,编写和测试更简单)
  • the servlet puts all relevant parameters in request and forward to a jsp servlet将所有相关参数放入请求中并转发给jsp
  • the jsp simply formats all in good html that will be displayed in the browser. jsp只是将所有格式正确的html格式设置在浏览器中。

You can keep parameters in session. 您可以将参数保留在会话中。

session.setAttribute("Att1","para1");
session.setAttribute("Att2","para2");
.
.
.

like this. 像这样。

you can use RequestDispatcher like this:- 您可以像这样使用RequestDispatcher:-

     RequestDispatcher rd=request.getRequestDispatcher("result.jsp");
     rd.forward(request,response);

if you are using response.sendRedirect then you can add the parameters in the ServletRequest Object,and can be use in jsp.like that:- 如果您使用response.sendRedirect,则可以在ServletRequest对象中添加参数,并且可以在jsp中使用。

      <% String st=(String)request.getParameter("uname"); %>

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

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