201819101035 翟鑫月 2020-09-16作业

 

1.page范围

代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%
    pageContext.setAttribute("name","zhxy");
    pageContext.setAttribute("time","9/16");
  %>
  姓名:${pageScope.name} <br>
  时间:${pageScope.time} <br>
  </body>
</html>

 

结果:

2.request属性范围

(1)getParameterValues


  <form action="request-1.jsp" method="post">
    username:<input type="text" name="user"/><br/>
    password:<input type="password" name="pwd"/><br/>
    <input type="submit" name="登录"/>
  </form>

<%
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("user");
    String pwd=request.getParameter("pwd");
    System.out.println(name+","+pwd);
    if("zhxy".equals(name)&&"12345".equals(pwd))
    {
        out.println("登录成功");
    }
    else
        {
        out.println("errors");
        }
%>

获取结果

 

(2)request.gettAttribute(key)

<form action="request-1.jsp" method="post">
    username:<input type="text" name="user"/><br/>
    password:<input type="password" name="pwd"/><br/>
    <input type="submit" name="登录"/>
<%
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("user");
    String pwd=request.getParameter("pwd");
    System.out.println(name+","+pwd);

    if("zhxy".equals(name)&&"12345".equals(pwd))
    {
        out.println("登录成功");
        request.setAttribute("age","20");
        request.getRequestDispatcher("request-2.jsp").forward(request,response);
    }
    else
        {
        out.println("errors");
        }
%>
欢迎,<%=request.getParameter("user") %><br/>
年龄:<%=request.getAttribute("age")%>

获取结果 

 3.resonpse

<%
    request.setCharacterEncoding("UTF-8");
    String name=request.getParameter("user");
    String pwd=request.getParameter("pwd");
    System.out.println(name+","+pwd);

    if("zhxy".equals(name)&&"12345".equals(pwd))
    {
        out.println("登录成功");
        request.setAttribute("age","20");
        request.getRequestDispatcher("request-2.jsp").forward(request,response);
    }
    else
        {
        out.println("errors");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.sendRedirect("response.jsp");
        }
%>

 

<body>
登录失败!!
</body>

获取结果

4.session 

  <form action="session.jsp" method="post">
    username:<input type="text" name="user"/><br/>
    password:<input type="password" name="pwd"/><br/>
    <input type="submit" name="登录"/>
  </form>
<%
    request.setCharacterEncoding("UTF-8");
String name=request.getParameter("user");
String pwd=request.getParameter("pwd");
if("zhxy".equals(name)&&"12345".equals(pwd))
  {
    session.setAttribute("name",name);
    session.setAttribute("pwd",pwd);
    response.sendRedirect("session-1.jsp");
    System.out.println(session.getId());
    session.setMaxInactiveInterval(1000*60*30);
    session.invalidate();
  }
else
    {
        response.sendRedirect("fail.jsp");
    }
%>
<%
    String name=(String)session.getAttribute("name");
    String pwd=(String)session.getAttribute("pwd");
    out.println("登录成功");
    out.println("name+","+pwd");
    System.out.println(session.getId());
%>
<body>
登录失败!!
</body>

5. out

        <%
            out.print("hello world");
        %>

结果

6.pageContext

  <form action="<%=request.getContextPath()%>/session.jsp" method="post">
    username:<input  type="text" name="user"/><br/>
    password:<input  type="password" name="pwd"/><br/>
    <input  type="submit" name="登陆"/>
    </form>
    <h2>pageContext对象</h2>
    <%
    pageContext.getRequest();
    pageContext.getResponse();
    pageContext.getSession();
    pageContext.getOut();
    String path = request.getContextPath();
    out.println("当前上下文的绝对路径:"+path);
    %>

结果

7.application

  <%
   String serverPath = application.getContextPath();
   out.println(serverPath);
   application.setAttribute("", "");
 %>

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值