Redirect all websites requests via SSL/HTTPS (Rrequire Private JVM + SSL certificate) Print

  • 0

The following article shows an example only of a servlet redirect (please, note that this may or may not work depending on your configuration) of how you can redirect your normal http traffic to https:// traffic if you have already private SSL and using our Private JVM tool. 

For more information on our SSL certificates please, check

Please, note that this solution will NOT work on our Shared Tomcat implementation or if you do not have private SSL certificate.

Step 1: Write a filter to redirect from http to https

Example for such filter is listed below:

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HTTPSFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws, ServletException {

HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;

String uri = req.getRequestURI();
String getProtocol = req.getScheme();
String getDomain = req.getServerName();
String getPort = Integer.toString(req.getServerPort());

if (getProtocol.toLowerCase().equals("http")) {
// Set response content type
// New location to be redirected
String httpsPath = "https" + "://" + getDomain + ":" + getPort
+ uri;
String site = new String(httpsPath);
res.setHeader("Location", site);
// Pass request back down the filter chain
chain.doFilter(req, res);
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
public void destroy() {
// TODO Auto-generated method stub

Step 2: Compile above filter and create the following entries in your web.xml

<!-- HTTPS Redirect -->

Was this answer helpful?

« Back