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 存 ...
随机推荐
- java 接口1
接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口 ...
- Thymeleaf中href与 th:href的区别
语法格式如下: <a th:href="@{/channel/page/add}">添加渠道 </a> <a href="/channel/ ...
- ecside中<c:table>使用
<ec:table action="sjzc/tbWaterproject!list.do" items="objList" var="tbWa ...
- readfile() file_get_content f
php.ini:memory_limit memory_limit是设置内存限制的,如果使用readfile()读取文件就会和这个有关, 调用readfile()函数将打开这个文件,并且将文件内容输出 ...
- SHELL输出带颜色字体
输出特效格式控制:\033[0m 关闭所有属性 \033[1m 设置高亮度 \03[4m 下划线 \033[5m 闪烁 \033[7m 反显 \033[8m 消隐 \ ...
- sqlserver智能提示插件-sql prompt(9.4.6)的安装及注册流程
官网下在地址:https://www.red-gate.com/products/sql-development/sql-prompt/ CSDN下载地址(对应的版本是9.4.6,其中包含安装包和注册 ...
- CSS效果:checkbox点选效果
HTML: <html lang="en"> <head> <meta charset="UTF-8"> <meta ...
- Knut重排算法
/// <summary> /// 这是Knut重排算法的实现 /// </summary> /// <param name="number"> ...
- EMMET 的HTM自动生成
{ // Custom snippets definitions, as per https://github.com/emmetio/emmet/blob/master/snippets.json ...
- Go Lang
IDE: https://www.jetbrains.com/products.html?fromMenu#type=ide Study: http://www.runoob.com/go/go-en ...