3-jsp 内置对象、转发与重定向

时间:2021-11-01 02:07:29

1.request:请求
常用api:
  getParameter("name"):获取页面表单单个元素的值
  getParameterValues("name"):获取一组同名表单元素,比如说复选框
  setCharacterEncoding("UTF-8"):设置字符集,要在获取参数之前执行才能解决乱码问题
  setAttribute("name","value"):在请求对象中封装一个名字叫name的值
  getAttribute("name"):从请求对象中通过name来获取参数

2.response:响应对象
  作用:一般是服务器端跳转到客户端
  重定向方法:response.sendRedirect("url");
  转发:DispatchRequest dr = request.getDispatchRequest("url");
      dr.forward(request,response);
  重定向和转发的区别:重定向会改变跳转后的url地址,相当于两次请求,所以请求中的参数是不能往下传递的
          转发不会改变跳转后的url地址,实际上是一次请求,可以将请求中的参数往下一个页面传递

3.页面之前传参数
  a。表单提交:
  b。url传参: url+?+参数名=值&参数名=值.....
    http://localhost:8080/my/my.jsp?name=admin
    以上两种都是用request.getParameter获取
  c.往request.setAttribute()保存,用getAttribute("name")来取,前提是用转发来跳转

4.session:会话
  作用域:是一个浏览器或者说一个客户端与服务端之间的一次通信,可以包含多次请求和响应。每个客户端的会话都是独立的。
  生命周期:浏览器发送第一个请求开始,时限已到或者浏览器关闭将消亡
  作用:是可以将数据保存在服务端,互相独立,可以保存任何数据类型。重定向也可以用session传递数据
  常用api:
    setAttribute("name","value"):在session对象中封装一个名字叫name的值
    getAttribute("name"):从session对象中通过name来获取参数
    session.setMaxInactiveInterval(arg0):设置session的有效期

5.application:全局应用
  作用域:整个应用范围,所有的客户端都可以共用这个作用域
  生命周期:就是服务器的运行周期
  常用api:
  setAttribute("name","value"):在对象中封装一个名字叫name的值
  getAttribute("name"):从对象中通过name来获取参数

案例:做一个统计网站登录人数的案例,登录后显示给用户看您是第几位登录我们网站的用户(显示在success页面)

  login.jsp

<%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
String error = (String)request.getAttribute("msg");
if(error==null){
error="";
}
%>
<body>
<div align="center">
<h1>登录页面</h1>
<form action="login/control.jsp" method="post">
用户名:<input name="username"><br>
密码:<input type="password" name="password"><span style="color: red"><%=error %></span><br>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>

  control.jsp

 <%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
request.setCharacterEncoding("utf-8");
//1.拿到页面表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
if("admin".equals(username)&&"123456".equals(password)){
//2.登录成功 跳转到成功页面
//跳转方式:1.重定向 2.转发
//统计登录次数
Integer count = (Integer)application.getAttribute("count");
if(count==null){
count = 1;
}else{
count++;
}
application.setAttribute("count", count); session.setAttribute("username", username);//将用户信息保存在session中
request.setAttribute("welcome", "热烈欢迎");//往请求中添加新的参数,特点是可以添加任何数据类型
response.sendRedirect("success.jsp");
//request.getRequestDispatcher("success.jsp").forward(request, response);
}else{
//3.登录失败 调回登录页面
//response.sendRedirect("login.jsp?error=true");
request.setAttribute("msg", "用户名密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>
<body> </body>
</html>

  success.jsp

 <%@ 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 http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
//1.从登录页面的请求中获取表单参数
String username = request.getParameter("username");
//2.从控制页面的请求中获取新增的参数
String welcome = (String)request.getAttribute("welcome");
//3.从session中获取用户信息
String username2 = (String)session.getAttribute("username");
if(username2==null){//没有登录
response.sendRedirect("login.jsp");
}
String url = application.getRealPath("");
Integer count = (Integer)application.getAttribute("count");
%>
<body>
<h1>登录成功!<%=welcome+username2 %>,您是第<%=count %>位登录本网站的会员</h1>
<%=url %>
</body>
</html>