Redirect HTTP to HTTPS using Servlet Filter

1. Write a filter to redirect from http to https


package vn.nvanhuong.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 Redirect -->
<filter>
<filter-name>HTTPS</filter-name>
<filter-class>vn.nvanhuong.servlet.HTTPSFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HTTPS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

References
[1]. http://www.tutorialspoint.com/servlets/servlets-page-redirect.htm
[2]. http://www.easywayserver.com/blog/jsp-get-url/

Servlet HelloWorld

[Here is introduction]

1. Create a Maven Project named helloservlet

maven_project

2. Add library

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency></p>

3. HelloWorld.java

package vn.nvanhuong;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");

}

}

4. web.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>vn.nvanhuong.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>

5. Run

http://localhost:8080/helloservlet/HelloWorld

Reference:

[1]. http://www.roseindia.net/servlets/HelloWorld.shtml

Download:

https://drive.google.com/file/d/0B7JQL2BurAL4eElaMElJVkRST28/edit?usp=sharing