当前位置: 代码迷 >> J2SE >> tomcat 找不到 servlet,该怎么解决
  详细解决方案

tomcat 找不到 servlet,该怎么解决

热度:573   发布时间:2016-04-24 16:08:15.0
tomcat 找不到 servlet
import   javax.servlet.*;
import   javax.servlet.http.*;
import   java.io.*;
import   java.util.*;

public   class   MyTestServlet   extends   HttpServlet{
private   static   final   String   CONTENT_TYPE= "text/html;charset=GB2312 ";
public   void   init(ServletConfig   config)   throws   ServletException{
super.init(config);
}
public   void   doGet(HttpServletRequest   request,HttpServletResponse   response)
throws   ServletException,IOException{
response.setContentType(CONTENT_TYPE);
PrintWriter   out   =   response.getWriter();
out.print( " <html> ");
out.println( " <head> <title> MyTestServlet   小应用程序   </title> </head> ");
out.print( " <body> ");
out.print( " <p> MyTestServlet   小应用程序   </p> ");
out.print( " </body> </html> ");
}
public   void   destroy(){
}


}

web.xml

<?xml   version= "1.0 "   encoding= "ISO-8859-1 "?>


<web-app   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 "
        version= "2.4 ">

    <display-name> Welcome   to   Tomcat </display-name>
    <description>
          Welcome   to   Tomcat
    </description>


<!--   JSPC   servlet   mappings   start   -->

        <servlet>
                <servlet-name> MyTestServlet </servlet-name>
                <servlet-class> LianXi.MyTestServlet </servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name> MyTestServlet </servlet-name>
                <url-pattern> /LianXi/MyTestServlet </url-pattern>
        </servlet-mapping>

<!--   JSPC   servlet   mappings   end   -->

</web-app>


------解决方案--------------------
LianXi.MyTestServlet 确认包名是否正确?
访问的路径是这个吗 http://localhost/LianXi/MyTestServlet
你给的信息太少了
------解决方案--------------------
错误信息贴出来啊!
------解决方案--------------------
首先,如果你的项目根context是LianXi的话(默认都是web项目的名称),那么你想请求这个servelt应该在输入:http://localhost:8080/LianXi/LianXi/MyTestServlet。
说白了就是上下文路径没有找对。

如果你是在%TOMCAT_HOME%\webapps\LianXi目录下的某个jsp文件中访问这个servlet的话(比如是index.jsp)就应该这样写:
<form action= "servlet/MyTestServlet " method= "get ">
<input type= "submit " value= "submit ">
</form>

而你的web.xml中的servlet映射就应该这样配置:
<servlet-mapping>
<servlet-name> MyTestServlet </servlet-name>
  相关解决方案