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..
My aim
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.
The issue
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.
My question
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:
First make and AJAX call to find whether pdf need to be generated or not
If response is false show error message.
If response is true send request to server to generate PDF
Hopefully I was able to help you a bit here.
Related
I'm showing a dropdown on a web page but when using characters as ○ as options, the dropdown shows a question mark
I'm getting the dropdown option from a SQL Server database in which the column that saves the value is nvarchar type
Then I create an XML output string with the values to send it as response of an AJAX call
When I do xmlWriter.toString() , being xmlWriter a StringWriter object, I'm able to see the ○ character using Eclipse's debug mode but that string needs to be sent as a ByteArrayOutputStream object to add it to response stream for the response to see the XML file on the client side but when doing xmlWriter.toString().getBytes() the ○ character becomes a question mark
I've tried to use xmlWriter.toString().getBytes("UTF-8") but the result is some strange symbols
What am I missing?
By guessing what might be your problem it feels like you're not specifying the encoding in your response object to the browser and it fails guessing the right one. Consider calling getBytes("UTF-8") as you did (better: getBytes(StandardCharsets.UTF_8)) and submit an encoding information along with your response, either in the HTTP header (Content-Type: application/xml; charset=utf-8) as you're probably using HTTP or in the XML header (<?xml version="1.0" encoding="utf-8"?>). Maybe even both as this will provide you the best compatibility.
I am using sapui5 control UploadCollection to upload set of images and servlet to process the post request.
Problem 1: I have slightest idea how to parse the content to get images in doPost.
Problem 2: For the UploadCollection, it's not advisable to change the content type by modifying the header parameters. So, i'd need to get those images in servlet without multipart as content type.
I have seen dozens of examples, all having multipart as content type. I need a solution where content type from browser comes as image/*. Hints or code snippets would do.
I am not sure which examples you have seen. Normally the UploadCollection never uses multipart. You can check the code of the UploadCollection here and see that the FileUploaders are always built with useMultipart: false.
Also, if you check the examples from the Explored app, you will see that the content type is image/png or whatever type of file you select (on Chrome):
I am not really sure what is the behaviour on IE 8 / 9, where things are a little different (uploads through AJAX is not supported).
The multipart content type is controlled by the useMultipart property of the FileUploader. If you need to play around with this value, you will need to replace the default upload button from the UploadCollection. To do this, simply make the default upload button invisible (using the uploadButtonInvisible property) and add your own FileUploader in the toolbar of the UploadCollection.
Related to the Servlet question: it depends what you want to do with the Image. You can get the InputStream from the request
and then use it for whatever you need it. The input stream will contain the image itself (if the content is not multi-part that is).
I have a simple TCP serversocket that will GET a byte array. This GET comes from when entering a website on this server that contains an img src link to a gif image, the requests looks like this:
GET /myHome.htm HTTP/1.1
GET /house.gif HTTP/1.1
Now the byte array is done like this:
byte[] fileByte = Files.readAllBytes(filePath);
To print the website which contains this image I do this:
out.writeBytes(new String(fileByte));
out:
DataOutputStream out= new DataOutputStream(socketClient.getOutputStream());
Now to make the image display I think I have to use something else then
out.writeBytes()
but I do not know for sure. Anybody knows how to make the image display? Right now the image just dont show at all.
First, make sure your GIF file is not corrupted. (Happened to me before, too).
If that is the case, try this code for sending the GIF file:
byte[] fileByte = Files.readAllBytes(filePath);
writer.writeBytes("HTTP/1.1 200 OK\r\n");
writer.writeBytes("Content-Type: image/gif\r\n");
writer.writeBytes("Content-Length: "+fileByte.length+"\r\n");
writer.writeBytes("\r\n");
writer.write(fileByte, 0, fileByte.length);
And then try to navigate to "house.gif" directly instead of "myHome.htm". Let me know in the comments what this does.
Previous answer attempts:
I think I may have misunderstood your question. Let me try with a different answer:
You are not sure how to figure out on the server when to return the HTML file myHome.htm and when to return house.gif?
I think for this you need to simply parse out the requested URL. Just check whether it contains "house.gif" or not. Then, depending on this, you either return the HTML file as you described above, or you send the .gif file, making sure that you use
writer.write(fileByte, 0, fileByte.length);
to send the binary data and that you set a reply header of
Content-Type: image/gif
In both cases (for the HTML file and the GIF file), though, you should prepend the data you are sending with correct HTTP response headers. Don't take the page-title the wrong way, but this site might help: http://net.tutsplus.com/tutorials/other/http-headers-for-dummies/
And just to make sure: Your server will be receiving TWO independent requests. The first one will ask for the HTML file, the second one will ask for the GIF file. So you send either one or the other. So, there's no "special way" to send the GIF instead of the HTML file. You use the same clientSocket. But it's a different connection.
Previous answer(s):
I think you might be missing the mime-type of your returned data. Try adding the following HTTP header to your reply:
Content-Type: image/gif
Actually... Are you sending a correct HTTP reply at all (including headers, specifically Content-Length)? If not, shoot me a comment and I'll post the code that you need for this.
If, for some reason, you cannot set the content-type header to let the browser know that you are sending it an image, you might be able to load the binary data on the client with an XMLHttpRequest into a JavaScript function rather than specifying it as the source Url of an img tag. Then you can use JavaScript to encode the binary data into a dataURI (http://en.wikipedia.org/wiki/Data_URI_scheme) with the correct mime type and set that as the source of the image.
Actually, I just noticed something in your code:
new String(fileByte)
might interpret the fileBytes as unicode characters rather than binary. Then, when you write this to the writer, it might screw it up as probably not all data in the image are valid unicode. Try replacing the line with this:
writer.write(fileByte, 0, fileByte.length);
Maybe this is all you need to do to fix it???
What's the best practice to handle clicks on file download links when the respective files are missing?
The specific situation is that attachment entities exist in the DB, pointing to file names only and the file storage path can be configured individually/separately. This is for a legacy app and has to be supported.
Is this possible? How does such code look like? I tried
if ( file.canRead() )
{
byte[] data = FileUtils.readFileToByteArray( file );
// goes to code like here: http://balusc.blogspot.de/2006/05/pdf-handling.html
downloadFile( attachment.getFileName(), data );
}
else
{
this.log.errorv( "Attachment {0} not found in configured storage path {1}", file, this.storagePath );
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.addMessage( null,
new FacesMessage( FacesMessage.SEVERITY_ERROR, "Failed.",
"Downlosding file " + file + " failed! File doesn't exist in the configured storage path " + this.storagePath ) );
// ???
facesContext.responseComplete();
}
but this results in
XML-Verarbeitungsfehler: Kein Element gefunden
Adresse: https://localhost:8181/cmc-compliance/view/prototype/collisionManager.xhtml
Zeile Nr. 1, Spalte 1:
(<rant>OMG, I HATE... eh DISLIKE int18ned error messages... someone should get rid of the person who thought this is a good idea... </rant>)
OK, the above means something like "XML processing error: no element found + Line 1, Column 1"
My code is obviously not the right way to do it...
I'm using the JSF code in a datatable:
<h:commandLink action="#{attachmentManager.downloadAttachment(att)}">
<h:graphicImage library="images/icons" name="page_white.png" />
<h:outputText value="#{att.fileName}" />
</h:commandLink>
What I'd ideally want is to display a JSF message (or PrimeFaces growl) and then leave the page as is, that is without issuing a full request on the same page again.
How do you do this?
With facesContext.responseComplete(); you're basically preventing JSF from rendering the response. Hence the client retrieves a completely empty response. The webbrowser is trying to make its best out of it. All the webbrowser knows is that the requested resource has a .xhtml file extension. So the webbrowser presumes that it's some XML content. Then the webbrowser tries to parse the empty response as XML. But it failed miserably with the given error as there are no XML elements at all. A XML well formed document is namely required to have at least one root XML element.
Remove the facesContext.responseComplete(); line and just return from the method the usual way. You should only use facesContext.responseComplete(); when you've already written to the response yourself, such as providing a file download in this particular case.
OMG, I HATE int18ned error messages... someone should get rid of the person who thought this is a good idea...
Just change the operating system platform default locale accordingly in platform specific settings. In case of Windows, a related answer with a screenshot can be found here: Getting OS language in java. If the program itself (e.g. Firefox/Chrome) has also some locale-related settings as well, you might want to change it in the program itself as well.
Hi I have GWT client with standard server-side Servlets.
I can upload file from GWT client-side and read it's contents at server-side
I can send it back to client as String
BUT
I have GWT FormPanel with action (myModule+"import"). FormPanel invokes POST from servlet. Browser then redirects me to myurl/import so I can see contents of uploaded file.
This is not what I wanted though. I'd simply like to have my String back. I added
submitCompleteHandler to my FormPanel, but it doesn't log any results.
I noticed that servlets have method such setContentType so I tried text/html, text/plain ... I don't know what should be there ...
To say it in one sentence, I want to send String back to client from servlet without having browser to redirect me somewhere else. Is it possible?
Since you are submitting a form you get your browser to change navigation. In order to make it work the way you want you have to send the file with ajax. For GWT there is the GWTUpload library that allows you to do that.
If the browser redirects you, it's because you gave a "target" to the FormPanel. By default, it submits within an hidden iframe (a.k.a "ajax upload").
As said in the javadoc, you have to setContentType("text/html") in your servlet if you want onSubmitComplete to be reliably called.
onSubmitComplete's results is the returned HTML's body innerHTML so you have to be very careful when sending back values with < or & in them. The only reliable way to get them back is to escape them on the server-side, and unescape them on the client-side. You can either use your own escaping mechanism, or you can use < and &. In the latter case, to unescape on the client-side, you'd either use String#replace, or create an HTML element, set it's innerHTML with the string you got back, and then get its innerText:
public String htmlUnescape(String htmlEscaped) {
Element tmp = Document.get().createDivElement();
tmp.setInnerHTML(htmlEscaped);
return tmp.getInnerText();
}
On the server-side, you'd use:
escaped = content.replace("&", "&").replace("<", "<")
(order matters here if you don't want <s to become <; also, replacing < and & is enough, > and " won't cause any issue here)
In your case however, make sure first that the file's content is "text" and not "binary", as it wouldn't make sense to return it as String could cause issues depending on how you use the value on the client side.