在HttpServlet中强制从HTTPS到HTTP的请求

问题描述:

我有一个链接使用

https://和http://

之前。

当我使用htppservlet从https://请求时,
我想要响应返回http://
因此客户端可以更轻松地查看。

When I use htppservlet to request from a "https://" , I want the response return a "http://" . So the client can view easier.

我该怎么办?

您可以使用以下代码执行此操作:

You can do that with the code like this:

if (request.isSecure()) { // it is HTTPS
    String reqUrl = req.getRequestURL().toString().replaceFirst("https:", "http:");
    String queryString = req.getQueryString();
    if (queryString != null)
        reqUrl += '?' + queryString;
    response.sendRedirect(reqUrl);
}