servalet java中的tomcat错误
我是servlet的初学者。。。我想将一个简单的参数从html传递到servlet,但出现了一个错误“HTTP状态404-未找到”。。这是我的密码。。。 网状物xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>NewClass</servlet-name>
<servlet-class>NewClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewClass</servlet-name>
<url-pattern>/NewClass</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>newhtml.html</welcome-file>
</welcome-file-list>
</web-app>
newhtml。xml
<html>
<head>
<title>Simple Application</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="/NewClass" >
<input name="username" />
<input type="submit"/>
</form>
</body>
</html><html>
<head>
<title>Simple Application</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="/NewClass" >
<input name="username" />
<input type="submit"/>
</form>
</body>
</html><html>
<head>
<title>Simple Application</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="/NewClass" >
<input name="username" />
<input type="submit"/>
</form>
</body>
</html>
新班级。爪哇$
public class NewClass extends HttpServlet{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
String req = request.getParameter("username");
out.println(req);}}
# 1 楼答案
你的
<form action=""/>
问题 一定是当你把
/
放在动作标记中时,它指向根,而不是指向上下文路径