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 存 ...
随机推荐
- 依赖注入之setter注入---只需修改配置,电脑就可以安装不同的打印机;读取properties配置文件并创建实例;实现不采用new的方式直接实例化对象
1.项目截图 2.黑白打印机类 package com.example.demo.printer; public class GrayPrinter implements Printer{ @Over ...
- IntelliJ IDEA及maven、git下载与配置
maven下载地址:http://maven.apache.org/download.cgi,下载bin文件然后解压 maven环境变量配置: MAVEN_HOME:D: \install\apac ...
- TypeError: format string
先来看一段Python代码: class Negate: def __init__(self, val): self.val = -val def __repr__(self): return str ...
- mongo-2ds索引对超过半球范围的适用性测试
以下测试均基于mongo v4.0 win10 一.GeoJSON GeoJSON是一种基于json的经纬度描述数据格式.在这里主要服务于2dsphere索引查询. 基本格式 <type:&q ...
- .NET复习笔记-泛型
1.yield关键字用于返回迭代器具体的值,如下框代码所示 /// 返回0~9整数集合 public static IEnumerable<int> yieldSampleMethod() ...
- json&pickle模块
序列化:我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化 反序列化:把变量内容从序列化的对象重新读到内存中,这一过程称为反序列化 为什么要序列化? 1.持久保存状态 一个软件的执行就是在处 ...
- Python 锁 同步 互斥锁
import time from threading import Lock,Thread num = 100 def f1(loc): loc.acquire() global num tmp = ...
- 正则表达式andJS内存空间详细图解
http://www.runoob.com/js/js-regexp.html https://blog.csdn.net/pingfan592/article/details/55189622
- 大数据 - hadoop - HDFS+Zookeeper实现高可用
高可用(Hign Availability,HA) 一.概念 作用:用于解决负载均衡和故障转移(Failover)问题. 问题描述:一个NameNode挂掉,如何启动另一个NameNode.怎样让两个 ...
- 修改XAMPP的默认根目录
XAMPP安装完成后,默认根目录路径是C:\xampp\htdocs,如果想要在服务器下运行文件就必须把该文件copy到C:\xampp\htdocs下.超麻烦不说,公司代码总不能放进去运行吧...所 ...