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
- 浏览器地址栏不发生变化,发一次请求
- 数据共享
servlet_3的更多相关文章
- AndroidStudio3.0无法打开Android Device Monitor的解决办法(An error has occurred on Android Device Monitor)
---恢复内容开始--- 打开monitor时出现 An error has occurred. See the log file... ------------------------------- ...
- SpringBoot拦截器中Bean无法注入(转)
问题 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于其他bean在service,controller层注入一点问题也没有,开始根本没意识到Be ...
- JaveWeb学习之Servlet(一):Servlet生命周期和加载机制
原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2018-07-22/19.html 作者:夜月归途 出处:http://www.guitu ...
- JavaWeb中监听器+过滤器+拦截器区别、配置和实际应用
JavaWeb中监听器+过滤器+拦截器区别.配置和实际应用 1.前沿上一篇文章提到在web.xml中各个元素的执行顺序是这样的,context-param-->listener-->fil ...
- Eclipse Configuration
*** Date: 2013年9月12日星期四中国标准时间上午8时41分50秒 *** Platform Details: *** System properties:applicationXMI=o ...
- Win7 JavaEE 安装
新建四个目录 D:\ApacheServer\eclipse 存放eclipse D:\ApacheServer\jdk jdk安装目录 D:\ApacheServer\apache-tomcat 存 ...
随机推荐
- L1-060 心理阴影面积
这是一幅心理阴影面积图.我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线).由红.蓝线围出的面积,就是我们在做作业时的心理阴影面积. 现给出 ...
- HTML中data-* 属性
使用 data-* 属性来嵌入自定义数据: <ul><li data-animal-type="bird">Owl</li><li dat ...
- Async CallBack promise fetch
Callback (回调函数) 在计算机程序设计中,回调函数,或简称回调(Callback 即call then back 被主函数调用运算后会返回主函数),是指通过函数参数传递到其它代码的,某一块可 ...
- spring-data-mongodb与mongo shell的对应关系
除了特殊注释外,本文的测试结果均基于 spring-data-mongodb:1.10.6.RELEASE(spring-boot-starter:1.5.6.RELEASE),MongoDB 3.0 ...
- Mysql和Hadoop+Hive有什么关系?
1.Hive不存储数据,Hive需要分析计算的数据,以及计算结果后的数据实际存储在分布式系统上,如HDFS上. 2.Hive某种程度来说也不进行数据计算,只是个解释器,只是将用户需要对数据处理的逻辑, ...
- 为什么使用Nosql:Nosql和SQL的区别
1.概念: SQL(Structured Query Language)数据库,指关系型数据库.主要代表:SQL Server.Oracle.MySQL.PostgreSQL. NoSQL(Not O ...
- java发送soapui格式的报文
import java.io.*;import java.net.HttpURLConnection;import java.net.URL; 使用java对soapui报文进行发送 public c ...
- docker查看挂载目录Volume
使用docker inspect命令查看container的volume信息,按照书本上面敲,发现一直报错: 使用命令如下: sudo docker inspect --format "{{ ...
- Arm11-mini6410入坑
Mini6410 学习Stm32的时候原子哥的资料真全啊,而且原子哥在他论坛也解答问题.然而现在学习ARM买了一套友善之臂的开发板,官方的资料只能刚好入门而已,其实入门都算不上.看了一下,网上的资料很 ...
- Delphi编程之爬取贴吧图片最终版
接着前面两篇文章的内容,我们今天把这个贴吧爬取图片的程序完善,让它具有可以下载贴吧多页和帖子多页图片的能力. 主界面设计如下,包含3个labelededit,3个button,1个memo,1个str ...