简体   繁体   中英

Stopping a Servlet from returning a Response

Had a look through SO and couldn't find a question similar to what I'm after. I'll start off by explaining what I'm trying to do, then finish up with a more specific question..

I have a link that passes a query string parameter to my servlet. That parameter is report . If report = true in the servlet, then I'll generate a PDF document. The PDF document then returns this value, by setting the response's mime type to application/pdf . This code is shown below:

        String mimeType = "application/pdf";
        res.setContentType(mimeType);
        res.setHeader("Content-Disposition", "attachment; filename=\"" +    
        getEventID(doc) + ".pdf\"");
        // Set the response content type and pdf attachment.

        out = new ByteArrayOutputStream();
        // All PDF Data is pushed onto the output stream.
        com.lowagie.text.Document pdfDoc = buildPDF(getEventID(doc));

This code is then written to the response object's output stream.

        if(pdfDoc == null)
        {
            // Something went wrong in generating the report.
            return false;
        }
        // Create the PDF document.
        out.writeTo(res.getOutputStream());

If all goes well, the class returns true . If not, it returns false . Now, the problem I'm having is if it returns false. Essentially, I want to point blank stop the data from going anywhere. I added the check to make sure things went well, before I write anything to the output stream, so at the moment what I have is a response that is set to PDF type, but contains no data, if something goes wrong that is.

Next, I have a function that will test the output of the class. If it's true, then all is good, but if it is false, then it sets an error parameter:

 if(!PdfReportGenerator.generateReport(res, repositoryURI)) {
             req.getSession().setAttribute(SDRestServlet.PDF_ERROR, "error");
             // This will then re-direct back to the current URL, meaning the page
             // looks like it doesn't do anything.
             res.sendRedirect(req.getRequestURI());
 }

The problem is, this re-directing is really not helping at all. It's messing up other values that are stored in the request and, while it's making the page appear like it's doing nothing, it doesn't allow me to output an error message to the user.

While I know how to make it seem like the web response is not returning, it means that I can not output any meaningful information to the user, which is obviously not the ideal outcome.

Is there a way to force the servlet to stop, or return something so that the browser ignores the data?

My second question is, if there is something I can send back to the browser, is there anything I can do on the client side to cause a message to pop up (can be as simple as alert() )?

I've been as clear as I possibly can be, so if there's anything you need to know, just ask :)

Is there a way to force the servlet to stop, or return something so that the browser ignores the data?

Please try setting zero response using method "ServletResponse.setContentLength(int)"

My second question is, if there is something I can send back to the browser, is there anything I can do on the client side to cause a message to pop up (can be as simple as alert())?

Yes you can but you need to update back header to say "text/html" and set all the variable as you would do in a normal scenario of a server request

SECOND APPROACH:

If I have to build it from scratch, would following following approach:

  1. First make and AJAX call to find whether pdf need to be generated or not
  2. If response is false show error message.
  3. If response is true send request to server to generate PDF

Hopefully I was able to help you a bit here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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