找不到Java Servlet(错误404)

时间:2015-04-03 18:39:10

标签: java jsp tomcat servlets intellij-idea

注意:我现在开始使用GlassFish。

我有Intellij Idea 14.1.1和Tomcat 8.0.21,我创建了一个名为index.jsp的jsp文件和一个名为TestServlet的servlet,我已经在web.xml文件中添加了servlet。 index.jsp文件包含一个表单,其中action参数设置为TestServlet,方法是获取表单只包含一个按钮。当我运行index.jsp时,一切正常,然后当我点击按钮时,我被重定向到" http://localhost:8080/TestServlet"我得到HTTP状态404 - / TestServlet以下是该页面的完整内容:

HTTP Status 404 - /TestServlet

type Status report

message /TestServlet

description The requested resource is not available.
Apache Tomcat/8.0.21

以下是index.jsp的代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
  </head>
  <body>
  <p>Test works!</p>
  <form action="TestServlet" method="get">
    <button>click me</button>
  </form>
  </body>
</html>

这是TestServlet的代码

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;

@WebServlet(name = "TestServlet")
public class TestServlet 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("The servlet works!");
    }
}

这是web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>TestServlet</servlet-name>
            <servlet-class>TestServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>TestServlet</servlet-name>
            <url-pattern>TestServlet</url-pattern>
        </servlet-mapping>
</web-app>

我没有对TomCat配置做任何事情,我只是在创建TestServlet并更新web.xml文件时重新启动了。我错过了什么?

0 个答案:

没有答案