HttpServletRequest对象:
接收请求的 : get , post请求
get: ? & 200 不安全,
post: 放在浏览器的 请求头中 Network 200 OK 404
方法: request.getParameter(“form表单中的 name 属性值”);
request.setCharacterEncoding(“UTF-8”);
request.getRequestDispatcher(“目标地址”).forward(request,response);//转发
request.getSession();------>session
request.setAttribute(“名”,值);
request.getAttribute(“名”);
HttpServletResponse对象:
response.getWriter().write("");
response.setContentType(“text/html;charset=UTF-8”);
response.sendRedirect(“目标地址”); //重定向
HttpSession对象:
session.setAttribute(“域对象的名”,值);
session.getAttribute(“域对象的名”);
ServletContext:servlet上下文;
sc.setAttribute(“域对象的名”,值);
sc.getAttribute(“域对象的名”);
sc.getInitParameter("");
web.xml
ServletConfig:
getInitParameter();
config.getServletContext();
config.getServletName();
jsp: java server page , java服务器页面
属于 动态 页面 运行在 tomcat服务器上 ,编译后 是一个 servlet类
指令:
1)page 当前页面配置信息
2)include 包含其他 页面
3)taglib 引入 第三方 类库
<%@ page | include taglib >
表达式 , 声明 ,
<%= 输出的结果 >
<%
User user = new User();
%>
1)jsp的 9大内置对象:
request ------ > 对象 servlet中的 request对象
session -------> 对象servlet中的 session对象
response -------> 对象 servlet中的 response对象
applicationContext —> 对象servlet中的 servletContext
pageContext 代表当前页面
page 当前的信息
out 用于输出
exception 异常
config 当前页面的配置信息 servletConfig
====================================================
2)jstl:
java standard tag library
java 标准标签库
用于 jsp ,是 jsp 核心 标签库 ,第三方的
1)导入jstl的 jar包
2)在 jsp页面 导入 路径
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
3)使用 jstl的 标签 完成数据的遍历
3)ajax
ajax: 不是一个 新的技术,最早 是 google 谷歌 提出的
异步的 局部刷新技术
Asynchronous java script and xml
js and xml
提高用户体验度;
异步:
自己 干 自己的 相互 不影响
同步:
同步锁
他们两个 要保持同一种 状态
1)需要创建 XMLHttpRequest
2)建立连接
3)发送请求
4)判断 当前 响应的状态
分 5个状态 我们只需要 第四个
if(xml.status ==200 && xml.readyState == 4){
var a = xmlHttp.responseText;
alert(a);
}
基于jquery的 ajax:
$(function() {
$(“input[name=‘username’]”).blur(function() {
var name = $(“input[name=‘username’]”).val();
$.ajax({
url : “userServlet”,
type : “GET”,
async : true,
contentType:“application/json”,
data : {uname : name},
success : function(data) {
alert(data);
if (data == “success”) {
$("#sp").html(“√”);
} else {
$("#sp").html(“×”);
}
}
})
})
})
jstl:
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basepath%>">
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<c:forEach items="${goods }" var="good">
${good }<br/>
</c:forEach>
</body>
</html>
基于js的ajax:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="userServlet" method="get">
用户名:<input type="text" onblur="checkName(this.value)" name="uname"><span id="sp"></span><br/>
密 码:<input type="password" name="pwd"><br/>
<input type="submit" value="登录">
</form>
</body>
<script type="text/javascript">
function checkName(a){
//需要 使用 ajax完成 局部刷新 技术 , 首先必须 创建 XMLHttpRequest对象
var xmlhttp = new XMLHttpRequest();
//建立连接
xmlhttp.open("GET","userServlet?uname="+a,true);// 与 ajax 引擎 建立 连接
//发送请求
xmlhttp.send(); //发送
//获取响应
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var b = xmlhttp.responseText;//这就是我 响应的结果
if(b == "success"){
document.getElementById("sp").innerHTML = "<span style='color: green'>√</span>";
}else{
document.getElementById("sp").innerHTML = "<span style='color: red'>×</span>";
}
}
}
}
</script>
</html>
基于jquery的 ajax:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basepath%>">
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<form action="userServlet" method="get">
用户名:<input type="text" name="uname"><span id="sp"></span><br/>
密 码:<input type="password" name="pwd"><br/>
<input type="submit" value="登录">
</form>
</body>
<script type="text/javascript" src="js/jquery-1.11.3.js"></script>
<script type="text/javascript">
//页面 加载函数
$(function(){
$("input[name='uname']").blur(function(){
var name = $("input[name='uname']").val();
$.ajax({
url :"userServlet",
type : "GET",
async : true,
contentType :"application/json",
data : {uname : name},
success :function(data) {
if (data == "success") {
$("#sp").html("<span style='color:green'>√</span>");
}else{
$("#sp").html("<span style='color:red'>×</span>");
}
}
})
})
})
</script>
</html>