1. ServletContext
  2. 介绍
  3. 提供的功能
  4. servlet中获取servletcontext实例
  5. servletcontext接口的方法
  1. package com.fgy;
  2.  
  3. import java.io.IOException;
  4. import java.net.MalformedURLException;
  5. import java.net.URL;
  6. import java.util.Enumeration;
  7. import java.util.Set;
  8.  
  9. import javax.servlet.ServletConfig;
  10. import javax.servlet.ServletContext;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. @WebServlet(name="servlet",urlPatterns="/servlet",loadOnStartup=1)
  18. public class InitParaServlet extends HttpServlet {
  19. private static final long serialVersionUID = 1L;
  20.  
  21. @Override
  22. public void init(ServletConfig config) throws ServletException {
  23. ServletContext context=config.getServletContext();
  24. System.out.println(context.getServerInfo());
  25. System.out.println(context.getMajorVersion());
  26. System.out.println(context.getMinorVersion());
  27. System.out.println(context.getServletContextName());
  28. System.out.println(context.getClassLoader());
  29. System.out.println(context.getVirtualServerName());
  30.  
  31. Enumeration<String> names=context.getInitParameterNames();
  32. while (names.hasMoreElements()) {
  33. String name = names.nextElement();
  34. System.out.println(name+":"+context.getInitParameter(name));
  35.  
  36. }
  37. try {
  38. URL url=context.getResource("/index.html");
  39. System.out.println(url);
  40. } catch (MalformedURLException e) {
  41. e.printStackTrace();
  42. }
  43.  
  44. Set<String> paths=context.getResourcePaths("/");
  45. for (String path : paths) {
  46. System.out.println(path);
  47. }
  48. System.out.println(context.getRealPath("/"));
  49.  
  50. }
  51. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  52. // ServletContext context1=getServletContext();
  53. // ServletContext context2=request.getServletContext();
  54.  
  55. }
  56.  
  57. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  58. doGet(request, response);
  59. }
  60.  
  61. }
  1. 请求分派-1
  2.  
  3. 请求分派概述
  4. RequestDispatcher接口
  5. 获取实例方法
  6. 实例提供的方法
  7.  
  8. getServletContext().getRequestDispatcher("/index.html");
  9. request.getRequestDispatcher("index.html");
  10. 上面这二者的区别,一个是相对于应用根目录,一个是相对于映射的servlet目录
  11.  
  12. forwardinclude方法
  13.  
  14. package com.fgy;
  15.  
  16. import java.io.IOException;
  17.  
  18. import javax.servlet.RequestDispatcher;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.annotation.WebServlet;
  21. import javax.servlet.http.HttpServlet;
  22. import javax.servlet.http.HttpServletRequest;
  23. import javax.servlet.http.HttpServletResponse;
  24.  
  25. @WebServlet("/servlet6/test")
  26. public class Servlet6 extends HttpServlet {
  27. private static final long serialVersionUID = 1L;
  28.  
  29. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. // RequestDispatcher rd=getServletContext().getRequestDispatcher("/index.html");
  31. // System.out.println(rd);
  32. RequestDispatcher rd=request.getRequestDispatcher("index.html");
  33. rd.forward(request, response);
  34. }
  35.  
  36. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37. doGet(request, response);
  38. }
  39.  
  40. }
  41.  
  42. 请求分派-2
  43. 路径中查询字符串中的参数优先级
  44. 前台重定向与后台转发的区别
  45.  
  46. httpservletresponse.sendredirect
  47. 浏览器地址栏会发生变化,发两次请求
  48. requestdispatcher.forward
  49. 浏览器地址栏不发生变化,发一次请求
  50.  
  51. 数据共享

servlet_3的更多相关文章

  1. AndroidStudio3.0无法打开Android Device Monitor的解决办法(An error has occurred on Android Device Monitor)

    ---恢复内容开始--- 打开monitor时出现 An error has occurred. See the log file... ------------------------------- ...

  2. SpringBoot拦截器中Bean无法注入(转)

    问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...

  3. JaveWeb学习之Servlet(一):Servlet生命周期和加载机制

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-22/19.html 作者:夜月归途 出处:http://www.guitu ...

  4. JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用

    JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...

  5. Eclipse Configuration

    *** Date: 2013年9月12日星期四中国标准时间上午8时41分50秒 *** Platform Details: *** System properties:applicationXMI=o ...

  6. Win7 JavaEE 安装

    新建四个目录 D:\ApacheServer\eclipse 存放eclipse D:\ApacheServer\jdk jdk安装目录 D:\ApacheServer\apache-tomcat 存 ...

随机推荐

  1. L1-060 心理阴影面积

    这是一幅心理阴影面积图.我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线).由红.蓝线围出的面积,就是我们在做作业时的心理阴影面积. 现给出 ...

  2. HTML中data-* 属性

    使用 data-* 属性来嵌入自定义数据: <ul><li data-animal-type="bird">Owl</li><li dat ...

  3. Async CallBack promise fetch

    Callback (回调函数) 在计算机程序设计中,回调函数,或简称回调(Callback 即call then back 被主函数调用运算后会返回主函数),是指通过函数参数传递到其它代码的,某一块可 ...

  4. spring-data-mongodb与mongo shell的对应关系

    除了特殊注释外,本文的测试结果均基于 spring-data-mongodb:1.10.6.RELEASE(spring-boot-starter:1.5.6.RELEASE),MongoDB 3.0 ...

  5. Mysql和Hadoop+Hive有什么关系?

    1.Hive不存储数据,Hive需要分析计算的数据,以及计算结果后的数据实际存储在分布式系统上,如HDFS上. 2.Hive某种程度来说也不进行数据计算,只是个解释器,只是将用户需要对数据处理的逻辑, ...

  6. 为什么使用Nosql:Nosql和SQL的区别

    1.概念: SQL(Structured Query Language)数据库,指关系型数据库.主要代表:SQL Server.Oracle.MySQL.PostgreSQL. NoSQL(Not O ...

  7. java发送soapui格式的报文

    import java.io.*;import java.net.HttpURLConnection;import java.net.URL; 使用java对soapui报文进行发送 public c ...

  8. docker查看挂载目录Volume

    使用docker inspect命令查看container的volume信息,按照书本上面敲,发现一直报错: 使用命令如下: sudo docker inspect --format "{{ ...

  9. Arm11-mini6410入坑

    Mini6410 学习Stm32的时候原子哥的资料真全啊,而且原子哥在他论坛也解答问题.然而现在学习ARM买了一套友善之臂的开发板,官方的资料只能刚好入门而已,其实入门都算不上.看了一下,网上的资料很 ...

  10. Delphi编程之爬取贴吧图片最终版

    接着前面两篇文章的内容,我们今天把这个贴吧爬取图片的程序完善,让它具有可以下载贴吧多页和帖子多页图片的能力. 主界面设计如下,包含3个labelededit,3个button,1个memo,1个str ...