1

我直接在Tomcat的webapp目录下写了一个简单的web app,通过localhost url访问了hello.jsp文件。我现在要做的是对上一步中编译的 servlet 类进行 url 映射。我的 web.xml 代码如下:

我希望能够指向以“/hi”结尾的 url 并运行已编译的 jsp。任何帮助,将不胜感激。

<welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>hi</servlet-name>
        <servlet-class>org.apache.jsp.hello_jsp</servlet-class>
</servlet>

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

目前,当我尝试从指定的 url 模式访问它时,我得到了这个堆栈跟踪:

type Exception report

message Error instantiating servlet class org.apache.jsp.hello_jsp

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

exception

javax.servlet.ServletException: Error instantiating servlet class org.apache.jsp.hello_jsp
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    java.lang.Thread.run(Thread.java:722)
root cause

java.lang.ClassNotFoundException: org.apache.jsp.hello_jsp
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1822)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    java.lang.Thread.run(Thread.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.35 logs.
4

1 回答 1

3

这确实是不对的。您需要使用<jsp-file>而不是<servlet-class>. 毕竟,您在源代码中只是一个 JSP 文件,而不是一个 servlet 类。

<servlet>
    <servlet-name>hi</servlet-name>
    <jsp-file>/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>hi</servlet-name>
    <url-pattern>/hi</url-pattern>
</servlet-mapping>
于 2013-03-28T00:05:33.217 回答