简体   繁体   English

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

[英]Simple servlet project (HTTP Status 404 error)

I want to create a servlet project. 我想创建一个servlet项目。

My Java class is called AzpanaServlet.java , and it contains an inner class. 我的Java类叫做AzpanaServlet.java ,它包含一个内部类。 (When I compile it, I have 2 class files). (当我编译它时,我有2个类文件)。

My project is a simple application that receives a string input and does some stuff with it (not relevant). 我的项目是一个简单的应用程序,它接收一个字符串输入并用它做一些事情(不相关)。

When I press on the "submit" button I receive the following error: 当我按下"submit"按钮时,我收到以下错误:

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

Please help me if you can, I can't solve this much time. 请尽可能帮助我,我无法解决这么多时间。 this is my Java code: 这是我的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() {

        }
    }

My web.xml code: 我的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>

My form.html code: 我的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>

The hierarchy of the Folder is following: 文件夹的层次结构如下:

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

Copied your exact code and just commented out the Servlet code to be 复制了您的确切代码,并注释掉了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);

Works like a charm. 奇迹般有效。

In my server console 在我的服务器控制台中

Parameter name : helloo

There is no problem with your configuration. 您的配置没有问题。 You might want to clean your browser cache and try again. 您可能希望清理浏览器缓存并重试。

The problem may be that you are using IDE to run the Tomcat Server and deployed to the root application context. 问题可能是您使用IDE运行Tomcat服务器并部署到根应用程序上下文。 Try to use some app context for your application in deployment. 尝试在部署中为应用程序使用某些应用程序上下文。 For example /myapp . 例如/myapp

Have a look at your IDE console or your tomcat log, did your web application ever started successfully? 看看您的IDE控制台或您的tomcat日志,您的Web应用程序是否已成功启动?
It might didn't start for some reason. 它可能由于某种原因而无法启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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