AL̲̳I
AL̲̳I

Reputation: 2461

How to redirect requests to https from a servlet

I have a website where the welcome-file is a servlet.

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

The servlet collects the data from the database and send it to JSP. How can I redirect all the requests to https from the main servlet? Any idea??? Thanks

Upvotes: 0

Views: 5303

Answers (1)

AL̲̳I
AL̲̳I

Reputation: 2461

Thanks to Eng.Fouad for the hint... Problem solved

//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);
}

Upvotes: 3

Related Questions