Servlet:HTTP状态404 - 请求的资源不可用

时间:2015-03-25 07:51:16

标签: java html jsp servlets

我是Java Programming& S的新手。与Servlets一起度过艰难时期JSP,针对它抛出的广泛挑战。目前,由于此错误,我无法访问Servlet页面:

 HTTP Status 404 - The requested resource is not available

对于许多人来说,这似乎是一个天真的问题,但是在尝试了从Stack Overflow到求助于其他学习资料的所有提示和技巧之后,我无法找出问题的确切原因。

Servlet文件:

package coreservlets;

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

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

@WebServlet("/GoodCodeServlet")
public class GoodCodeServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

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

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String title = "Code Sample";
    String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +      
                      "Transitional//EN\">\n"; 

    out.println(docType + "<html> \n" + 
                "<head> <title>" +title+ "</title></head>"  +
                "<body bgcolor=\"#eee\">" + 
                "<h1 align=\"center\">" +title+ "</h1>" +

                // Text inside a <pre> tag is displayed in a fixed-width font, 
                //and it preserves both spaces and line breaks....

                "<pre> \n" + getCode(request)+ "</pre>" +               
                "</body> </html>"  
             );

}

  protected String getCode(HttpServletRequest request) 
    {
       return (request.getParameter("code"));
    }

HTML文件:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body bgcolor="#FDEFD6">
    <center> <h1>Submit Code Samples</h1>

    <form action="/coreservlets.GoodCodeServlet" > 
    Code: <br><br> 
    <textarea rows="12" cols="40" name="code"></textarea> <br><br>
    <input type="submit" value="submit" /> 

    </form>
    </center>
    </body>
    </html>

web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

   <servlet>
   <servlet-name>CodeSample</servlet-name>
   <servlet-class>coreservlets.GoodCodeServlet</servlet-class>
   </servlet>

   <servlet-mapping>
   <servlet-name>CodeSample</servlet-name>
   <url-pattern>/coreservlets.GoodCodeServlet</url-pattern>
   </servlet-mapping>

   <servlet>
   <servlet-name>ShowParameters</servlet-name>
   <servlet-class>/coreservlets.ShowParameters</servlet-class>
   </servlet>

   <servlet-mapping>
   <servlet-name>ShowParameters</servlet-name>
   <url-pattern>/coreservlets.ShowParameters</url-pattern>
   </servlet-mapping>

   </web-app>

1 个答案:

答案 0 :(得分:0)