Servlet 小例子,请教
写一个Servlet 的helloworld程序
先建了一个MyServlet.java
package Lin;
import
java.io.IOException;
import java.io.PrintWriter;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void init() throws
ServletException {
super.init();
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException,
IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head></head><body>Hello World!</body></html>");
}
public void destroy() {
super.destroy();
}
}
然后写了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_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>MaServlet</servlet-name>
<servlet-class>Lin.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MaServlet</servlet-name>
<url-pattern>/Test1/MyServlet.java</url-pattern>
</servlet-mapping>
</web-app>
运行的时候地址是http://localhost:8070/Test1/MyServlet
但一直出现404错误
请教怎么回事???
------解决方案--------------------<servlet-mapping>
<servlet-name>MaServlet</servlet-name>
<url-pattern>/Test1/MyServlet.java</url-pattern>
</servlet-mapping>
url-pattern:这个参数是用来配访问路径的。
如果你想这样访问http://localhost:8070/Test1/MyServlet,那你url-pattern 就应该是/MyServlet了
------解决方案--------------------url-pattern:这个参数是用来配访问路径的。
如果你想这样访问http://localhost:8070/Test1/MyServlet,那你url-pattern 就应该是/MyServlet了