0

我使用 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>
4

3 回答 3

1

如果没有设置 methed="",表单默认发送 post 请求。然后web.xml根据映射找到回复哪个servlet

<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>

然后它调用 doGet 方法或 doPost 方法(取决于您的请求方法)。

public class LifeCycleServlet extends HttpServlet{

 public void doPost(HttpServletRequest request,
                 HttpServletResponse response)
   throws ServletException, IOException {
   //Here call the service method.
 }
}

所以,在这个例子中,你最好重写 doPost 方法。

于 2013-09-24T03:37:04.127 回答
1
<?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>
<!-- modify LifeCycleServelt != LifeCycleServlet <servlet-class>one.LifeCycleServelt</servlet-class> -->
<servlet-class>one.LifeCycleServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LCServelt</servlet-name>
<url-pattern>/myFirstServlet</url-pattern>
</servlet-mapping>

</web-app>
于 2013-09-24T03:30:05.560 回答
1

您需要调用 PrintWriter 上的 flush() 方法来提交响应。

于 2013-09-24T03:16:55.490 回答