本文共 1853 字,大约阅读时间需要 6 分钟。
Servlet 接口 < —— GenericServlet 抽象类 < —— HttpServlet 抽象类
GenericServlet: 将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象 。
HttpServlet: 对http协议的一种封装,简化操作。
图示:
示例:
package com.xww.web.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet("/demo1")public class ServetHttpDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doget...."); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("dopost..."); }}
@WebServlet({"/x","/xw","/xww"})
http://localhost:8080/xww
http://localhost:8080/x/xww
http://localhost:8080/xww.do
package com.xww.web.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Servlet路径配置 *///@WebServlet({"/x","/xw","/xww"})//@WebServlet("/x/xw")//@WebServlet("/x/*")//@WebServlet("/*")@WebServlet("*.do")public class ServletUrlDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doget..."); }}
转载地址:http://skgwz.baihongyu.com/