HttpServletResponse: setStatus(int status) : HttpServletResponse « javax.servlet.http « Java by API






HttpServletResponse: setStatus(int status)

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SiteSelector extends HttpServlet {

  Vector<String> sites = new Vector<String>();

  Random random = new Random();

  public void init() throws ServletException {
    sites.addElement("http://www.java2s.com");
    sites.addElement("http://www.servlets.com");
    sites.addElement("http://java.sun.com/products/servlet");
    sites.addElement("http://www.google.com");
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
      IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    int siteIndex = Math.abs(random.nextInt()) % sites.size();
    String site = sites.elementAt(siteIndex);

    res.setStatus(res.SC_MOVED_TEMPORARILY);
    res.setHeader("Location", site);
  }
}

           
       








Related examples in the same category

1.HttpServletResponse: addCookie(Cookie arg0)
2.HttpServletResponse.SC_BAD_REQUEST
3.HttpServletResponse.SC_MOVED_TEMPORARILY
4.HttpServletResponse: encodeURL(String url)
5.HttpServletResponse: sendRedirect(String arg0)
6.HttpServletResponse: setContentType( text/html; charset=UTF-8 );
7.HttpServletResponse: setHeader(String name, String value)
8.ServletResponse: setBufferSize(int arg0)
9.HttpServletResponse: setContentType("text/vnd.wap.wml")
10.HttpServletResponse: sendError(int arg0, String arg1)
11.HttpServletResponse: setHeader(String name, String value) (2)