Eclipse工程目录如图:
input.html代码:
<!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=GBK">
<title>input</title>
</head>
<body>
<form action="InputServlet" method="post">
输入内容:<input type="text" name="info">
<input type="submit" value="提交">
</form>
</body>
</html>
InputServlet.java代码:
package org.xq.servletdemo;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InputServlet {
public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
String info = req.getParameter("info");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head><title>input</title></head>");
out.println("<body>");
out.println("<h1>"+info+"</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException{
this.doGet(req, resp);
}
}
web.xml配置文件配置:
<?xml version="1.0" encoding="GBK"?>
<web-app 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"
version="3.0"
metadata-complete="true">
<servlet>
<servlet-name>input</servlet-name>
<servlet-class>org.xq.servletdemo.InputServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>input</servlet-name>
<url-pattern>/ch09/InputServlet</url-pattern>
</servlet-mapping>
</web-app>
按上面运行之后 报404的错误,肯请各位高手指导!
------解决思路----------------------
public class InputServlet 这个类没有继承HttpServlet吧?
改为
public class InputServlet extends HttpServlet