1

I am learning how to make web applications and have just implemented a simple one that should display the current date on the web page. However, I am receiving an error while doing so. I ran it on debug mode with a breakpoint placed on: resp.getWriter().println(new Date());

Here is my Java Code:

package org.test.webapp;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.getWriter().println(new Date());
    }

}

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

    <servlet>
        <servlet-name>Test</servlet-name>
        <servlet-class>org.test.webapp/TestServlet</servlet-class>
    </servlet>

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



    <display-name>Example01</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>
</web-app>

This is the error I am receiving:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error allocating a servlet instance
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)

root cause

java.lang.NoClassDefFoundError: IllegalName: org.test.webapp/TestServlet
    java.lang.ClassLoader.preDefineClass(Unknown Source)
    java.lang.ClassLoader.defineClass(Unknown Source)
    java.security.SecureClassLoader.defineClass(Unknown Source)
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2818)
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1159)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs.
5
  • The Error is when I enter: localhost:8080/Example01/test on the URL Commented Dec 8, 2013 at 21:03
  • I think the error is in the definition of servlet, in web.xml Commented Dec 8, 2013 at 21:05
  • By the way, if you're wanting to start working with web applications, I strongly recommend using a framework like Spring MVC instead of handling all the servlet details yourself; they can provide plumbing features like URL parsing, content negotiation, and response building for you. Commented Dec 8, 2013 at 21:09
  • Sidenote: be sure to close the writer by wrapping it in a try-finally block. Something like: PrintWriter writer = null; try { writer = resp.getWriter(); writer.println(new Date()); } finally { if( writer != null ) writer.close(); } Commented Dec 8, 2013 at 21:09
  • I am trying to create an application where you are able to convert video's to different formats where the video is saved on a web server so that the user can access it once the conversion is complete. What would you recommend as the best way of approaching this? Commented Dec 8, 2013 at 22:27

1 Answer 1

3

your serlvet class should reference a class name. so instead of this:

<servlet-class>org.test.webapp/TestServlet</servlet-class>

which is an illegal class name (that your server fails to find, hence the exception), try this:

<servlet-class>org.test.webapp.TestServlet</servlet-class>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.