java - Stopping a Servlet from returning a Response -


had through , couldn't find question similar i'm after. i'll start off explaining i'm trying do, finish more specific question..

aim

i have link passes query string parameter servlet. parameter report. if report = true in servlet, i'll generate pdf document. pdf document returns value, setting response's mime type application/pdf. code shown below:

        string mimetype = "application/pdf";         res.setcontenttype(mimetype);         res.setheader("content-disposition", "attachment; filename=\"" +             geteventid(doc) + ".pdf\"");         // set response content type , pdf attachment.          out = new bytearrayoutputstream();         // pdf data pushed onto output stream.         com.lowagie.text.document pdfdoc = buildpdf(geteventid(doc)); 

this code written response object's output stream.

        if(pdfdoc == null)         {             // went wrong in generating report.             return false;         }         // create pdf document.         out.writeto(res.getoutputstream()); 

if goes well, class returns true. if not, returns false. now, problem i'm having if returns false. essentially, want point blank stop data going anywhere. added check make sure things went well, before write output stream, @ moment have response set pdf type, contains no data, if goes wrong is.

next, have function test output of class. if it's true, good, if false, sets error parameter:

 if(!pdfreportgenerator.generatereport(res, repositoryuri)) {              req.getsession().setattribute(sdrestservlet.pdf_error, "error");              // re-direct current url, meaning page              // looks doesn't anything.              res.sendredirect(req.getrequesturi());  } 

the problem is, re-directing not helping @ all. it's messing other values stored in request and, while it's making page appear it's doing nothing, doesn't allow me output error message user.

issue

while know how make seem like web response not returning, means can not output meaningful information user, not ideal outcome.

question

is there way force servlet stop, or return browser ignores data?

my second question is, if there can send browser, there can on client side cause message pop (can simple alert())?

i've been clear possibly can be, if there's need know, ask :)

is there way force servlet stop, or return browser ignores data?

please try setting 0 response using method "servletresponse.setcontentlength(int)"

my second question is, if there can send browser, there can on client side cause message pop (can simple alert())?

yes can need update header "text/html" , set variable in normal scenario of server request

second approach:

if have build scratch, following following approach:

  1. first make , ajax call find whether pdf need generated or not
  2. if response false show error message.
  3. if response true send request server generate pdf

hopefully able bit here.


Comments