servlet程序没有输出

时间:2013-09-24 02:57:27

标签: java eclipse servlets

我使用eclipse kepler开发了这个代码。我没有得到我的错误。我没有得到任何输出.Below是我的html页面,它存在于eclipse的webcontent文件夹中

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

下面是我的servlet文件,位于一个包

侧的src文件夹中

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.xml文件,它位于WEB-INF

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

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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>

答案 2 :(得分:1)

没有set 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方法。