i have
project folder name: website_test
package name: hello_servet
testing_01.java
web.xml
index.html

i need help with servlet name. i think iam names wrong. for ex is servlet-name the class name or project folder name??
web.xml file:

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <servlet>
    <servlet-name>website_test</servlet-name>
    <servlet-class>hello_servet.testing_01</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>website_test</servlet-name>
    <url-pattern>/website_test</url-pattern>
  </servlet-mapping>

</web-app>

Dani AI

Generated

asked whether servlet-name should be the class name or the project folder name; requested to see the package/controller. The key points are: servlet-name is just an internal label used inside web.xml (it can be any string). servlet-class must be the fully qualified Java class name (the package + class as declared in the servlet source) and must match the compiled class file placement. The project folder (the webapp name) becomes the webapp context path (the first segment of the URL) and is not used by servlet-class.

A minimal servlet example (uses Servlet 3 annotations so web.xml is optional):

package com.example.hello;

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

@WebServlet("/test")
public class TestServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.getWriter().write("OK");
    }
}

If using web.xml, the servlet-class entry must match the package+class exactly:

<servlet>
  <servlet-name>TestServlet</servlet-name>
  <servlet-class>com.example.hello.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>TestServlet</servlet-name>
  <url-pattern>/test</url-pattern>
</servlet-mapping>

Troubleshooting checklist: confirm the package line at the top of the Java file matches the folder path under WEB-INF/classes (or contents of the WAR), ensure the Java filename matches the class name (case-sensitive), rebuild and redeploy, and check server logs for ClassNotFoundException or NoClassDefFoundError. Remember the final servlet URL is the context root (often the project/WAR name, e.g. /website_test) plus the url-pattern (so /website_test/test in the examples). Using @WebServlet removes the need for a web.xml mapping in Servlet 3+.

Member Avatar for Member #949455

i need help with servlet name. i think iam names wrong. for ex is servlet-name the class name or project folder name??

This is your code:

<?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" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0">

<servlet>
<servlet-name>website_test</servlet-name>
<servlet-class>hello_servet.testing_01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>website_test</servlet-name>
<url-pattern>/website_test</url-pattern>
</servlet-mapping>

You're missing some info here. How does the package controller looks like?

I mean you wrote this code.

In order to write it you have to look at your package controller code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.