如何将请求从servlet重定向到https

时间:2013-02-25 13:46:58

标签: java jsp servlets

我有一个网站,欢迎文件是servlet。

<welcome-file-list>
    <welcome-file>Main</welcome-file>
 </welcome-file-list>

servlet从数据库收集数据并将其发送到JSP。如何将所有请求从主servlet重定向到https? 任何的想法??? 感谢

1 个答案:

答案 0 :(得分:3)

感谢 Eng.Fouad 提示...... 问题解决了

//Check if the requested scheme is http then redirect it to https
if(request.getScheme().equals("http"))
{
    response.sendRedirect("https://www.mysite.com");
}
//If the request is not http but https then collect the data and send to jsp
else
{
        //Collect the data from the database and send it to JSP
        request.setAttribute("data",data);
        RequestDispatcher rd = request.getRequestDispatcher("/main.jsp");
    rd.forward(request, response);
}
相关问题