0x01: 什么是Servlet?

  • 是sun公司开发动态web的技术
  • 实现了servlet接口的Java程序

0x02: Servlet的实现类有哪些?

Servlet接口默认有两个实现类

  • HttpServlet
  • GenericServlet

    HttpServlet 继承自 GenericServlet, 一般我们自己写类只需要继承HttpServlet,重写方法就可以了
public class HelloServlet extends HttpServlet {

    //由于get或者post只是请求实现的不同的方式,可以相互调用,业务逻辑都一样;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter(); //响应流
writer.print("Hello,Serlvet");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);

0x03:Servlet的原理

浏览器发送http请求给web容器,web容器产生Request和Response对象,这两个对象调用servlet接口的Service方法,Service方法再将返回的Responce信息返回给Responce对象,web容器从Responce对象读取将其响应给客户端。(注意,如果web容器是首次被访问,需要先把我们的java类变成class字节码文件)

什么是ServletContext

Web容器在启动时,为每个web程序创建一个对应的ServletContext,它代表当前web应用

ServletContext的一些用法和特性

在同一个web应用中,不同的servlet可以共享数据

public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //this.getInitParameter() 初始化参数
//this.getServletConfig() Servlet配置
//this.getServletContext() Servlet上下文
ServletContext context = this.getServletContext(); String username = "黎星澄"; //数据
context.setAttribute("username",username); //将一个数据保存在了ServletContext中,名字为:username 。值 username } }
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username"); resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字"+username); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

获取初始化参数

    <!--在web.xml配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param> protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url"); //得到name为url的参数
resp.getWriter().print(url);
}

请求转发

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo04");
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/xxx"); //转发的请求路径
//requestDispatcher.forward(req,resp); //调用forward实现请求转发;
context.getRequestDispatcher("/gp").forward(req,resp);
}
//请求转发和重定向的区别?
请求转发:像当于在服务器内部将请求转给请求的资源,然后由服务器相应给客户端
重定向:服务器将请求资源作为响应信息响应给客户端,客户端根据这个信息对服务器发起请求

读取资源文件

在java目录下新建properties

在resources目录下新建properties

都被打包到同一路径:classes

username=admin
password=123456
public class Servlet_Properties extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/hu/servlet/test.properties"); //输入流 Properties prop = new Properties(); //创建一个Properties对象
prop.load(is); //将读取到的加载到prop里
String user = prop.getProperty("username");
String pwd = prop.getProperty("password"); resp.getWriter().print(user+":"+pwd); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

最新文章

  1. 嵌入式 python异常except语句用法与引发异常 zz
  2. [转载]Macaca 测试 Android 应用:UIAutomator
  3. UIButton的常见设置
  4. spring二级缓存的ehcache 的 配置文件
  5. SQL Server系统表sysobjects介绍与使用(转)
  6. Layui框架+PHP打造个人简易版网盘系统
  7. ZOJ 2859 二维RMQ(模板)
  8. B. Lynyrd Skynyrd
  9. 使用idea搭建maven项目,结果spring-mybatis.xml文件报红“Cannot resolve file &#39;jdbc.properties&#39; less... (Ctrl+F1) Inspection info:Spring XML model validation”
  10. 命令行批量修改IP并ping测试
  11. springBoot_freemark配置
  12. linux学习第三天 (Linux就该这么学)
  13. 7 切片slice
  14. 1星|《社群X平台》:没有实际工作经验的职业写手拼凑而成
  15. ASP.NET Web API使用示例
  16. Java IO流-随机访问流
  17. 登入爱丽网后台(非JS绕过、非盲打、非IP欺骗)
  18. Java并发编程:Lock(转)
  19. 【HDU5857】Median
  20. constructor()方法

热门文章

  1. VMware搭建内网渗透环境
  2. 在GCP上创建Cloud SQL的三种方式(Console,gcloud,Terraform)
  3. flutter flutter_screenutil Looking up a deactivated widget&#39;s ancestor is unsafe.
  4. 都用过@Autowired,但你知道它是怎么实现的吗
  5. day15-声明式事务
  6. FAQ Selenium中提示can not connect to the service chromedriver 的解决方法
  7. 宠物小精灵之收服(等级考试4级 2021-03 T1)
  8. C-04\IDE基础知识和分支,循环语句
  9. 2021级《JAVA语言程序设计》上机考试试题
  10. ASP.NET Core - 依赖注入(一)