Example usage for javax.servlet.http HttpServletResponse SC_SWITCHING_PROTOCOLS

List of usage examples for javax.servlet.http HttpServletResponse SC_SWITCHING_PROTOCOLS

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_SWITCHING_PROTOCOLS.

Prototype

int SC_SWITCHING_PROTOCOLS

To view the source code for javax.servlet.http HttpServletResponse SC_SWITCHING_PROTOCOLS.

Click Source Link

Document

Status code (101) indicating the server is switching protocols according to Upgrade header.

Usage

From source file:org.sample.protocolhandler.UpgradeServlet.java

/**
 * Processes requests for both HTTP//from   ww  w  . ja va  2s .  c o m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet UpgradeServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet UpgradeServlet at " + request.getContextPath() + "</h1>");
        if (request.getHeader("Upgrade").equals("echo")) {
            response.setStatus(HttpServletResponse.SC_SWITCHING_PROTOCOLS);
            response.setHeader("Connection", "Upgrade");
            response.setHeader("Upgrade", "echo");
            request.upgrade(MyProtocolHandler.class);
            System.out.println("Request upgraded to MyProtocolHandler");
        }
        out.println("</body>");
        out.println("</html>");
    }
}