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

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

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     String path = request.getContextPath();
 5     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html>
 8 <html>
 9 <head>
10 <base href="<%=basePath%>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Insert title here</title>
13 </head>
14 <%
15     request.setCharacterEncoding("utf-8");
16     //1.拿到页面表单数据
17     String username = request.getParameter("username");
18     String password = request.getParameter("password");
19     if("admin".equals(username)&&"123456".equals(password)){
20         //2.登录成功 跳转到成功页面
21         //跳转方式:1.重定向  2.转发
22         //统计登录次数
23         Integer count = (Integer)application.getAttribute("count");
24         if(count==null){
25             count = 1;
26         }else{
27             count++;
28         }
29         application.setAttribute("count", count);
30         
31         session.setAttribute("username", username);//将用户信息保存在session中
32         request.setAttribute("welcome", "热烈欢迎");//往请求中添加新的参数,特点是可以添加任何数据类型
33         response.sendRedirect("success.jsp");
34         //request.getRequestDispatcher("success.jsp").forward(request, response);
35     }else{
36         //3.登录失败 调回登录页面
37         //response.sendRedirect("login.jsp?error=true");
38         request.setAttribute("msg", "用户名密码错误");
39         request.getRequestDispatcher("login.jsp").forward(request, response);
40     }
41 %>
42 <body>
43 
44 </body>
45 </html>

  success.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%
 4     String path = request.getContextPath();
 5     String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 6 %>
 7 <!DOCTYPE html>
 8 <html>
 9 <head>
10 <base href="<%=basePath%>"/>
11 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12 <title>Insert title here</title>
13 </head>
14 <%
15     //1.从登录页面的请求中获取表单参数
16     String username = request.getParameter("username");
17     //2.从控制页面的请求中获取新增的参数
18     String welcome = (String)request.getAttribute("welcome");
19     //3.从session中获取用户信息
20     String username2 = (String)session.getAttribute("username");
21     if(username2==null){//没有登录
22         response.sendRedirect("login.jsp");
23     }
24     String url = application.getRealPath("");
25     Integer count = (Integer)application.getAttribute("count");
26 %>
27 <body>
28     <h1>登录成功!<%=welcome+username2 %>,您是第<%=count %>位登录本网站的会员</h1>
29     <%=url %>
30 </body>
31 </html>



原文地址:https://www.cnblogs.com/wlxslsb/p/10725155.html