简单的Servlet项目(HTTP状态404错误)

4

我想创建一个servlet项目。

我的Java类名为AzpanaServlet.java,其中包含一个内部类。(编译后会生成2个类文件)。

我的项目是一个简单的应用程序,接收一个字符串输入并对其进行一些操作(与主题无关)。

当我点击"提交"按钮时,出现以下错误:

HTTP Status 404 - /AzpanaServlet
Type Status report
Message /AzpanaServlet
Description The requested resource (/AzpanaServlet) is not available.
Apache Tomcat/6.0.18

如果您能帮忙,我将不胜感激。我已经花费了很多时间也无法解决这个问题。 以下是我的Java代码:

public class AzpanaServlet extends HttpServlet {
//
//Some functions 
//
//Inner class: public class oneChar{...}
//

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
        * Get the value of form parameter
        */
        String name = request.getParameter("name");
        /*
        * Set the content type(MIME Type) of the response.
        */
        response.setContentType("text/html");
        String str = "";
        PrintWriter out = response.getWriter();
        ArrayList<Integer> list = new ArrayList<Integer>();
        try {
            list = mainmanu(name); //not relevant function.
        } catch (Exception e) {
              str  = e.toString();

            e.printStackTrace();
        }
        /*
        * Write the HTML to the response
        */
        out.println("<html>");
        out.println("<head>");
        out.println("<title> this is your answers</title>");
        out.println("</head>");
        out.println("<body>");
        if(str != ""){
            out.println(str);
        }
        else{
        for(int i = 0;i<=40;i++){
            out.println(list.get(i));
            out.println("<br>");

        }
        }
        out.println("<a href='form.html'>"+"Click here to go back to input page "+"</a>");
        out.println("</body>");
        out.println("</html>");
        out.close();

        }


        public void destroy() {

        }
    }

我的web.xml代码:

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
        <servlet-name>AzpanaServlet</servlet-name>
        <servlet-class>com.example.AzpanaServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AzpanaServlet</servlet-name>
        <url-pattern>/AzpanaServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/form.html</welcome-file>
    </welcome-file-list>
</web-app>

我的form.html代码:

<html>
    <head>
    <title>Zeev's Project</title>
    </head>
    <body>
        <h1>Just Enter String</h1>
        <form method="POST" action="AzpanaServlet">
            <label for="name">Enter String</label>
            <input type="text" id="name" name="name"/><br><br>
            <input type="submit" value="Submit Form"/>
            <input type="reset" value="Reset Form"/>
        </form>
    </body>
</html>

文件夹的层次结构如下:

ROOT[
    WEB-INF[
        web.xml
        classes[
               com[
               example[
                    AzpanaServlet.class
                AzpanaServlet$oneChar.class
                ]
              ]
               ]
        lib[
            AzpanaServlet.java
           ]
       ]
    META-INF[
        MANIFEST.MF
        ]
    form.html
    ]
3个回答

0

复制了您的代码,只是将Servlet代码注释掉了

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*
        * Get the value of form parameter
        */
        String name = request.getParameter("name");

        System.out.println("Parameter name : "+name);

非常好用。

在我的服务器控制台中。

Parameter name : helloo

你的配置没有问题。您可能需要清除浏览器缓存,然后重试。


0

请查看您的IDE控制台或Tomcat日志,您的Web应用程序是否成功启动?
由于某些原因,它可能没有启动。


0
问题可能是您正在使用IDE运行Tomcat服务器并部署到根应用程序上下文。尝试在部署中为您的应用程序使用一些应用程序上下文。例如/myapp

@zeevroyt 你需要重新安装tomcat,因为你的ROOT文件夹已被覆盖。 - Roman C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接