I'm suppose to write a servlet that returns a randomly chosen greeting from a list of five different greetings. The greetings must be stored as constant strings in the program.
This is all I've gotten so far and when I try to run it all it returns is "Hello World" which isn't even one of the greetings.
Any help would be appreciated...
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class asgnmnt {
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String[] myStrings = {"This is the first random message", "A chicken crossed the
road", "Its cold outside", "The tree is green", "School is almost over"},
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet RandomMessage</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet RandomMessage</h1>");
Random r = new Random( );
out.println(myStrings[r.nextInt(myStrings.length)]);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}}}}