您好我在提交以下jsp时收到了404 HTTP状态。
HTTP状态404 - / TestServlet1 你能帮我解决这个错误吗? 注意:定义了人和狗类 的的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
<form id="a" action="/TestServlet1">
<input type="submit">
</form>
<%
%>
Name = '${person.name}'
Dog = '${person.dog.name}'
</body>
</html>
TestServlet1
package test;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 TestServlet
*/
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public TestServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
Dog g = new Dog();
g.setName("dogeee");
Person p = new Person();
p.setDog(g);
p.setName("xxx");
request.setAttribute("person", p);
RequestDispatcher dispatch = request.getRequestDispatcher("/index.jsp");
dispatch.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
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>GlobalWeather</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet1</url-pattern>
</servlet-mapping>
</web-app>
答案 0 :(得分:0)
404表示找不到该网址。我怀疑您需要URL中的Web应用程序名称。
所以不要使用以下形式的动作:
/TestServlet1
,
尝试
/name_of_your_web_app/TestServlet1
答案 1 :(得分:0)
只需删除表单操作中的/:
<form id="a" action="/TestServlet1">
将其更改为
<form id="a" action="TestServlet1">
在HTML中,添加/表示相对URL而不带斜杠表示绝对URL。或者更好地使用这里提到的上下文:
<form id="a" action=${pageContext.request.contextPath}/TestServlet1>
答案 2 :(得分:0)
你必须把你的contextroot +你的servlet名称... contextRoot通常是项目的名称。
action="/nameProject/TestServlet1"
我希望这有助于你
答案 3 :(得分:0)
第一和第三个答案是对的。写动作=&#34; TestServlet1&#34;或action =&#34; / Projectname / TestServlet1&#34;。如果你把/ testServet1放在行动中,这意味着你正在指定所需文件的路径,在这种情况下是错误的,而如果你使用testservlet1,它意味着你正在项目中搜索文件名testservlet1来运行。