0

Inside a Java servlet, how can I forward the user to a specific URL? For example, my servlet is here:

https://www.mydomain.com/myJavaServlet

and after some business logic, I want to forward the user to this URL:

https://www.mydomain.com/myFile.pdf#nameddest=Chapter3

but in the user's browser, the following URL must be present:

https://www.mydomain.com/myJavaServlet

because the forwarded URL must remain hidden. I think that's the concept behind forwarding (let me know if not).

UPDATE 1:

Why doesn't the following work?

req.getRequestDispatcher("https://www.mydomain.com/myFile.pdf#nameddest=Chapter3").forward(req, res);

UPDATE 2:

The following doesn't give error in browser, it just hangs:

String url = "https://www.mydomain.com/myFile.pdf";
RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(url); 
requestDispatcher.forward(req, res);

If I place the pdf in the web app directory and try

String url = "/WEB-INF/myFile.pdf";

then the browser opens its PDF reader, but no file gets loaded into the reader (it's empty).

If I replace the 2nd line in UPDATE2 above with

RequestDispatcher requestDispatcher = req.getRequestDispatcher(url); 

I get similar results for the two url strings as reported above for UPDATE 2.

ggkmath
  • 4,188
  • 23
  • 72
  • 129
  • You want to send someone to a website, but not show them where they actually are? – takendarkk Apr 08 '14 at 23:23
  • 1
    Is the forwarded resource in the same web application? – Sotirios Delimanolis Apr 08 '14 at 23:24
  • 1
    As a servlet you can load the data from `forwardURL.html` and give it back to the caller as the result of visiting `myJavaServlet`. The url will remain as they expect but the content will be whatever your servlet wants to write. The only requirement is that your servlet have read access on the server to whichever content you want it to show. – Always Learning Apr 08 '14 at 23:26
  • The web app runs in Flash Player, which calls the servlet. The forwarded resource is a PDF file in a public_html folder of a website. They're both on the same domain. – ggkmath Apr 08 '14 at 23:28
  • @stvcisco, I guess that's not forwarding. I've already gone down the route of reading a PDF file and streaming its output to a browser, and PDF named destinations don't work. I don't know how, from a servlet, to download the file myFile.pdf and then have it open at the named destination of Chapter 3 in the file. Thus, my interest in forwarding. – ggkmath Apr 08 '14 at 23:32
  • Do you want to return a PDF file directly from your url? You should be able to set the mime type in the header of your response and dump the PDF file out. The browser would then have to deal with the PDF file appropriately, presumably by invoking Adobe reader. – Always Learning Apr 08 '14 at 23:42
  • @stvcisco, Functionally it should appear as if the user had typed `https://www.mydomain.com/myFile.pdf#nameddest=Chapter3` into the browser URL. Meaning, the file should load into cache, open, and advance to the named destination. Although, the url shown in the browser needs to be `https://www.mydomain.com/myJavaServlet`. Alternatively, if I set the MIME type and download the PDF to the browser, while the URL of the PDF file remains hidden, the file gets downloaded each time (even if it's already open in a browser window) and doesn't advance to the named destination after opening. – ggkmath Apr 08 '14 at 23:47
  • Is this [link](http://www.javapractices.com/topic/TopicAction.do?Id=181) helpful? It sounds like you might want to use the `forward(..)` method of `RequestDispatcher`. – Always Learning Apr 09 '14 at 00:28
  • Isn't that what I have in my UPDATE above? I couldn't get it to work (it gives 404 error). It doesn't appear that it can take a URL as an argument. See: https://community.oracle.com/thread/1455963?start=0&tstart=0 – ggkmath Apr 09 '14 at 00:30
  • Set the url when you create the RequestDispatcher as in the example on the link I gave: RequestDispatcher dispatcher = aRequest.getRequestDispatcher(newurl); dispatcher.forward(aRequest, aResponse); – Always Learning Apr 09 '14 at 00:48
  • See UPDATE 2 above. What to use for `new url`? Should it begin with context root (e.g. `/WEB-INF/myFile.pdf#Chapter3`, I'm guessing that's from context root(?))? – ggkmath Apr 09 '14 at 00:59
  • It sounds like you want to do a non-supported include. You can achieve this effect with JavaScript (and/or `jQuery`) in the browser, but a Servlet (or a JSP) cannot send you to an anchor tag in a PDF without changing the URL (which even JavaScript will do, you'll need a frame or iframe). – Elliott Frisch Apr 09 '14 at 01:16
  • Sorry, I hadn't seen your updates. The url you give has to be one that would actually work if they typed it in. Is `myfile.pdf` accessible from https://www.mydomain.com/myFile.pdf if you type it in your browser? If not then you have to use the url that would work from a browser. – Always Learning Apr 09 '14 at 01:19
  • @ElliottFrisch I've been trying to use a Servlet to send to anchor tag in PDF by changing the URL (e.g. forwarding). Are you saying that is possible? – ggkmath Apr 09 '14 at 01:19
  • @stvcisco Good point. Well, the `https://...` IS externally accessible, but I don't think, from the link I mentioned 6 posts above, that this URL format is supported (e.g. the URL must be a path starting with /). It gives a 404 error. – ggkmath Apr 09 '14 at 01:22
  • @stvcisco are you sure the url needs to be externally accessible? Because I thought typically these forwards are used for JSP pages residing in the application server (e.g. which are not directly accessible from the client browser). – ggkmath Apr 09 '14 at 01:30
  • No, I'm not sure... The doc says it takes a path, so perhaps instead of a url you should be giving a relative path. Try a relative path like "./WEB-INF/myFile.pdf" (note the leading dot). – Always Learning Apr 09 '14 at 01:38
  • I placed the pdf file in every directory from WEB-INF to the one containing the Servlet. I tried the dot as you said, and other variants. Either I get a white browser window with no error, or the PDF reader opens in the browser but no file is shown. – ggkmath Apr 09 '14 at 01:41
  • @ggkmath You'll need to send a redirect for that... `response.sendRedirect()`... – Elliott Frisch Apr 09 '14 at 01:49
  • @ElliottFrisch a redirect will display the PDF file perfectly in the client browser. But, it also displays the URL that I need to have hidden, so it's not a solution. Is there any other way to get the PDF file in the browser without showing its file location? – ggkmath Apr 09 '14 at 02:07
  • Use JavaScript and a frame. – Elliott Frisch Apr 09 '14 at 02:11
  • @ElliottFrisch can you recommend a link? – ggkmath Apr 09 '14 at 02:14
  • @ggkmath [Here](http://stackoverflow.com/q/221192/2970947) you go. – Elliott Frisch Apr 09 '14 at 02:15
  • It appears if you right-click and view source, you'll still be able to view the "hidden" url, so it doesn't seem that Frames will work here. – ggkmath Apr 09 '14 at 02:46
  • Anything under WEB-INF is going to be inaccessible to the client. Other than a Servlet or something mapped specifically to a url-pattern in web.xml, in which case you would use that url-pattern. – david brainerd Apr 09 '14 at 04:39

0 Answers0