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 存 ...
随机推荐
- 汉明码(Hamming)编码与纠错原理
一 汉明码的编解码说明 (一)编码 Hamming(12,8) N=12,表示编码后的比特长度 K=8,待编码数据的比特长度 R=N-K=4,校验位的比特长度 D=3 汉明距离:相邻行之间不同比特数据 ...
- JavaWeb开发流程
- css中width:auto和width:100%的区别是什么
width的值一般是这样设置的: 1,width:50px://宽度设为50px 2,width:50%://宽度设为父类宽度的50% 3,还有一个值是auto(默认值),宽度是自动的,随着内容的增加 ...
- Node.js和html数据交互(一) form表单
一.form表单提交数据 数据流向:前端 - > 后端 1.get方法 (action是提交路径,method是提交方法,get方法可以不写) 前端: <form action=" ...
- vue跳坑笔记
序号 报错截图 关键词 解决办法 1 - code EPERM errno 4048 syscall scandir operation not permitted 清除npm缓存,命令: npm c ...
- selenium多窗口切换定位
from selenium import webdriverimport unittest,time,refrom selenium.webdriver.common.keys import Keys ...
- 牛客网小a的旅行计划
地址:https://ac.nowcoder.com/acm/contest/223/B 此题是一道套公式题目,不难,ans=(pow(4,n+1)-4*pow(3,n+1)+6*pow(2,n+1) ...
- beifen
Comparison of Models for Predicting the Outcome of Craniocerebral Injury by Using Machine Learning ...
- 如何开发微信小程序学习
文件中 project.config.json是开发者工具为我们自动生成的一个开发者工具的配置文件,主要是保存了一些我们对开发者工具的个性化配置. 一个微信小程序应用至少要有两个文件 一个是app.j ...
- JAVA高级-面试题总结
最近面试了一些公司,针对面试中遇到的问题在此记录,提升自己,造福大家 一.java源码相关 ArrayList创建和add等各种api使用原理 HashMap 的创建,put原理,和HashTable ...