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的更多相关文章

  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. jQuery 入口函数主要有4种写法

    jqery  入口函数主要有4种写法,其中以第3种方法最为方便. <!DOCTYPE html> <html lang="en"> <head> ...

  2. js优化总结

    避免全局查找 在一个函数中会用到全局对象存储为局部变量来减少全局查找,因为访问局部变量的速度要比访问全局变量的速度更快些 function search() {    //当我要使用当前页面地址和主机 ...

  3. L343 中译英

    爱显摆的人遇事总喜欢标新立异.Showoffs never miss an opportunity to draw attention to themselves by some outrageous ...

  4. [Hive安装问题]

    启动Hive时出现: Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumen ...

  5. Java学习NO.3

    今日学习重点: while循环: 语法:while(条件表达式){ 循环操作 } 条件表达式符合,循环继续执行:否则,退出循环. 循环四要素:循环初始化.循环条件.循环操作.循环变量的改变 do-wh ...

  6. django 模型操作

    # 添加 Fruit.objects.create(name='Apple') # 更新 UserProfile.objects.filter(user=admin).update(**{'onlin ...

  7. L3-015 球队“食物链” (30 分)

    L3-015 球队“食物链” (30 分)   某国的足球联赛中有N支参赛球队,编号从1至N.联赛采用主客场双循环赛制,参赛球队两两之间在双方主场各赛一场. 联赛战罢,结果已经尘埃落定.此时,联赛主席 ...

  8. 2018-计算机系机试(第二批)-D-最小差值

    单点时限: 2.0 sec 内存限制: 256 MB 输入 n 个整数,输出最小差值.最小差值指所有数之间差的绝对值的最小数. 例如:3 个整数 1,2 和 6 的最小差值是 1. 输入格式 第一个数 ...

  9. Linux-1-用户管理

    目录: 用户账号的添加.删除与修改 用户口令的管理 用户组的管理 总结用户与用户组常用命令 ***用户账号的添加.删除与修改*** 添加用户:useradd  选项  用户名 选项: -c comme ...

  10. javascript 对象数组排序(按照科目级次)

    需求 从后台获取的数据是这样的                  上帝要这样的 背景 从后台获取到表格数据,然后填充到excel.当然是用js来填充的.js 本身的数组具有sort()功能.但是是针对 ...