[opencms-dev] Declare http status (404/301/...) in JSP

Kai Widmann Widmann at mediaworx.de
Wed Feb 6 09:25:34 CET 2019


Hey there,

I succeeded sending headers by using the „top response“, that’s the original http response.
Here’s what I did:

CmsFlexController flexController = CmsFlexController.getController(pageContext.getRequest());
HttpServletResponse topResponse = flexController.getTopResponse();
topResponse.setContentType(MIME_TYPE_PDF);
topResponse.setContentLength(pdf.length);
topResponse.setHeader("Content-Disposition","inline; filename="+fileName);

I think with the topResponse you might also be able to send an error as well, but I’m not sure and I can’t try right now.
As for redirecting to the error page, I’m not sure if that’s possible by sending an error. Maybe the dispatcher is already done, so maybe you have to do the redirect yourself like this (again I can’t try).

flexController.getTopResponse().sendRedirect(errorRedirect);

Hope that helps, best

Kai



Am 05.02.2019 um 22:22 schrieb raphael.cavalcanti <raphael.cavalcanti at almg.gov.br<mailto:raphael.cavalcanti at almg.gov.br>>:

Hello!

Does anyone know if the problem described on the original post has now been
resolved? Yes, i know, 14 years have passed, but i'm trying to the the same
thing as the original author and having the same problems.

Complementing the original post, what i'm trying to do is validate a few
parameters expected by my JSP before running it.
So, for example, my page "http://mytest.com?myparam=%5Ctext", in which
"myparam" should only receive integer values.
What i want to do is, at the begining of my JSP, do that validation, and if
a value that is not an integer is identified then do a
"response.sendError(400);" for "bad request".

I've tried many things and none of then worked:

<%
//1st try
CmsStaticExportResponseWrapper respw = new
CmsStaticExportResponseWrapper(response);
respw.sendError(400);

//2nd try
CmsJspBean cmsBean = new CmsJspBean();
cmsBean.init(pageContext, request, response);
cmsBean.setStatus(400);

//3rd try
cmsBean.getResponse().sendError(404, "Invalid params.");

//4th try
OpenCmsServlet ocs = new OpenCmsServlet();
ocs.handle(request, response, "400");

//5th try
response.sendError(400, "Invalid params.");

//end of test
out.println("END OF THE TEST...<br />");
%>

All of those attempts end with the message "END OF THE TEST..." printed on
the screen. What i wanted was for the page to redirect to the "handle400"
error page.

According to this reference, the best practice would be to do that
validation on the servlet, before redirecting to the jsp, but i don't know
if that is possible on OpenCMS:

https://stackoverflow.com/questions/7109419/what-is-the-best-practice-for-validating-parameters-in-jsp/

Thanks in advance for any help!



--
Sent from: http://opencms.996256.n3.nabble.com/
_______________________________________________
This mail is sent to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/cgi-bin/mailman/listinfo/opencms-dev




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20190206/5dd1831e/attachment.htm>


More information about the opencms-dev mailing list