2017-08-25 106 views
0

单击窗体上的提交按钮时出现404错误。获取错误(请求的资源不可用。)URL映射http://localhost:8080/HelloWorld/HelloServlet。我也试过这个参考,Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"但这也似乎不起作用。在提交表单时获取请求时发生Servlet 404错误

1,索引,HTML

<!DOCTYPE html> 
    <html> 
    <head> 
    <meta charset="ISO-8859-1"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
     <form action="HelloServlet"> 
     <input type="submit" value="HIT"> 
     </form> 
    </body> 
    </html> 

2. HelloServlet

 package com.example.aman; 

     import java.io.IOException; 
     import javax.servlet.ServletConfig; 
     import javax.servlet.ServletException; 
     import javax.servlet.annotation.WebServlet; 
     import javax.servlet.http.HttpServlet; 
     import javax.servlet.http.HttpServletRequest; 
     import javax.servlet.http.HttpServletResponse; 

     /** 
     * Servlet implementation class HelloServlet 
     */ 
     @WebServlet("/HelloServlet") 
     public class HelloServlet extends HttpServlet { 
     private static final long serialVersionUID = 1L; 

     /** 
     * Default constructor. 
     */ 
     public HelloServlet() { 
     // TODO Auto-generated constructor stub 
     } 

     /** 
     * @see Servlet#init(ServletConfig) 
     */ 
     public void init(ServletConfig config) throws ServletException { 
     // TODO Auto-generated method stub 
     System.out.println("DIVESH"); 
     } 

     /** 
     * @see HttpServlet#doGet(HttpServletRequest request, 
     HttpServletResponse response) 
     */ 
     protected void doGet(HttpServletRequest request, HttpServletResponse 
     response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     response.getWriter().append("Served at: 
     ").append(request.getContextPath()); 
     response.getWriter().println("DIVESH"); 
     } 

     /** 
     * @see HttpServlet#doPost(HttpServletRequest request, 
     HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     doGet(request, response); 
    } 

} 

3. 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" id="WebApp_ID" version="3.0"> 
    <display-name>HelloWorld</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

回答

0

http://localhost:8080/HelloWorld/HelloServlet

直接告诉我问题是什么。您正尝试从同一文件夹目录中访问一个servlet。这是如果你不小心你的relative URLS

在您的形式发生了什么,改变URL映射到这一点:

<form action="/HelloServlet"> 
     <input type="submit" value="HIT"> 
     </form> 

或者,如果没有工作,做到这一点:

<form action="../HelloServlet"> 
     <input type="submit" value="HIT"> 
     </form> 

向路径添加/使其成为absolute

添加../使其转到当前目录的父级。

希望有所帮助。

相关问题