<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.javajavac -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文件中犯了一个错误。有人可以帮我解决这个问题吗?