0

I have a simple random number generator built. I was trying to use a servlet with a form. The first page pulls up just fine. After hitting the button to get the random numbers I get 'HTTP Status 404 - Not_Found' The requested resource is not available.

I have looked for anything I might have mistyped, or missing any ':' etc. It all looks good to me so I was hoping that someone could put a different set of eyes on it and maybe tell me what a noob I am.

Here is my JSP file:

<%@page import="java.util.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<HTML>
<HEAD><TITLE>Random Numbers Page</TITLE></HEAD>
<BODY BGCOLOR="white">
<H1>Random Numbers Page</h1>
<P>This will display 20 random numbers </P>
<FORM METHOD=POST         ACTION="${pageContext.request.contextPath}/randomNumberServlet">
<BR>
<INPUT TYPE="SUBMIT" VALUE="Get Numbers Here">
</FORM>
</BODY>
</HTML>

And here is my servlet:

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

public class randomNumberServlet extends HttpServlet {
protected final int DEFAULT_NUMBER = 20;

 /** Called when the form is filled in by the user.
 * @param req
 * @param resp
 * @throws java.io.IOException */
 @Override
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter( );

// HTML setup stuff.
out.println("<HTML>");
out.println("<HEAD>");
out.println("<BODY BGCOLOR=\"white\">");

// HTML for this page
out.println("<TITLE>Random Numbers</TITLE>");
out.println("<P>Here are the 20  random numbers,");

// Now actually generate some random numbers.
// Now actually generate some random numbers.
Random r = new Random(100);
for (int i=0; i<20; i++) {
  out.print("<LI>");
  out.println(r.nextInt());  

out.println("</OL>");
}
}
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Luis Ramos
  • 529
  • 1
  • 6
  • 13
  • Can you post your web.xml. Also is the servlet in a package or default package. If the servlet is in default package move it to a package name and try – Avinash Reddy Aug 22 '15 at 07:24
  • I am not sure how to get that. I will google it and learn though. In the end after working on this for more than 20 hours in two days I figured it out. I made this more complicated then it really was. – Luis Ramos Aug 22 '15 at 16:09
  • Ended up putting the regular java code inside the JSP file. The only thing I had done wrong was trying to put it in the '_body'. I had to put it outside of any area designated already (body, head, title etc). – Luis Ramos Aug 22 '15 at 16:11
  • I still want to figure this out using the above method so I will get the web.xml for you. Thank you so much! – Luis Ramos Aug 22 '15 at 16:12

1 Answers1

0

Most probably the url mapping /randomNumberServlet does not point to the randomNumberServlet servlet. You have 2 options:

  1. In your web xml define your servlet and a URL mapping
  2. Or, If your servlet container is Servlet 3.0 Specification complaint, you can use Servlet Annotations.

Here's a Servlet Annotation example:

@WebServlet(name="randomNumberServlet", urlPatterns={"/randomNumberServlet"}) 
public class randomNumberServlet extends HttpServlet { ... }
Community
  • 1
  • 1
chrisl08
  • 1,658
  • 1
  • 15
  • 24