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>
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>
访问的路径是这个吗 http://localhost/LianXi/MyTestServlet
你给的信息太少了
路径是http://localhost:8080/LianXi/servlet/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>
<url-pattern>/servlet/MyTestServlet</url-pattern>
</servlet-mapping>