1、概述
2、Servlet API
3、Servlet生命周期
4、部署运行Servlet
样例:
package com.ljb.servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet3 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("处理请求,doGet方法被调用。");
}
@Override
public void destroy() {
System.out.println("servlet被销毁,destroy方法被调用。");
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("servlet初始化,init方法被调用。");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("处理请求,doPost方法被调用。");
}
// @Override
// protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
// throws ServletException, IOException {
// // TODO Auto-generated method stub
// super.service(arg0, arg1);
// }
}
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletDemo</display-name>
<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>
<servlet>
<servlet-name>myServlet3</servlet-name>
<servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet3</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
停止服务:
servlet被销毁,destroy方法被调用。
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletDemo</display-name>
<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>
<servlet>
<servlet-name>myServlet3</servlet-name>
<servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet3</servlet-name>
<url-pattern>/demo/*</url-pattern>
</servlet-mapping>
</web-app>
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletDemo</display-name>
<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>
<servlet>
<servlet-name>myServlet3</servlet-name>
<servlet-class>com.ljb.servlet.MyServlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet3</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
5、完成信息添加功能