我使用 eclipse kepler 开发了这段代码。我没有得到我的错误在哪里。我没有得到任何输出。下面是我的 html 页面,它位于 eclipse 的 webcontent 文件夹中
主页.html
<!DOCTYPE html>
<html>
<head>
<title>home html</title>
</head>
<body>
<form action="myFirstServlet">
<input type="submit" value="invoke life cycle servlet" />
</form>
</body>
</html>
下面是我在一个包一侧的 src 文件夹中的 servlet 文件
LifeCycleServlet.java
package one;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
public class LifeCycleServlet implements Servlet {
private ServletConfig config;
public void init(ServletConfig sc) {
sc=config;
System.out.println("in init");
}
public void destroy() {
System.out.println("in destroy");
}
public ServletConfig getServletConfig() {
return config;
}
public String getServletInfo() {
return "servelet life cycle info";
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
PrintWriter printWriter=response.getWriter();
printWriter.print("life cycle service");
System.out.println("service");
}
}
下面是我在 WEB-INF 中的 web.xml 文件
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">
<servlet>
<servlet-name>LCServelt</servlet-name>
<servlet-class>one.LifeCycleServelt</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LCServelt</servlet-name>
<url-pattern>/myFirstServlet</url-pattern>
</servlet-mapping>
</web-app>