0

Am using a HTML form like this,

<form action="/myservlet?userid=12345" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Submit">
</form>

And the path of this HTML form is say for example, http://www.mywebapp.com/sample.html

In my servlet when i use the String url = req.getRequestURL().toString(); and printout the string it prints the form action URL http://www.mywebapp.com/myservlet?userid=12345 and not the HTML URL.

The expected HTML URL : http://www.mywebapp.com/sample.html (from here only i request and i need this URL)

Can anyone suggest me how to get the HTML url path in the servlet.

sathya
  • 384
  • 1
  • 8
  • 19

3 Answers3

3

If I understand you correctly you want to get the URL of one request (the request that loaded the html) when you make a subsequent request (the form post).

HTTP is stateless and therefore there is no way to reliably do this.

Needing to do this has a bit of a whiff of a code smell about it, but if you really need the URL you're going to have to pass the URL as a hidden input in the form. There's various ways to do this, whether you use JSP/JSTL or do something client side with JavaScript, but basically you trying to get a form like this:

<form action="/myservlet?userid=12345" method="post" enctype="multipart/form-data">
   <input type="file" name="file">
   <input type="hidden" name="url" value="http://www.mywebapp.com/sample.html"/>
   <input type="submit" value="Submit">
</form>
Nick Holt
  • 33,455
  • 4
  • 52
  • 58
  • Yes you understood it correctly. The problem i face here is client may use any device and from that HTML page request will be send to my servlet. So its not possible for me to ask client to send the URL as Hidden. Do you have any other idea? – sathya Jan 22 '13 at 09:53
  • Persumably if you don't control the client html then it is being loaded from another webapp or even server? If it was in the same webapp you might have been able to do something with a session, but doesn't sound like that's the case. The bottom line is that if the URL is important to the servlet then it's one of the parameters required by the servlet and any client will have to adhere to that just as they do the other parameters. – Nick Holt Jan 22 '13 at 10:02
  • When i use `multipart/form-data` am getting the hidden field value as null in servlet can you tell how to solve it – sathya Jan 22 '13 at 11:48
  • Sorry, I copied the `from` from your original question - you can drop the `multipart/form-data` bit. I'd then start by hardcoding the value, then once that works I'd set the value using the *JSTL* url tag. – Nick Holt Jan 22 '13 at 13:08
  • Nick i got that link successfully. Now i want to dispatch the output to the URL which is passed as hidden. I tried with Request Dispatcher but am failing can give idea in that – sathya Jan 22 '13 at 13:13
  • If the `RequestDispatcher` is used to *forward* the request to another resource on the server, something normally done with `GET` requests. Given your form is a `POST` and the `url` parameter potentially points to a request on another server I think you want to use a `redirect`. See: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html#sendRedirect(java.lang.String) – Nick Holt Jan 22 '13 at 13:40
  • yes but redirect shows 302 status which should not happen. Can you see this link i just posted that problem here... http://stackoverflow.com/questions/14459574/how-to-forward-the-requestdispatcher-to-a-remote-url/14459657#comment20139318_14459657 – sathya Jan 22 '13 at 13:43
3

It's some kind of problem, because user loaded, as you write, page http://www.mywebapp.com/sample.html and at this initial request you will get from request.getRequestURL().toString exactly this value

On next request with use a form (from client side) url is not anymore this http://www.mywebapp.com/sample.html but the one specified in form action atributte.

There are many suggestion of passing hidden value in form or something like this, but you can easly make use of simple HTTP. Everytime when you do GET request you get a new URL in your browser (or other client) window but when you do POST request URL in browser doesn't change. So now in your code if you know that this is a POST request and you want to get page from where this request is comming you can easly read it from Http Header "Referer" as:

String URLfromWherePostWasMade = request.getHeader("Referer"); 

This should work the same as all "hidden field" examples

Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23
1

Your 'HTML Page' is called the "Referrer" and you can get it in this way

HttpServletRequest.getHeader("Referer");

It will return the page where the form has been submitted, in your case

http://www.mywebapp.com/sample.html
BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • I tried with it but if its HTTPS den it returns null value see this link http://allben.net/post/2009/02/25/Null-Url-Referrer-going-from-HTTPS-to-HTTP – sathya Jan 22 '13 at 10:23
  • If it returns a null value, it means that the HTTP referrer was not filled. HTTP referrer is filled by the browser, and HTTPS connections don't have a http referrer: `Clients SHOULD NOT include a Referer header field in a (non-secure) HTTP request if the referring page was transferred with a secure protocol` So you cannot do it if you use a HTTPS connection. Source: http://www.w3.org/Protocols/rfc2616/rfc2616-sec15.html#sec15.1.3 – BackSlash Jan 22 '13 at 10:38