1 Servlet的快速入门
1 web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEBTest13</display-name> <servlet> <servlet-name>myservlet</servlet-name> <servlet-class>p1.servlet.MyFirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myservlet</servlet-name> <url-pattern>/fist</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
2 servlet代码
package p1.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; /* * 1 创建类实现servlet接口 * 2 添加里面没有实现的方法,并重写service方法 * 3 配置web.xml文件。 */ public class MyFirstServlet implements Servlet{ /** * 重写service方法 */ @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { HttpServletResponse response = (HttpServletResponse) res; res.getWriter().write("this is my first servlet"); } @Override public void init(ServletConfig config) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }