Servlet不在控制台上打印消息

时间:2016-11-08 15:33:38

标签: java servlets

抱歉我犯过的愚蠢错误。我正在关注来自" java for web的示例,其中包含servlets,jsp和ejb"书 所以我有以下问题。我有一个servlet。 servlet的功能非常简单 - 只需检索请求参数并在控制台上打印它们。从index.html文件中调用servlet。 index.html文件的代码是

<html>
<head>
    <title> sending a request</title>
</head>
<body>
    <form action=RequestDemoServlet method="POST">
        <br> <br>
    Author: <input type="TEXT" name="author">
    <input type="SUBMIT" name="submit">
    <input type="RESET" value="reset">
    </form>
</body>
</html>

servlet的代码如下:

 import javax.servlet.*;
 import java.util.Enumeration;
 import java.io.IOException;

 public class RequestDemoServlet implements Servlet
 {
     public void init(ServletConfig config) 
        throws ServletException
     {}
     public void destroy()
     {}
     public void service(ServletRequest request,
            ServletResponse response)
            throws ServletException, IOException
     {
         System.out.println("Server port: "+request.getServerPort());
         System.out.println("Server name: "+request.getServerName());
         System.out.println("Protocol: "+request.getProtocol());
         System.out.println("Char encoding: "+
                request.getCharacterEncoding());
         System.out.println("Constent length: "+
                request.getContentLength());
         System.out.println("Remote address: "+
                request.getRemoteAddr());
         System.out.println("remote Host: "+request.getRemoteHost());
         Enumeration parameters=request.getParameterNames();
         while(parameters.hasMoreElements())
         {
             String parameterName=(String)parameters.nextElement();
             System.out.println("Parameter name: "+parameterName);
             System.out.println("Parameter value: "+
                    request.getParameter(parameterName));
         }
    }
     public String getServletInfo()
     {
         return null;
     }
     public ServletConfig getServletConfig()
     {
         return null;
     }
}

我用

中的javac编译了RequestDemoSevlet.java
javac -cp $CATALINA_HOME/lib/servlet-api.jar -d ./classes RequestDemoServelt.java

servlet的.class文件放在我的应用程序的classes目录中。 然后我编辑了web.xml文件

<?xml version ="1.0" encoding ="ISO-8859-1"?>
<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">
    <display-name>Chapter 2</display-name>
    <description> Apress</description>
<servlet>
    <servlet-name>RequestDemo</servlet-name>
    <servlet-class>RequestDemoServlet</servlet-class>
</servlet>
</web-app>

我想我在web.xml或index.html文件中犯了一个错误。有人可以帮我解决这个问题吗?

0 个答案:

没有答案