简体   繁体   中英

Simplest possible servlet and web.xml in netbeans

I am trying to understand how java servlets work and I'm using netbeans 7.1.1 as my IDE. This is hosted on my dev machine which is an Ubuntu VM running on a windows 7 host OS.

I used the netbeans wizard to set up my simple app, however there must be something I'm not understanding as going to the url http://localhost:8080/hssdatabase/ results in the following error message:-

javax.servlet.ServletException: PWC1397: Wrapper cannot find servlet class hss.index or a class it depends on

Here is my project folder tree:-

Netbeans项目

Here is my 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">
    <servlet>
        <servlet-name>index</servlet-name>
        <servlet-class>hss.index</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

I also tried:-

<url-pattern>/index</url-pattern>

and going to http://localhost:8080/hssdatabase/index , but that had the same result.

To deploy the app I used the new project wizard to set up a basic project, then deleted the jsp that was generated. Then deleted everything in web.xml between the tags and then used the new servlet wizard to create the servlet and fill in web.xml. I'm interested in understanding why this doesn't work rather than inspecting the steps I took to get here. I'm trying to understand the web.xml and it's relationship with the servlet at the moment. Could it be the xmlns declarations that are wrong?

And my index.java is just the bog standard default servlet generated by netbeans, so I don't think it's worth posting it here.

My question is: Is there something missing or wrong in the web.xml?

I have read everything I can find about it, but can't see anything wrong with it.

The message indicates that your hss/index.java file did not compile OR one of the files that it depends on is not in the classpath. You may want to look at the ant output associated with 'Run' or 'Deploy' action's execution. The output should look something like this:

init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/WEB-INF/classes
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/META-INF
Copying 1 file to /Users/vkraemer/NetBeansProjects/WebApplication37/build/web/META-INF
Copying 2 files to /Users/vkraemer/NetBeansProjects/WebApplication37/build/web
library-inclusion-in-archive:
library-inclusion-in-manifest:
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/empty
Created dir: /Users/vkraemer/NetBeansProjects/WebApplication37/build/generated-sources/ap-source-output
compile:
compile-jsps:
In-place deployment at /Users/vkraemer/NetBeansProjects/WebApplication37/build/web
Initializing...
run-deploy:
Browsing: http://localhost:8080/WebApplication37
run-display-browser:
run:
BUILD SUCCESSFUL (total time: 1 second)

You should also verify that the index.class file is in the $projectroot/build/web/WEB-INF/classes/hss directory. The best way to check that out is to use the Files explorer.

Make sure that in index.java, before all the imports, you declare the package:

package Interfaz;

Otherwise it won't be able to find that servlet under that package even though web.xml is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM