get与post区别
比较项 | Get | post |
---|---|---|
参数出现在URL中 | 是 | 否 |
长度限制 | 有 | 无 |
安全性 | 低 | 高 |
URL可传播 | 是 | 否 |
request对象
作用
处理客户端请求
方法
方法名称 | 说明 |
---|---|
String getParameter(String name) | 根据表单组件名称获取提交数据 |
String[ ] getParameterValues(String name) | 获取表单组件对应多个值时的请求数据 |
void setCharacterEncoding(String charset) | 指定每个请求的编码 |
RequestDispatcher getRequestDispatcher(String path) | 返回一个RequestDispatcher对象,该对象的forward( )方法用于转发请求 |
示例
<% //读取用户名和密码 String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); ……//HTML页面 //读取复选框选择项 String[] channels = request.getParameterValues("channel"); if (channels != null) { for (String channel : channels) { out.println(channel); } } %>
内置对象
请求对象:request 输出对象:out 响应对象:response 应用程序对象:application 会话对象:session 页面上下文对象:pageContext 页面对象:page 配置对象:config 异常对象:exception
解决字符集编码问题
post方式
<% // 以POST方式提交数据时 // 设置读取请求信息的字符编码为UTF-8 request.setCharacterEncoding("UTF-8"); // 读取用户名和密码 String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); %>
get方式1
<% // 以GET方式提交数据时 // 读取用户名和密码 String name = request.getParameter("name"); // 对请求数据进行字符编码 name = new String(name.getBytes("ISO-8859-1"), "UTF-8"); %>
get方式2
在Tomcat目录结构\conf\server.xml中设置字符集 <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
response对象
作用
响应客户请求并向客户端输出信息
方法
void sendRedirect(String location)//重定向,客户端将重新发送请求到指定的URL
重定向与转发
转发
作用
转发是在服务器端发挥作用,将同一请求在服务器资源之间进行传递
特点
客户端浏览器的地址栏不会显示转向后的地址
举例
request.getRequestDispatcher("welcome.jsp").forward(request, response);
重定向
作用
重定向是在客户端发挥作用,通过发送一个新的请求实现页面转向
特点
在地址栏中可以显示转向后的地址
举例
response.sendRedirect("welcome.jsp");
session对象
定义
一个会话就是在一段时间内,一个客户端与Web服务器的一连串相关的交互过程
常用方法
方法名称 | 说明 |
---|---|
String getId() | 获取sessionid |
void setMaxInactiveInterval(int interval) | 设定session的非活动时间 |
int getMaxInactiveInterval() | 获取session的有效非活动时间(以秒为单位) |
void invalidate() | 设置session对象失效 |
void setAttribute(String key, Object value) | 以key/value的形式保存对象值 |
Object getAttribute(String key) | 通过key获取对象值 |
void removeAttribute(String key) | 从session中删除指定名称(key)所对应的对象 |
注意点
1.每个session对象都与一个浏览器窗口对应 ,重新开启一个浏览器窗口,可以重新创建一个session对象(不同版本浏览器可能有所差别) 2.通过超链接打开的新窗口,新窗口的session与其父窗口的session相同
举例
<% if ("admin".equals(name) && "admin".equals(pwd)) { // 如果是已注册用户 session.setAttribute("login", name); // 设置session过期时间 session.setMaxInactiveInterval(10*60); request.getRequestDispatcher("admin.jsp").forward(request, response); } else { response.sendRedirect("index.jsp"); } %> <% String login = (String) session.getAttribute("login"); if (login == null) { response.sendRedirect("index.jsp"); return; } %>
关于失效
手动设置失效:
session.invalidate()
超时失效
1.通过setMaxInactiveInterval( )方法,单位是秒
session.setMaxInactiveInterval(600);
<% session.setAttribute("login","admin"); session.setMaxInactiveInterval(600); response.sendRedirect("admin.jsp"); %>
2.通过设置项目的web.xml或Tomcat目录下的/conf/web.xml文件,单位是分钟
<session-config> <session-timeout>10</session-timeout> </session-config>
cookie
简介
是Web服务器保存在客户端的一系列文本信息
作用
对特定对象的追踪 实现各种个性化服务 简化登录
安全性
容易泄露信息
创建对象
Cookie newCookie = new Cookie(String key, String value);
写入
response.addCookie(newCookie);
读取
Cookie[] cookies = request.getCookies();
常用方法
方法名称 | 说 明 |
---|---|
void setMaxAge(int expiry) | 设置cookie的有效期,以秒为单位 |
void setValue(String value) | 在cookie创建后,对cookie进行赋值 |
String getName() | 获取cookie的名称 |
String getValue() | 获取cookie的值 |
int getMaxAge() | 获取cookie的有效时间,以秒为单位 |
cookie与session的对比
session | cookie |
---|---|
在服务器端保存用户信息 | 在客户端保存用户信息 |
session中保存的是Object类型 | cookie保存的是 String类型 |
随会话的结束而将其存储的数据销毁 | cookie可以长期保存在客户端 |
保存重要的信息 | 保存不重要的用户信息 |
application对象
作用
实现用户之间的数据共享
常用方法
方法名称 | 说 明 |
---|---|
void setAttribute(String key, Object value) | 以key/value的形式保存对象值 |
Object getAttribute(String key) | 通过key获取对象值 |
String getRealPath(String path) | 返回相对路径的真实路径 |
示例
<% Integer count = (Integer) application.getAttribute("count"); if (count != null) { count = 1 + count; } else { count = 1; } application.setAttribute("count", count); %>
作用域分类
page作用域
对应的作用域访问对象为pageContext
范围
指本JSP页面的范围
request作用域
对应的作用域访问对象为request
范围
一次请求
session作用域
对应的作用域访问对象为session
范围
一次会话
application作用域
对应的作用域访问对象为application
范围
整个应用的上下文
jsp内置对象回顾
内置对象名称 | 说明 |
---|---|
out | 用于向客户端输出数据 |
request | 主要用于处理客户端请求的数据信息 |
response | 用于响应客户端请求并向客户端输出信息 |
session | 用于记录会话状态的相关信息 |
application | 类似于系统的全局变量,用于实现Web应用中的资源共享 |
第一 用户界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="a.jpg" >
<style>
body{background:url(a.jpg) top left;
background-size:50%;}
</style>
<h1 style="color:pink" >
<% out.print("强将手下无弱兵"); %>
</h1>
<font color="red" size="3">
欢迎来到实力至上主义的教室
</font>
<h1 style="color:orange" >
吃好喝好玩好
</h1>
<%
if(application.getAttribute("count")==null){
application.setAttribute("count", 1);
}else{
int count=(int)application.getAttribute("count");
count+=1;
application.setAttribute("count", count);
}
out.print("亲爱的用户你已经访问了这个页面"+(int)application.getAttribute("count")+"次了 注意休息!!!");
%>
<%
//获取到的cookie 遍历 给初始账号 属性赋值 用户不用二次填写 增加用户体验
Cookie[] cookies=request.getCookies();
Cookie Cookie1= new Cookie("username","");
Cookie Cookie2= new Cookie("sex","");
for(Cookie cookie:cookies){
if("username".equals(cookie.getName())){
Cookie1=cookie;
}
if("sex".equals(cookie.getName())){
Cookie2=cookie;
}
}
%>
<form action="createUser.jsp" method="post" autocomplete="off">
<h5 style="color:green"><span>姓名:</span> <input type="text" name="name" value="<%=Cookie1.getValue() %>"></h5>
<h5 style="color:green"><span>性别:</span> <input type="text" name="sex" value="<%=Cookie2.getValue() %>"></h5>
<h5 style="color:green"><span>年龄:</span> <input type="text" name="age"></h5>
<h5 style="color:green"><span>电话:</span> <input type="text" name="phone"></h5>
<h5 style="color:green"><span>爱好 打球:</span> <input type="checkbox" name="hobby" value="打球"></h5>
<h5 style="color:green"><span>打代码:</span> <input type="checkbox" name="hobby" value="打代码"></h5>
<h5 style="color:green"><span>看源码:</span> <input type="checkbox" name="hobby" value="看源码"></h5>
<h5 style="color:green"><span>听歌:</span> <input type="checkbox" name="hobby" value="听歌"></h5>
<button>提交</button>
</form>
</body>
</html>
第二步 后端处理转发界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body background="37a9b.jpg" >
<style>
body{background:url(37a9b.jpg) top left;
background-size:10%;}
</style>
<%! boolean isnull(String str1){
if(str1==null || "".equals(str1.trim())){
return true;
}
return false;
}%>
<%
request.setCharacterEncoding("UTF-8");
String name =request.getParameter("name");
String sex =request.getParameter("sex");
String age =request.getParameter("age");
String phone =request.getParameter("phone");
String[] hobbys =request.getParameterValues("hobby");
StringBuilder str=new StringBuilder("");
if(isnull(name)||isnull(sex)||isnull(age)||isnull(phone)||hobbys==null|| hobbys.length<1){
str.append("输入值为空");
}else if(!("男".equals(sex)||"女".equals(sex))){
str.append("输入值性别不对");
}
else if(Integer.parseInt(age)<0||Integer.parseInt(age)>300){
str.append("输入值年龄不对");
}
else if(phone.length()!=11){
str.append("输入值电话长度不对");
}else if(hobbys.length == 0){
str.append("输入值为空");
}
if(str.length()==0){
str.append("成功");
}
Cookie newCookie=new Cookie("username",name);
response.addCookie(newCookie);
Cookie newCookie1=new Cookie("sex",sex);
response.addCookie(newCookie1);
//request.setAttribute("message", str);
//转发 同一个请求
//转发地址是不变的 url
//通过request.setAttribute()也可以实现值得传递
//request.getRequestDispatcher("index.jsp").forward(request,response);
//重定向 不同一个请求
//转发地址是变的 url
session.setAttribute("message", str);
//session.invalidate();
session.setMaxInactiveInterval(10);
response.sendRedirect("index.jsp");
%>
</body>
</html>
第三步 转发的界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body >
<style>
body{background:url(deb.jpg) top left ;
background-size:50%;}
</style>
<%=
"注册"+ session.getAttribute("message")
%>
</body>
</html>