-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSiteSelector.java
More file actions
30 lines (25 loc) · 1000 Bytes
/
SiteSelector.java
File metadata and controls
30 lines (25 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Program which randomly selects a site among given sites
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SiteSelector extends HttpServlet {
Vector sites = new Vector();
Random random = new Random();
public void init(ServletConfig config) throws ServletException {
super.init(config);
sites.addElement("http://www.google.com");
sites.addElement("http://www.yahoo.com");
sites.addElement("http://www.youtube.com");
sites.addElement("http://www.facebook.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 = (String)sites.elementAt(siteIndex);
res.setStatus(res.SC_MOVED_TEMPORARILY);
res.setHeader("Location", site);
}
}