JSP download from intranet - java

I need to read binary file from intranet http server and get it to download to public.
SCHEMA
intranet file server(Apache)=1 <-->Public http server(Apache Tomcat)=2<-->internet authorized user=3
howto release this without save to filesystem on server 2
Thanks for answers i am new on java.
Sorry for my English.

Use java.net.URL (or another http client) to read from 1 and then print it out (in response to 3).
(In Apache Http Server or Nginx this can be achieved using reverse proxy.)

I can only think of two ways in this situation:
Redirect the internet request to intranet.
In JSP page use:
<% response.sendRedirect("http://intranet_address");%>
or
<c:redirect url="http://intranet_address"/> using standard taglib.
In Servlet page use:
response.setStatus(302);
response.setHeader("Location", "http://intranet_address"); or just
response.sendRedirect("http://intranet_address");
Use a kind of proxy on server 2 to read from server 1 and send directly to internet user without saving to server 2.
I have never tried the first approach on an intranet, but I don't think it would work given the fact the intranet address won't be valid to the internet user.
Now we are only left with the second approach - using a proxy layer. The proxy function could be implemented in many ways: a simple one might be just a bean behind the Servlet to open URL to the file server 1, read file and send it through Servlet response stream to the user or maybe you can use some kind of embedded HTTPClient.
Edit: Since you are going to download binary file, JSP is not a good choice. It's meant to handle textual data. You need Servlet to do binary stream. You can set things like the following on your HttpServletResponse:
resp.setContentType("application/octet-stream");
resp.setContentLength(length);
resp.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"" );
so the content will be send as an attachment with the name you set.

Related

java - send a file to client by file URL without download on the server

In my web application I have a link which, when clicked, invokes an external web service to retrieve a download URL for a file.
I need to send back to client the file which is beyond this URL, instead of the download URL retrieved from the web service. If possible, I would also like to do it without having to download the file on my server beforehand.
I've found this question about a similar task, but which used PHP with the readfile() function.
Is there a similar way to do this in Java 8?
If you doesn't even want to handle that file you should answer the request with a redirect (eg HTTP 301 or 302). If you want to handle the file you should read the file in a byte buffer and send it to the client which would make the transfer slower.
Without seeing your implementation so far, this is my best suggest.

request parameters from a jsp in java without using servlets

I want to request parameters in a java file from a jsp without using servlets. Does anyone know how to do this? I believe its something to do with setting an attribute but I'm new to java so its all a learning game from me. I want to send the lotsize and bedrooms parameters to the java file. My jsp file is shown below and i need to know what to put in my java file in order to retrieve these parameters
If you want this indeed, you should use HTML instead to JSP. For your question, I'm not sure, but I guess, because of the socket is based on the TCP protocol, so you can use a browser as a socket client, use GET or POST request to the socket server, you can enter the socket server's address in browser or submit an HTML form, the socket receives the request and response headers and HTML body to the browser.

how to send file objects to servlet using json

I am new to web programming. My web application can upload files (uploaded by drag and drop method in javascripts ) and i want to retrieve them in servlet using Json . servlet file only needs the contents of the text files to do the calculation.
Can any one suggest how to do this ?
softwares used - netbeans ,tomcat
Thank you.
I'm not quite sure if you mean rendering your files in servlets or actually downloading them from your browser. I'm going to assume you mean rendering them. If so, then what you have to do is set up a URI which is associated with the content you want to render. Let's say this is a simple "hello world" rendering in the browser. what you would do is set up a URI as such:
localhost:3000/helloWorld.html.jsp
What you do on your back end is then wait to receive a http GET request to "/helloWorld.html.jsp" page, and then have your router send over the correct page. Luckily, with tools such as tomcat, the routing from local host is straight forward enough, and as your comments mentioned this is simple enough to do with the ample resources on the web.
Also an important point, you don't need JSON when using servlets (if i understood your problem correctly). If you want to send over data in a JSON format, then all you would do is modify the above steps to listen for "/sendJSON.html.jsp" (this could be an empty jsp), and you send over JSON from your back end in your response.

Tomcat WebDAV is missing Content-Length header

I'm using Tomcat's WebDAV servlet and it seems that I can't get the header "Content-Length" when I'm issuing a PUT request.
How do I get the content length of the file I'm "putting"?
Assuming you mean that you're writing code which is part of the server-side PUT operation - ie you're extending the webdav servlet or something. Then if the client has sent a file via PUT and there is no content-length header then you need to buffer the bytes (probably to disk) and then use the resulting buffered data to give you the length.
Its perfectly legal for clients to send a file without a content length. In that case they simply drop the TCP connection to indicate EOF.
Note that if you are extending the tomcat webdav servlet, you might also want to consider using milton.io. Its a webdav servlet intended to allow a pluggable backend. It also ships with a filesystem implementation equivalent to tomcat's webdav servlet.

Apache Http Client Encoding UTF-8 not working

I have the following scenario:
JSP -> Servlet -> ServiceAPI -> Service Servlet
I enter some cyrilic symbols in the JSP page, which is the start of the scenario. On the next step, the Servlet, I read the data from the JSP in UTF-8. So for, so good. Everything is OK.
Then I pass the data to a ServiceAPI, which sends it to a Service Servlet. Here comes the problem. The data in the Service Servlet is read as '??????'. So, I guess the problem is in the Service API which does not send the data correctly. ServiceAPI implementation uses Apache Http Client to send the data to the Service Servlet.
As I read in Apache Http Client documentation (http://hc.apache.org/httpclient-3.x/preference-api.html#HTTP_method_parameters) there is a way to set a character encoding in the request. But I am not able to apply this, becuase of a the following error: "Access restriction: The method setParameter(String, Object) from the type HttpParams is not accessible due to restriction on required library ...". So I am kind of stuck. Do you have any idea if the problem is really in Apache Http Client and I how can I fix it.
Thanks in advance.
To correctly implement Character Encoding in web apps consists of 4 steps
1.First you have to configure your web server.
2.Then you have to force your web app to use UTF-8 encoding for all requests/responses.
3.Third you have to use JSP page encoding.
4.And last you must use HMTL-meta tags.
In your case the problem lies most probably on step 2 IMO
Here is the perfect article for you How to get UTF-8 working in Java webapps? that describes how to do all these extensively

Categories

Resources