• HttpServlet概念与ServletContext对象


    HttpServlet概念

    创建类的时候继承HttpServle

    实现步骤:

    1)创建类继承HttpServlet类

    2)覆盖doGet和doPost

    3)web.xml中进行servlet的配置

    HttpServlet继承了GenericServlet,而GenericServlet实现Servlet接口, 所以我们可以继承HttpServlet来相当于创建一个Servlet

    HttpServlet重写service()方法:
                1.在该方法中先将ServletRequest和ServletResponse
                    强转为了HttpServletRequest和HttpServletResponse。
     2.然调用重载的service()方法,并将刚刚强转得到对象传递到重载的方法中。
         如果是GET请求,则调用doGet(HttpServletRequest request , HttpServletResponse response)
                    如果是POST请求,则调用doPost(HttpServletRequest request , HttpServletResponse response)
    当通过继承HttpServlet来创建一个Servlet时,只需要根据要处理的请求的类型,来重写不同的方法。
                    处理get请求,则重写doGet()
                    处理post请求,则重写doPost()
     ServletContext对象:
    什么是 ServletContext?

    ServletContext代表是一个web应用的环境(上下文)对象,

    ServletContext对象一个web应用只有一个,但是一个web应用可以又多个Servlet对象!!!

    ServletContext对象的生命周期?

    web应用被卸载,服务器关闭

    获得 ServletContext 如何获得?

    ServletContext Context = getServletContext();

    String getServletName() – 获取当前Servlet在web.xml中配置的名字 
    String getInitParameter(String name) –获取当前Servlet指定名称的初始化参数的值 

    根据相对路径获取绝对路径

    package com.oracle.demo01;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Servlet01 extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//获取servletContext对象
    		ServletContext context=getServletContext();
    		//获取相对于服务器的相对路径获取绝对路径
    		String patha=context.getRealPath("WEB-INF/classes/a.txt");
    		String pathb=context.getRealPath("b.txt");
    		String pathc=context.getRealPath("WEB-INF/c.txt");
    		System.out.println(patha);
    		System.out.println(pathb);
    		System.out.println(pathc);
    		
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

     

    ServletContext是一个域对象,用来存储数据区域就是域对象

    域对象的通用的方法:

    setAtrribute(String name,Object obj);(往里存键值对 键只能是String)

    getAttribute(String name);(根据建取值,取到的值是object类型,需要向下转型)

    removeAttribute(String name);(移出)

    package com.oracle.demo01;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Servlet02 extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//获取ServletContext对象
    		ServletContext context=getServletContext();
    		//往ServletContext域中设置值
    		context.setAttribute("name","xiahouyuan");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

      

    package com.oracle.demo01;
    
    import java.io.IOException;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class Servlet03 extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		//获取ServletContext对象
    		ServletContext context=getServletContext();
    		//获取ServletContext域里面的值
    		String name=(String)context.getAttribute("name");
    		response.getWriter().write(name);
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    

      

  • 相关阅读:
    Gogs http和ssh地址显示localhost的问题
    SQL Server从读写频繁的大表中删除大批量数据
    Java Spring Boot: Unable to determine jdbc url from datasource
    NPOI导入Excel日期格式的处理
    手把手教你整合最优雅SSM框架
    JDBC链接数据库MySQL 8.0 Public Key Retrieval is not allowed 错误的解决方法
    Vim 入门教程
    jquery.i18n.properties前端国际化方案
    生产者消费者模式中条件判断是使用while而不是if
    ThreadPoolExecutor 中的 shutdown() 、 shutdownNow() 、 awaitTermination() 的用法和区别
  • 原文地址:https://www.cnblogs.com/awdsjk/p/11115384.html
Copyright © 2020-2023  润新知