Reputation: 141
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"
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? If not, how to solve this problem?
Thank you!
EDIT 1:
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:
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:
My doGet() method:
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:
Upvotes: 0
Views: 11471
Reputation: 141
you can use RequestDispatcher like this:-
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:-
<% String st=(String)request.getParameter("uname"); %>
Upvotes: -1
Reputation: 148975
As stated by mahesh, you have a general design problem :
search.jsp
includes searchServlet
(hmm ...)searchServlet
forwards (or redirect as a workaround) to 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.
What would be better (according to common practices)
Upvotes: 1
Reputation: 1331
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
. So correct the issue now.
Upvotes: 2
Reputation: 3386
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.
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. For example 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.
In your case I would look at why the forward is not working.
Upvotes: 1
Reputation: 7069
You can keep parameters in session.
session.setAttribute("Att1","para1");
session.setAttribute("Att2","para2");
.
.
.
like this.
Upvotes: 0