Servlet学习(1)
取得HttpSession实例
在Servlet中去个一个Session对象,可以通过HttpServletRequest接口完成。
HttpSession ses = request.getSession();
取得ServletContext实例
Application内置对象是ServletContext借口的实例,表示是Servlet上下文
| 方法 | 类型 | 描述 |
| public ServletContext getServletContext | 普通 | 取得ServletContext对象 |
ServletContext application = super.getServletContext();
Servlet跳转
- 客户端跳转
在Servlet中如果想要进行客户端跳转,直接使用HttpServletResponse借口的sendRedirect()方法即可,但是需要注意的是,此跳转只能传递session 范围的属性,而无法传递request范围的属性。
<%=request.getAttribute("ss")%>值为空,<%=session.getAttribute("ss")%>能够取得到传进来的
由于是客户端跳转,所以跳转后的地址栏是会改变的。只能接收到session属性范围的内容,二request属性范围的内容是无法接收到的,这是因为request属性范围只有在服务器端跳转才有用。
response.sendRedirect("info.jsp")
- 服务端跳转
在服务器跳转中必须依靠RequestDispatcher借口完成。
public void forward(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面跳转
public void include(ServletRequest request,ServletResponse response) throws ServletException,IOException 页面包含
使用RequestDispatcher借口的frrward()方法即可完成跳转功能的实现,但是如果要想使用此借口还需要使用ServletRequest借口提供的
public RequestDispatcher getRequestDispatcher(String path) 进行实例化
RequestDispatcher view=request.getRequestDispatcher("result.jsp");
view.forward(request, response);
服务器端跳转后,页面的路径不会发生改变,而且此时可以在跳转后的JSP文件中接收到session以及request范围的属性
Servlet学习(1)的更多相关文章
- JSP&Servlet学习手册
JSP&Servlet学习手册 沙琪玛 书 目录 JSP 指令... 3 书写方式... 3 指令列表... 3 JSP 内置对象... 3 内置对象特点... 3 常用内置对象... 3 o ...
- Servlet 学习笔记
Servlet 运行在服务器上的 java 类: Servlet 容器为 javaWeb 应用提供运行时环境,负责管理 servlet 和 jsp 生命周期,以及管理他们的共享数据. 现在我们知道了 ...
- Servlet学习:(三)Servlet3.0 上传文件
转: Servlet学习:(三)Servlet3.0 上传文件 2018年08月03日 11:57:58 iDark_CSDN 阅读数:362 一.注意事项 客户端(浏览器) 表单的提交方法必须是 ...
- Servlet学习(九)——request
request运行流程在Servlet学习(四)——response已介绍,不再赘述 1.通过抓包工具获取Http请求 因为request代表请求,所以我们可以通过该对象分别获得Http请求的请求行, ...
- # jsp及servlet学习笔记
目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...
- Servlet学习笔记(四)
目录 Servlet学习笔记(四) 一.会话技术Cookie.session 1. 什么是会话技术? 2. 会话技术有什么用? 3. Cookie 3.1 什么是Cookie? 3.2 使用Cooki ...
- Servlet学习笔记(三)
目录 Servlet学习笔记(三) 一.HTTP协议 1.请求:客户端发送欸服务器端的数据 2.响应:服务器端发送给客户端的数据 3.响应状态码 二.Response对象 1.Response设置响应 ...
- Servlet学习笔记(二)
目录 Servlet学习笔记(二) Request对象 1.request和response对象: 2.request对象继承体系结构: 3.什么是HttpServletRequest ? 4.Htt ...
- servlet 学习(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- JavaWeb学习总结-04 Servlet 学习和使用
一 Servlet 1 Servlet概念 Servlet时运行在服务器端的Java程序. Servlet的框架核心是 javax.servlet.Servlet 接口. 所有自定义的Servlet都 ...
随机推荐
- loading上下左右居中
.loading { width: 50px; height: 60px; text-align: center; font-size: 10px; position:fixed; left:50%; ...
- tableView刷新中的问题
在开始之前先上一张效果图 相信大家都看到了“店铺优惠”这一栏,在这里假设这一栏就是单独的一个cell,当无店铺优惠的时候不可点击在有店铺优惠的时候会弹出优惠列表,选中并返回时会刷新数据,所以弹出视图采 ...
- Python语言 介绍
一.python介绍python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...
- 虚拟机centos 同一个tomcat、不同端口访问不同的项目
在tomcat中复制webapps目录,并重命名:
- Android Studio 常见命令
android studio 常用命令 Description Mac Linux/Win Lookup IDE commands Cmd+Shift+A Ctrl+Shift+A Open Clas ...
- 358. Rearrange String k Distance Apart
/* * 358. Rearrange String k Distance Apart * 2016-7-14 by Mingyang */ public String rearrangeString ...
- hdu5379||2015多校联合第7场1011 树形统计
pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...
- leetcode 题解 || Remove Nth Node From End of List 问题
problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- java性能监控工具jps-windows
jps Lists the instrumented Java Virtual Machines (JVMs) on the target system. This command is experi ...
- 小胖说事20--------GCD笔记
1.系统提供的dispatch方法 为了方便的使用GCD.苹果提供了一些方法方便我们将BLOCK放在主线程或者后台程序运行.或者延后运行. //后台运行: dispatch_async(dispatc ...