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. dom编程艺术笔记2--第三章

    DOM 1.dom:简单理解为文档对象模型<==>节点树 2.宿主对象:浏览器提供的对象—>document对象 3. html标签为树顶点元素 head body为html子元素以 ...

  2. JavaWeb开发流程

  3. 【SoftwareTesting】Homework1

    The errors I will mention after are from the project in the last semester. The project is a Java pro ...

  4. admin 自定义字段颜色 并加以简单判断

    在model中class Books(models.Model): nid = models.AutoField(primary_key=True, ) title = models.CharFiel ...

  5. Node.js 串口通讯 node-serialport 使用说明

    官网:https://serialport.io/en/ 安装:npm install serialport Parsers说明:parser-byte-length:大概意思是定义了一个长度为len ...

  6. selenium中的alter弹框

    from selenium import webdriverimport timedriver=webdriver.Chrome()driver.get('http://ui.imdsx.cn/uit ...

  7. tensorFlow入门实践(三)实现lenet5(代码结构优化)

    这两周我学习了北京大学曹建老师的TensorFlow笔记课程,认为老师讲的很不错的,很适合于想要在短期内上手完成一个相关项目的同学,课程在b站和MOOC平台都可以找到. 在卷积神经网络一节,课程以le ...

  8. IP通信基础课堂笔记----第三章(自认为的重点)

    网络层协议IP IP数据包格式:固定部分        {  标识(16):计数器,源站每发送一个分组,标识+1:源IP地址的标识是全网唯一的. (包含TCP/UDP首部)  |  标志(3):当最低 ...

  9. GoGland 快捷键说明

    关于Gogland一些常用快捷键的说明,我在网上没有搜索到,于是乎,我找到了官网中的视频介绍,然后将其中的一部分摘录了出来,希望能帮住大家... Gogland——使用说明前面是苹果|后面是linux ...

  10. Linux 驱动——Button驱动2

    button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...