Friday, May 29, 2015

Redirect HTTP to HTTPS using Servlet Filter

Method - I
1. Write a filter to redirect from http to https
package com.sample.servlet;
 
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 java.io.IOException, 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
response.setContentType("text/html");
 
// New location to be redirected
String httpsPath = "https" + "://" + getDomain + ":" + getPort
+ uri;
 
String site = new String(httpsPath);
res.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
res.setHeader("Location", site);
}
 
// Pass request back down the filter chain
chain.doFilter(req, res);
 
}
 
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
 
}
 
@Override
public void destroy() {
// TODO Auto-generated method stub
 
}
 
}
2. Compile above filter and create following entries in web.xml


HTTPS
com.sample.servlet.HTTPSFilter


HTTPS
/*

Method - II
1. Add the below snippet to your Web.xml


SessionTest
/*


CONFIDENTIAL


I preferably use Method - II , if not work's for some servers like Trifork 4.1.3.6 go for Method -I . I hope this article will helps you.

No comments:

Post a Comment