Servlet核心接口
1.4 核心接口概述
ServletConfig接口:其
中的方法主要可以访问两项内容:Servlet初始化参数和ServletContext对象。
ServletContext接口:
代表当前Servlet运行环境, Servlet容器在启动一个Web应用时,会为该应用创建一个唯一的ServletContext对象供该应用中的所有Servlet对象共享,Servlet对象可以通过ServletContext对象来访问容器中的各种资源。
ServletContext对象可以获取应用范围的初始化参数、在应用范围内存取共享数据、访问当前Web应用的信息、访问当前容器的信息和输出日志、访问服务器端的文件系统资源。
HttpServletRequest接口:
该接口继承了ServletRequest接口,是专用于HTTP协议的子接口,用于封装HTTP请求信息。
HttpServletRequest对象主要用于获取请求报文信息、获取网络连接信息、存取请求域属性。
HttpServletResponse接口继承自ServletResponse,是专用于HTTP协议的子接口,用于封装HTTP响应消息。
HttpServletResponse接口:
该接口主要用于创建响应报文。
注意:
ServletContex接口、HttpServletRequest接口具有相同的存取域属性的方法。
HttpServletRequest接口提供了统一的获取GET请求参数和POST请求参数的方法。
Servlet声明的两种方式
注解@WebServlet
web.xml进行声明配置
什么是JSP、JSP四大作用域及JSP执行过程
什么是JSP?
Jsp是一种动态网页技术标准,是一种用于开发包含动态内容的web页面技术,是一种服务器端脚本语言,与Servlet一样,也是一种基于Java的服务器端技术,主要用于产生动态网页内容。
JSP四大作用域?
四种作用域的生命周期和可访问性介绍如下:【高级的web开发还在使用的技术】
页面域(page scope),页面域的生命周期是指页面执行期间。存储在页面域的对象只对于它所在页面是可访问的(仅限于当前页面使用)。
使用pageContext对象;
请求域(request scope),请求域的生命周期是指一次请求过程(用户端浏览器发送请求到服务接收接收这个阶段),
包括请求被转发(forward)或者被包含(include)的情况。
存储在请求域中的对象只有在此次请求过程中才可以被访问。
使用request对象;
会话域(session scope),会话域的生命周期是指某个客户端与服务器所连接的时间,包括request阶段和response阶段;在一次会话过程中,可以有多次请求和响应;
客户端在第一次访问服务器时创建会话,在关闭浏览器或主动退出后,会话结束。
存储在会话域中的对象在整个会话期间(可能包含多次请求)都可以被访问。
使用session对象;
应用域(application scope),应用域的生命周期是指从服务器开始执行服务到服务器关闭为止,是四个作用域中时间最长的。
存储在应用域中的对象在整个应用程序运行期间可以被所有JSP和Servlet共享访问,在使用时要特别注意存储数据的大小和安全性,
否则可能会造成服务器负载过重和线程安全性问题。
使用application对象;
存储在application中的数据是可以被多个pageContext、request、session对象同时访问;
jsp的四种作用域:
总结:
一般用于数据存储,就是把数据存储在这四个作用域对象中,使用的时候,再从这四个作用域对象中取出;【缓存作用】
存储范围从小到大;生命周期从小到大:
pageContext(page)、request、session、application
pageContext(page)、request、session仅对当前用户有效,例如;用户a不会访问到用户b存储在这三个对象中的值;
application范围最大、生命周期最长,所有的用户之间的数据都是可以互相访问的;
jsp的四种作用域都是运行在服务器端的;由web服务器进行维护和管理;
在缓存对象中一般使用 setAttribute设值,使用getAttribute取值;这是一种一对操作,get方法的参数名和set方法的参数名要保持一致;
JSP执行过程?
1、客户端通过浏览器,向服务器发出请求,在该请求中包含了请求的资源和路径,这样,当服务器接收到该请求后,就可以知道被请求的资源
2、服务器根据接收到的客户端的请求,来加载被请求的JSP文件
3、Web服务器中的JSP引擎,会将被加载的JSP文件转化为Servlet
4、JSP引擎,将生成的Servlet代码,编程成Class文件
5、服务器执行这个Class文件
6、最后,服务器将执行结果,发送给浏览器进行显示
常用监听器和监听器创建步骤
常用监听器
创建步骤
原生JavaScript完成Ajax请求
原生的ajax的开发应用
(1)创建ajax引擎对象 XMLHttprequest对象(浏览器支持)
(2)委托ajax引擎发送服务器请求(get/post)
(3)根据web服务器响应结果状态,进行回调处理
(4)在回调函数中,进行浏览器局部刷新操作
ajax引擎的工作流程:
使用open函数传入客户端委托请求的各项参数;
ajax引擎对象有一个 readyState 属性,该属性的发生变化时就会触发一个叫做 onreadystatechange 事件 ;
onreadystatechange 事件所执行的函数就是所说的回调函数;也就是通过onreadystatechange 事件进行回调处理;
ajax引擎中包含:http状态码(status)和ajax引擎自身状态码(readyState):
http状态码由status属性表示:200/404/500
200: "OK"
404: 未找到页面
ajax引擎readyState状态:0/1/2/3/4
0: 请求未初始化
1: 服务器连接已建立
2: 请求已接收
3: 请求处理中
4: 请求已完成,且响应已就绪
<%@ page contentType
="text/html;charset=UTF-8" language
="java" %>
<html>
<head>
<title>原生的ajax处理
</title
>
<script type
="text/javascript">
function
doAjax() {
console
.log("正准备执行ajax请求");
var xmlhttp
;
if (window
.XMLHttpRequest
) {
xmlhttp
= new XMLHttpRequest();
} else {
xmlhttp
= new ActiveXObject("Microsoft.XMLHTTP");
}
var userName
= document
.getElementById("userName").value
;
xmlhttp
.open("GET", "AjaxDemoServlet?userName=" + userName
, true);
xmlhttp
.send();
xmlhttp
.onreadystatechange
= doCallBack
;
function
doCallBack() {
if (xmlhttp
.readyState
== 4 && xmlhttp
.status
== 200) {
document
.getElementById("showMsg").innerHTML
= xmlhttp
.responseText
;
}
}
}
function
doAjax_post() {
console
.log("正准备执行ajax请求");
var xmlhttp
;
if (window
.XMLHttpRequest
) {
xmlhttp
= new XMLHttpRequest();
} else {
xmlhttp
= new ActiveXObject("Microsoft.XMLHTTP");
}
var userName
= document
.getElementById("userName").value
;
xmlhttp
.open("POST", "AjaxDemoServlet", true);
xmlhttp
.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp
.send("userName="+ userName
);
xmlhttp
.onreadystatechange
= doCallBack
;
function
doCallBack() {
if (xmlhttp
.readyState
== 4 && xmlhttp
.status
== 200) {
document
.getElementById("showMsg").innerHTML
= xmlhttp
.responseText
;
}
}
}
</script
>
</head
>
<body>
<form>
用户名:
<input type
="text" id
="userName" name
="userName">
会掉结果:
<label id
="showMsg"></label
>
<input type
="button" value
="ajaxDemo" onclick
="doAjax()">
<input type
="button" value
="ajaxDemo_Post" onclick
="doAjax_post()">
</form
>
</body
>
</html
>
ServletContext对象获取方式
方法一:通过GenericServlet提供的 getServletContext()
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext3 = getServletContext();
}
方法二:通过ServletConfig提供的getServletContext()
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext2 = getServletConfig().getServletContext();
}
方法三:通过HttpServletRequest获取
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext1 = req.getServletContext();
}
方法四:通过HttpSession获取。
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getSession().getServletContext();
}
cookie和session
cookie和session的区别?
1、cookie数据存放在客户端,session数据放在服务器上。
2、cookie不是很安全,别人可以分析存放在本地的cookie并进行cookie欺骗,考虑到安全应当使用session。
3、session会在一定时间内保存在服务器上,当访问增多,会比较占用你服务器的性能,考虑性能应当使用cookie。
4、不同浏览器对cookie的数据大小限制不同,个数限制也不相同。
5、可以考虑将登陆信息等重要信息存放为session,不重要的信息可以放在cookie中。
Cookie
定义
作用
el表达式
EL(Expression Language,表达式语言)是一种简单的语言,可以方便地访问和处理应用程序数据,
而无需使用JSP脚本元素(Scriptlet)或JSP表达式。
EL在容器默认配置下处于启用状态,每个JSP页面也可以通过page指令的isELIgnored属性单独设置其状态。
编程
cookie的获取的添加
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
Cookie cookie1
= new Cookie("cookie的属性名", "cookie的属性值");
cookie1
.setComment("Web Host Name");
cookie1
.setMaxAge(24*60*60);
response
.setCharacterEncoding("UTF-8");
response
.setContentType("text/html;charset=UTF-8");
response
.addCookie(cookie1
);
response
.sendRedirect("./index.jsp");
}
cookie创建
cookie的获取
cookie访问路径
cookie存活时间
请求转发和重定向及参数传递
public class UserLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
doGet(request
, response
);
}
protected void doGet(HttpServletRequest request
, HttpServletResponse response
) throws ServletException
, IOException
{
request
.setCharacterEncoding("UTF-8");
String strAdminName
= request
.getParameter("adminName");
String strAdminPsw
= request
.getParameter("adminPsw");
System
.out
.println("登录用户名:" + strAdminName
+ ";密码:" + strAdminPsw
);
request
.setAttribute("loginedName", strAdminName
);
request
.getRequestDispatcher("logined.jsp").forward(request
, response
);
}
}
前端页面循环显示集合信息
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/7
Time: 14:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户信息新增显示</title>
<%-- 引入jquery资源文件:必须在bootstrap资源文件之前引入--%>
<script type="text/javascript" src="jquery-1.12.4/jquery-1.12.4.js"></script>
<%-- 引入bootstrap的资源文件--%>
<script type="text/javascript" src="bootstrap-3.3.6-dist/js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="bootstrap-3.3.6-dist/css/bootstrap.css">
</head>
<body style="width: 100%;">
<div style="width: 90%;margin: 10px auto">
<div>
<table class="table table-hover">
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>地址</th>
<th>联系电话</th>
<th>注册日期</th>
</tr>
</thead>
<tbody>
<c:forEach items="${sessionScope.lstAllUserInfo}" var="eachBean" varStatus="stat">
<tr>
<th scope="row">${stat.index + 1}</th>
<td>${eachBean.userName}</td>
<td>${eachBean.userAddress}</td>
<td>${eachBean.userTel}</td>
<td>${eachBean.userRegDate}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</body>
</html>