1. 创建一个JavaWEB项目
2. 新建一个Java类,让这个类继承javax.servlet.http.HttpServlet.
3. 在重写doGet和doPost方法
4. 在doGet或者doPost中编写业务代码
5. 在web.xml中注册Servlet
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
1 <servlet> 2 <servlet-name>servletDemo</servlet-name> 3 <servlet-class>com.qc.airquality.web.ServletDemo</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>servletDemo</servlet-name> 7 <url-pattern>/servletAction</url-pattern> 8 </servlet-mapping>
时间: 2024-11-13 09:30:08