0

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();
}}}}
2
Contributors
3
Replies
9
Views
4 Years
Discussion Span
Last Post by JorgeM
0

There's no "Hello World" string in your code. This leads me to believe that either you are running a different page/code, or you have not saved your changes, or haven't uploaded your code to the right place, etc..

0

here's my .jsp file...Also asgment has been changed to checkMate.

Now I get the following error:

HTTP Status 404 -

type Status report

message

descriptionThe requested resource () is not available.

GlassFish Server Open Source Edition 3.1.1

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action ="checkMate"  method ="get">
            <p>
                Press the button to run the Servlet
                <input type ="submit"  value ="Run Servlet" />
            </p>
        </form>
    </body>
</html>
0

A 404 error means "page not found". I suspect you get that error when you try to submit the form. Your form action is equl to "checkMate". Maybe that should be "checkMate.jsp" or something similar?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.