日期:2014-05-18  浏览次数:20579 次

servlet错误,请帮忙
html文件:
<html>
<title>
<head> me </head>
</title>
<body>
<form   action= "http://localhost:8080/test/login "   method= "post ">
<input   type= "text "   name= "name ">
<input   type= "password "   name= "pass ">
<input   type= "submit ">
</body>
</html>
servlet文件:
import   java.io.*;
import   java.sql.*;
import   javax.servlet.*;
import   javax.servlet.http.*;
public   class   LoginServlet   extends   HttpServlet
{
public   void   doPost(HttpServletRequest   req,HttpServletResponse   resp)throws   ServletException,IOException
{
PrintWriter   out=resp.getWriter();
String   n=req.getParameter( "name ");
String   p=req.getParameter( "pass ");
out.print(n);
out.print(p);
}
}
web.xml配置文件:
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<web-app   version= "2.4 "  
xmlns= "http://java.sun.com/xml/ns/j2ee "  
xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance "  
xsi:schemaLocation= "http://java.sun.com/xml/ns/j2ee  
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">
        <servlet>
                <servlet-name> login </servlet-name>
                <class-name> LoginServlet </class-name>
        </servlet>
        <servlet-mapping>
                <servlet-name> login </servlet-name>
                <url-pattern> /login/* </url-pattern>
        </servlet-mapping>
</web-app>

错误:
Servlet   login   is   not   available
The   requested   resource   (Servlet   login   is   not   available)   is   not   available.

以上简单的servlet试验不知道错误发生在哪里,请求帮助。

------解决方案--------------------
1.把servlet.jar包加入classpath中;
2.jsp页面中的action= "http://localhost:8080/test/login "应该与web.xml中 <servlet-name> login </servlet-name> 相对应,即改成action= "login "
3.web.xml中 <url-pattern> /login/* </url-pattern> ,你为什么要加个*号,这个路径的定义指明的是你在浏览器中访问时的地址,所以定义简单一些要好。