2015-01-12 13 views
0

我创建了一个简单的应用程序,登陆到index.jsp后,用户可以单击链接并使用链接到此链接的servlet。 问题是我没有看到/ Library/Tomcat/work/Catalina/localhost(index.jsp类存在)中生成的servlet类。如何解决从Tomcat 8/Library/Tomcat/work/Catalina/localhost目录中缺少的servlet类?

以下是相关文件:

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.skiabox.webapps</groupId> 
    <artifactId>TheWebApp1</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>TheWebApp1 Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>3.1.0</version> 
     <scope>provided</scope> 
    </dependency> 
    </dependencies> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.apache.tomcat.maven</groupId> 
     <artifactId>tomcat7-maven-plugin</artifactId> 
     <version>2.2</version> 
     <configuration> 
      <url>http://localhost:8080/manager/text</url> 
      <server>TomcatServer</server> 
     </configuration> 
     </plugin> 
    </plugins> 
    <finalName>TheWebApp1</finalName> 
    </build> 
</project> 

的index.jsp:

<html> 
    <head> 
     <title>My Home Page</title> 
    </head> 
<body> 
<h2>Hello World!</h2> 

Hello from index.jsp! 
Check your HelloWorld servlet <a href="HelloWorld">here</a> 
</body> 
</html> 

的web.xml:

<!DOCTYPE web-app PUBLIC 
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd" > 

<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"> 
    <display-name>Archetype Created Web Application</display-name> 
</web-app> 

HelloWorld.java:

package gui; 

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

/** 
* Created by Administrator on 12/01/15. 
*/ 
@WebServlet(name = "/HelloWorld/*") 
public class HelloWorld extends HttpServlet { 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     PrintWriter out = response.getWriter(); 

     out.println("<html>"); 
     out.println("<b>Hello World</b>"); 
     out.println("</html>"); 
    } 
} 

我使用Tomcat Maven插件为项目的编译和部署(你可以很容易地从pom.xml文件看到)

回答

1

你有没有困惑webservlet与urlpatterns的名字吗?尝试:

@WebServlet("/HelloWorld/*") 

或可能:

@WebServlet(name = "HelloWorld", urlPatterns = "/HelloWorld/*") 
+0

非常感谢您!我试图使用urlPatterns,但没有这种模式。 – skiabox

相关问题