Reputation: 2430
I'm trying to build a Java servlet, and I've done everything according to the instructions my prof gave our class, but I'm getting a weird error.
Background: I'm working with Java EE Helios and Tomcat 7.
I started a new dynamic web project in Eclipse, I made an index.jsp page that just gets the user's name and sends it to the servlet, and is then supposed to print out Hello, [username]. The code is all sample code that the prof gave us and works for other people in my class.
I made a new Servlet called ServletHome and it's in a package called servlets.
When I run the program from Eclipse, it starts up Tomcat fine, no problems. I can navigate to the index.jsp page, and it looks fine.
The problem is that when I fill in my name and press my 'submit' button, I get a tomcat 404 error with the message: "The requested resource (/MyFirstServlet/ServletHome) is not available."
Any ideas?
Thanks!!
--- Edit: Code ---
index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ServletHome" method="POST">
First Name: <input type="text" name="firstName" size="20"><br>
Last Name: <input type="text" name="lastName" size="20"> <br>
<br> <input type="submit" value="Submit">
</form>
</body>
</html>
ServletHome.java:
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHome extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletHome() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
/**
* The function that gets the name and returns an HTML file with Hello to them
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//set type of output
response.setContentType("text/html;charset=UTF-8");
//get writer
PrintWriter out = response.getWriter();
//get first name
String firstName = request.getParameter("firstName").toString();
//get last name
String lastName = request.getParameter("lastName").toString();
//write the file
out.println("<html>");
out.println("<head>");
out.println("<title>Test Title</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>Welcome, " + firstName + " " + lastName + "!</p>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>ServletHome</servlet-name>
<servlet-class>servlets.ServletHome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletHome</servlet-name>
<url-pattern>/servlets/*</url-pattern>
</servlet-mapping>
</web-app>
Upvotes: 1
Views: 13051
Reputation: 9444
The resource is no available because:
Use @WebServlet annotation, since you are using Tomcat 7:
@WebServlet( name = "ServletName", urlPatterns = { "/path/to/your/servlet/myName" } )
public class ServletName extends HttpServlet {
// your code here
}
Or map your servlet in web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- more code here... -->
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>yout.package.here.ServletName</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/path/to/your/servlet/myName</url-pattern>
</servlet-mapping>
<!-- more code here... -->
</web-app>
Another thing that you need to pay attention is that you MUST implement the doXXX methods that corresponds to the HTTP methods (get, post, etc.) that you want your servlet to serve.
To request this servlet through you form, you need to set the action attribute as:
<form action="/path/to/your/servlet/myName">
<!-- form fields here... -->
</form>
To finish, you can use the method attribute in your form to choose the HTTP method that your browser will use to request the Servlet. If you don't provide, the default method is get. As I already said, if the get method is used, you need to implement the doGet method in the servlet.
Upvotes: 5