• servlet_3


    ServletContext
    	介绍
    	提供的功能
    	servlet中获取servletcontext实例
    	servletcontext接口的方法
    	
    package com.fgy;
    
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Enumeration;
    import java.util.Set;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(name="servlet",urlPatterns="/servlet",loadOnStartup=1)
    public class InitParaServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        @Override
        public void init(ServletConfig config) throws ServletException {
            ServletContext context=config.getServletContext();
            System.out.println(context.getServerInfo());
            System.out.println(context.getMajorVersion());
            System.out.println(context.getMinorVersion());
            System.out.println(context.getServletContextName());
            System.out.println(context.getClassLoader());
            System.out.println(context.getVirtualServerName());
            
            Enumeration<String> names=context.getInitParameterNames();
            while (names.hasMoreElements()) {
                String name = names.nextElement();
                System.out.println(name+":"+context.getInitParameter(name));
                
            }
            try {
                URL url=context.getResource("/index.html");
                System.out.println(url);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            
            Set<String> paths=context.getResourcePaths("/");
            for (String path : paths) {
                System.out.println(path);
            }
            System.out.println(context.getRealPath("/"));
            
            
            
        }
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        ServletContext context1=getServletContext();
    //        ServletContext context2=request.getServletContext();
    
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    请求分派-1
    
        请求分派概述
        RequestDispatcher接口
            获取实例方法
            实例提供的方法
            
        getServletContext().getRequestDispatcher("/index.html");
        request.getRequestDispatcher("index.html");    
        上面这二者的区别,一个是相对于应用根目录,一个是相对于映射的servlet目录
        
        forward与include方法
        
    package com.fgy;
    
    import java.io.IOException;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/servlet6/test")
    public class Servlet6 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        RequestDispatcher rd=getServletContext().getRequestDispatcher("/index.html");
    //        System.out.println(rd);
            RequestDispatcher rd=request.getRequestDispatcher("index.html");
            rd.forward(request, response);
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }
    
    请求分派-2
        路径中查询字符串中的参数优先级
        前台重定向与后台转发的区别
            
            
        httpservletresponse.sendredirect
        浏览器地址栏会发生变化,发两次请求
        requestdispatcher.forward
        浏览器地址栏不发生变化,发一次请求
    
        数据共享
  • 相关阅读:
    git config 命令各参数有何区别
    Git 初始化
    Linux 安装 Git
    在Mac OS 上安装 Git
    maven 部署到远程仓库(私服)
    Git与SVN的区别
    windows挂载NFS文件系统无法访问/修改文件解决
    mysql5.7写入数据时间相差13/14小时解决
    mysql配置文件不生效解决
    springdata jpa mysql5.7写入中文乱码解决
  • 原文地址:https://www.cnblogs.com/createyuan/p/9089837.html
Copyright © 2020-2023  润新知