Java Servlet 错误“资源不可用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11620150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:48:15  来源:igfitidea点击:

Java Servlet error 'Resource Not Available'

javajsptomcatservlets

提问by BIU

I'm trying to build a Java servlet, and I've done everything according to the instructions my prof gave our class, but I'm getting a weird error.

我正在尝试构建一个 Java servlet,并且我已经按照教授给我们班级的说明完成了所有工作,但是我遇到了一个奇怪的错误。

Background: I'm working with Java EE Helios and Tomcat 7.

背景:我正在使用 Java EE Helios 和 Tomcat 7。

I started a new dynamic web project in Eclipse, I made an index.jsp page that just gets the user's name and sends it to the servlet, and is then supposed to print out Hello, [username]. The code is all sample code that the prof gave us and works for other people in my class.

我在 Eclipse 中启动了一个新的动态 Web 项目,我创建了一个 index.jsp 页面,该页面仅获取用户名并将其发送到 servlet,然后应该打印出 Hello,[用户名]。代码是教授给我们的所有示例代码,适用于我班上的其他人。

I made a new Servlet called ServletHome and it's in a package called servlets.

我创建了一个名为 ServletHome 的新 Servlet,它位于一个名为 servlets 的包中。

When I run the program from Eclipse, it starts up Tomcat fine, no problems. I can navigate to the index.jsp page, and it looks fine.

当我从 Eclipse 运行该程序时,它可以正常启动 Tomcat,没有问题。我可以导航到 index.jsp 页面,它看起来不错。

The problem is that when I fill in my name and press my 'submit' button, I get a tomcat 404 error with the message: "The requested resource (/MyFirstServlet/ServletHome) is not available."

问题是,当我填写我的名字并按下我的“提交”按钮时,我收到一个带有消息的 tomcat 404 错误:“请求的资源 (/MyFirstServlet/ServletHome) 不可用。”

Any ideas?

有任何想法吗?

Thanks!!

谢谢!!

--- Edit: Code ---

--- 编辑:代码 ---

index.jsp:

索引.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ServletHome" method="POST">
    First Name: <input type="text" name="firstName" size="20"><br>
    Last Name: <input type="text" name="lastName" size="20"> <br>
    <br> <input type="submit" value="Submit">
</form>
</body>
</html>

ServletHome.java:

ServletHome.java:

package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletHome extends HttpServlet {

private static final long serialVersionUID = 1L;
public ServletHome() {
    super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException 
{
}

/**
 * The function that gets the name and returns an HTML file with Hello to them
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
protected void doPost(HttpServletRequest request,
                                            HttpServletResponse response) 
throws ServletException, IOException {

    //set type of output
    response.setContentType("text/html;charset=UTF-8");

    //get writer
    PrintWriter out = response.getWriter();

    //get first name
    String firstName = request.getParameter("firstName").toString();

    //get last name
    String lastName = request.getParameter("lastName").toString();

    //write the file
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Test Title</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<p>Welcome, " + firstName + " " + lastName + "!</p>");
    out.println("</body>");
    out.println("</html>");

    out.close();
}
}

web.xml:

网页.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!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>
        <servlet>
            <servlet-name>ServletHome</servlet-name>
            <servlet-class>servlets.ServletHome</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ServletHome</servlet-name>
            <url-pattern>/servlets/*</url-pattern>
        </servlet-mapping>
</web-app> 

回答by davidbuzatto

The resource is no available because:

资源不可用,因为:

  1. Your action attribute is wrong, or;
  2. You didn't map your servlet corretly. To do this, you can:
  1. 您的操作属性错误,或者;
  2. 您没有正确映射您的 servlet。为此,您可以:

Use @WebServlet annotation, since you are using Tomcat 7:

使用 @WebServlet 注释,因为您使用的是 Tomcat 7:

@WebServlet( name = "ServletName", urlPatterns = { "/path/to/your/servlet/myName" } )
public class ServletName extends HttpServlet {
    // your code here
}

Ormap your servlet in web.xml:

或者在 web.xml 中映射您的 servlet:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
    <!-- more code here... -->
    <servlet>
        <servlet-name>ServletName</servlet-name>
        <servlet-class>yout.package.here.ServletName</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletName</servlet-name>
        <url-pattern>/path/to/your/servlet/myName</url-pattern>
    </servlet-mapping>
    <!-- more code here... -->
</web-app>

Another thing that you need to pay attention is that you MUST implement the doXXX methods that corresponds to the HTTP methods (get, post, etc.) that you want your servlet to serve.

您需要注意的另一件事是您必须实现对应于您希望 servlet 服务的 HTTP 方法(get、post 等)的 doXXX 方法。

To request this servlet through you form, you need to set the action attribute as:

要通过您的表单请求此 servlet,您需要将 action 属性设置为:

<form action="/path/to/your/servlet/myName">
    <!-- form fields here... -->
</form>

To finish, you can use the method attribute in your form to choose the HTTP method that your browser will use to request the Servlet. If you don't provide, the default method is get. As I already said, if the get method is used, you need to implement the doGet method in the servlet.

最后,您可以使用表单中的方法属性来选择浏览器将用于请求 Servlet 的 HTTP 方法。如果您不提供,则默认方法是get。前面已经说过,如果使用get方法,则需要在servlet中实现doGet方法。