我在根 /Reverse 目录中有这个 reverse.html
<!DOCTYPE html>
<html>
<body>
<h1>Assignment Servlets-2 - Reverse</h1>
<form method='post' action='ReverseServlet'>
Text: <input type='text' name='text' size='50'><br><br>
<input type='submit' value='submit'>
</form>
</body>
</html>
这是/WEB-INF目录下的web.xml
<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_2_5.xsd"
version="2.5">
<servlet> <servlet-name>ReverseServlet</servlet-name>
<servlet-class>ReverseServlet</servlet-class> </servlet>
<servlet-mapping> <servlet-name>ReverseServlet</servlet-name>
<url-pattern>/reverse</url-pattern>
</servlet-mapping>
</web-app>
这是 ReverseServlet.java 源,它被编译成 /classes 目录中的类文件
import java.io.* ;
import javax.servlet.http.* ;
public class ReverseServlet extends HttpServlet {
public void doGet( HttpServletRequest request, HttpServletResponse response ) {
String text = request.getParameter("text");
response.setContentType("text/html");
try {
PrintWriter out = response.getWriter();
out.println( new StringBuffer( text ).reverse().toString() );
out.close();
} catch( Exception e ) {
System.out.println( "cannot get writer: " + e );
}
}
}
它们都在正确的目录中,我可以加载 reverse.html,但该操作不会与 ReverseServlet 类相关联。我正在使用 Tomcat 7.0,并且我已经得到了一个简单的 servlet 来工作,所以我知道它安装正确。它只是不会在点击提交时进入 servlet 代码......