Pradeep
Pradeep

Reputation: 1223

correct file structure for mvc (servlets,jsp,web.xml)

I am writing a simple mvc application called FirstApp.The file structure for the project is as follows:

FirstApp
|--entername.html
|--WEB-INF
    |--web.xml
    |--classes
          |--web
             |--EnterName.class
          |--model

When i try to run it on the tomcat server,it gives me a HTTP 500 error. HTTP 500 ERROR
My deployment descriptor is correct is correct as far as i know.

<web-app 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"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to first servlet</display-name>
  <description>
     Welcome to My first servlet on linux
  </description>

  <servlet>
    <servlet-name>first servlet</servlet-name>
    <servlet-class>web.EnterName</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>first servlet</servlet-name>
    <url-pattern>/Enter.do</url-pattern>
  </servlet-mapping>

</web-app>

EnterName.java file

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

public class EnterName extends HttpServlet {
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
    {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        pw.println("hello how are you ");
    }
}

What's causing the error?There are no files in the model folder as of now

Upvotes: 0

Views: 746

Answers (1)

JB Nizet
JB Nizet

Reputation: 692053

Your class is not in the web package as you claim it is in the web.xml file, and in the directory structure.

Add

package web; 

as the very first line of the class.

The directory structure under WEB-INF/classes must match exactly with the package structure. The class is named web.EnterName only if its actual package is web, and its simple name is EnterName.

Upvotes: 1

Related Questions