我有一个HttpServletRequest对象.
如何获得导致此调用的完整且准确的URL到达我的servlet?
或者至少尽可能准确,因为可能存在可以重新生成的东西(可能是参数的顺序).
function demo(request,response){
request.getScheme() is returning http instead of returning https.
System.out.println(""+request.getScheme());
}
Run Code Online (Sandbox Code Playgroud)
输出:HTTP
- 正在从main方法调用上面的函数演示但它打印http而不是它应该在互联网服务器上打印https.
我正在处理托管在Tomcat服务器上的Java Web应用程序.我必须设置从www到非www和从http到https的重定向.我想要以下三个网址:
重定向到
为此,我使用UrlRewriteFiltertuckey.org的4.0.3版.这是我的urlrewrite.xml档案:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>
<rule>
<name>Redirect www to non-www and http to https</name>
<condition type="request-url" operator="equal">(^http://example.com|^http://www.example.com|^https://www.example.com)</condition>
<from>^(.*)$</from>
<to type="permanent-redirect" last="true">https://example.com$1</to>
</rule>
</urlrewrite>
Run Code Online (Sandbox Code Playgroud)
重定向工作但网站没有加载,浏览器显示消息:
这个页面不起作用
example.com重定向了你太多次了.
我使用了重定向检查器,发现在初次重定向到https://example.com/之后,另一个重定向到https://example.com/,然后是另一个,依此类推 - 该URL重定向到自身.我不明白是什么产生了这种无限循环.任何帮助,将不胜感激!
更新:我还没有解决方案.如果我从条件元素中删除第一个URL,则其他两个重定向工作正常,但问题是如何从http://example.com设置重定向.
我尝试了另一种方法 - 通过粘贴以下代码在web.xml文件中设置重定向到https:
<security-constraint>
<web-resource-collection>
<web-resource-name>all</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
Run Code Online (Sandbox Code Playgroud)
结果是相同的 - https://example.com在无限循环中重定向到自身.在这种情况下唯一的区别是重定向是状态代码302.任何关于导致此问题的原因以及如何解决它的想法?
更新:这是使用时curl命令的输出UrlRewriteFilter:
运行结果: curl http://example.com
HTTP/1.1 301 Moved …Run Code Online (Sandbox Code Playgroud) 我正在Spring MVC控制器中构建一个新的URL,以传递回客户端.目前我正在尝试这个:
// httpRequest is the current HttpServletRequest
new URL(httpRequest.getProtocol(),
httpRequest.getServerName(),
httpRequest.getServerPort(),
httpRequest.getContextPath().concat("/foo/bar.html"));
Run Code Online (Sandbox Code Playgroud)
问题是httpRequest.getProtocol()给我"HTTP/1.1"而不仅仅是"HTTP".我可以修剪它,但想知道是否有更优雅的方式.
我试图看看端口80和端口443是否被定义为任何地方的公共常量.这些是否存在于JDK中(或者可能存在于Apache HttpClient等公共库中)?