0

I created a Dynamic Web Project. I have Tomcat 7.0 installed to run it. This is my first time making a Website with java so I'm completely clueless as to what the problem might be. Here is my code:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>SimpleTest</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet> 
  <servlet-name>Hello</servlet-name>
  <servlet-class>Hello</servlet-class> 
  </servlet> 
  <servlet-mapping> 
  <servlet-name>Hello</servlet-name> 
  <url-pattern>/Hello</url-pattern> 
  </servlet-mapping>
</web-app>

Hello.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Hello
 */
@WebServlet("/Hello")
public class Hello extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Hello() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hello");
    }

}

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="Hello" method="post">
        Name: <input type="text" name="name" value="" />
        <input type="submit" name="name" value="Submit" />
    </form>
</body>
</html>

The app just prints "Hello" when submit is clicked. I made it like this because I just wanted to make sure the servlet was working first.

NeedsHelp
  • 427
  • 1
  • 7
  • 24
  • What are you using to build your project? Is Hello.class in the WEB-INF directory in either /WEB-INF/classes or in a jar which is in /WEB-INF/lib ? – slipperyseal Apr 07 '15 at 15:03
  • Oh, I'm using Eclipse, I should have said that. It's in myApp/src/. Should I export it to a jar, into /WEB_INF/lib? – NeedsHelp Apr 07 '15 at 15:45
  • Why are you mapping Hello using both xml and annotation? keep it only one. – Darshan Patel Apr 07 '15 at 15:52
  • So I should remove servlet tags from the web.xml? I only added them after my app wouldn't work and it was suggested. I'll try it. – NeedsHelp Apr 07 '15 at 15:56

4 Answers4

3

Servlet can be mapped using web.xml or annotation. You are using both of them.

Remove below portion from web.xml then clean and build your project then you are good to go.

<servlet> 
    <servlet-name>Hello</servlet-name>
    <servlet-class>Hello</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Hello</servlet-name> 
    <url-pattern>/Hello</url-pattern> 
</servlet-mapping>
Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
0

Remove @WebServlet("/Hello") from Servlet or servlet mapping from web.xml.Both doing same job, so remove one of the configuration.

 <servlet> 
  <servlet-name>Hello</servlet-name>
  <servlet-class>Hello</servlet-class> 
  </servlet> 
  <servlet-mapping> 
  <servlet-name>Hello</servlet-name> 
  <url-pattern>/Hello</url-pattern> 
  </servlet-mapping>
Annamalai Thangaraj
  • 522
  • 1
  • 5
  • 10
0

You are doing the same thing with web.xml and annotations , and with 3.0 or greater mostly it makes the decision engine to open both channel.For smaller projects like above it is better to change it with annotations only.With bigger projects or deploying servlet manually go for web.xml.

Also on servlet mapping use proper file representations.

SG001
  • 1
  • 1
0

You have to give the class name as packagename.classname in the Hello. You must have a path instead of using the classname directly

Surya
  • 1