简体   繁体   English

如何在Servlet中获取HTML请求URL

[英]How to get html request URL in a servlet

Am using a HTML form like this, 我正在使用这样的HTML表单,

<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 例如,此HTML表单的路径为http://www.mywebapp.com/sample.html

In my servlet when i use the String url = req.getRequestURL().toString(); 在我的servlet中,当我使用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. 并打印出字符串,它会打印表单操作URL http://www.mywebapp.com/myservlet?userid=12345,而不是HTML URL。

The expected HTML URL : http://www.mywebapp.com/sample.html (from here only i request and i need this URL) 预期的HTML URL: http : //www.mywebapp.com/sample.html (仅从这里我请求,并且我需要此URL)

Can anyone suggest me how to get the HTML url path in the servlet. 谁能建议我如何在Servlet中获取HTML网址路径。

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). 如果我对您的理解正确,那么当您发出后续请求( form发布)时,您想要获取一个请求(加载html的请求)的URL。

HTTP is stateless and therefore there is no way to reliably do this. HTTP是无状态的,因此无法可靠地执行此操作。

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 . 需要执行此操作时,会有一些代码异味,但是如果您确实需要URL,则必须将URL作为form的隐藏input传递。 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: 无论您使用JSP / JSTL还是使用JavaScript进行客户端操作,都有多种方法可以做到这一点,但是基本上,您尝试获取这样的form

<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>

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 这是某种问题,因为在您编写时,用户加载了http://www.mywebapp.com/sample.html页面,并且在此初始请求中,您将从request.getRequestURL().toString获得此值

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. 在下一个使用表单(从客户端)的请求中,URL不再是http://www.mywebapp.com/sample.html而是form action属性中指定的URL。

There are many suggestion of passing hidden value in form or something like this, but you can easly make use of simple HTTP. 有许多建议以诸如此类的形式传递隐藏值,但是您可以轻松地使用简单的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. 每次执行GET请求时,都会在浏览器(或其他客户端)窗口中获得新的URL,但是执行POST请求时,浏览器中的URL不会更改。 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: 因此,现在在您的代码中,如果您知道这是一个POST请求,并且想要从该请求进行处理的地方获取页面,则可以轻松地从Http Header“ Referer”中读取它,如下所示:

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

This should work the same as all "hidden field" examples 这应与所有“隐藏字段”示例相同

Your 'HTML Page' is called the "Referrer" and you can get it in this way 您的“ HTML页面”称为“引荐来源网址”,您可以通过这种方式获取它

HttpServletRequest.getHeader("Referer");

It will return the page where the form has been submitted, in your case 在您的情况下,它将返回提交表单的页面

http://www.mywebapp.com/sample.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM