• JSP的内置对象(上)


    1.JSP内置对象的概念:JSP的内置对象时Web容器所创建的一组对象,不使用new关键字就可以使用的内置对象

    2.JSP九大内置对象内置对象:out ,request ,response ,session ,application ,page , pageContent ,exception ,config等,前五个是经常使用的内置对象

    3.缓冲区:Buffer 就是内存中一块区域,用来临时保存数据

    4.out对象:是JspWriter类的实例,是向客户端输出内容常用的对象,(客户端大多数是指浏览器)

      常用的方法:

      

    out的例子:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>out内置对象</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
      <%
        out.println("<h2>静夜思</h2>");
        out.println("床前明月光<br/>");  
        out.println("疑是地上霜<br/>"); 
        out.flush();
        //out.clear();//这里会抛出异常,一旦抛出异常,后面的则不会被打印
        //out.clearBuffer();//这里不会抛出异常
        out.println("举头望明月<br/>");  
        out.println("低头思故乡<br/>");    
       %>
       缓冲区大小: <%=out.getBufferSize() %>byte<br/>
       缓冲区剩余:<%=out.getRemaining() %>byte<br/>
       是否自动清除缓冲区:<%=out.isAutoFlush() %><br/>
       
        
      </body>
    </html>

     5.表单提交数据有两种:get和post两种

    •  get:是通过明文的方式通过url提交数据,数据可以再url中看到,提交的数据最多不能超过3KB,安全性能低但是效率比post高,适合提交不大,安全性不高的数据,比如:搜索,查询等功能
    •  post:将用户提交的信息封装在HTML HEADER中,适合提交数据量大,安全性高的用户信息。比如。注册,修改等功

    request

    1.客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest实例。request对象具有请求域,即完成客户端的请求之前,它一直有效。

    2.request.setCharacterEncoding("UTF-8"),request.getParameter(),request.getParameterValues()例子:

    前端页面reg.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'reg.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
      <h1>用户登录</h1>
        <hr>
        <form action="JSP/request.jsp" method="get" name="regForm">
            <table>
                <tr>
                    <td>用户:</td>
                    <td><input type="text" name="usename">
                    </td>
                </tr>
                <tr>
                    <td>爱好:</td>
                    <td>
                    <input type="checkbox" name="hobby" value="read">阅读
                    <input type="checkbox" name="hobby" value="music">音乐
                    <input type="checkbox" name="hobby" value="movic">电影
                    <input type="checkbox" name="hobby" value="internet">上网
                    </td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="提交">
                    </td>
                </tr>
            </table>
        </form>
        <br>
        <br>
        <a href="JSP/request.jsp?usename=zhangsan">测试URL传参数
      </body>
    </html>

    request页面:request.jsp:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://"
                + request.getServerName() + ":" + request.getServerPort()
                + path + "/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'request.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <h1>request内置对象</h1>
        <hr>
        <!-- 设置Tomcat的编码方式,解决中文乱码问题 -->
        <%
            request.setCharacterEncoding("UTF-8");
        %>
        用户名:<%=request.getParameter("usename")%>
        爱好:<%
            if (request.getParameterValues("hobby") != null) {
                String[] favourites = request.getParameterValues("hobby");
                for (int i = 0; i < favourites.length; i++) {
                    out.println(favourites[i] + "&nbsp;&nbsp;");
                }
            }
        %>
    </body>
    </html>

     3.设置密码和请求密码:

    request.setAttribute("password", "123456");
    密码:<%=request.getAttribute("password") %>

    4.一些get:

    请求体的MIME类型:<%=request.getContentType() %><br>
        协议类型及版本号:<%=request.getProtocol() %><br>
        服务器主机名:<%=request.getServerName() %><br>
        服务器端口号:<%=request.getServerPort() %><br>
        请求文件长度:<%=request.getContentLength() %><br>
        请求客户端的IP地址:<%=request.getRemoteAddr() %><br>
        请求的真实路径:<%=request.getRealPath("request.jsp") %><br>
        请求的上下文路径:<%=request.getContextPath() %><br>

    运行结果:

     response

    1.response对象具有页面作用域,即访问一个页面时,该页面的response对象只能对这次访问有效,其他页面的response对当前页面无效

    2.response中的printWriter是提前与out,提前输出。

    <%@ page language="java" import="java.util.*,java.io.*" contentType="text/html; charset=UTF-8"%>
    <%
        response.setContentType("text/html,charset=UTF-8"); //设置响应MIME类型
         out.println("<h1>response</h1>");
         out.println("<hr>");
         
         PrintWriter pw = response.getWriter(); //获得输出流对象
         pw.println("printWriter");
    %>

    3.response.sendRedirect("reg.jsp");//请求重定向

    4.请求转发和请求重定向

     请求重定向:客户端的行为,response.sendRedirect(),从本质上讲是两次请求,前一次的请求对象不会被保存,地址栏的URL会改变

     请求转发:服务器的行为,request.getRequestDispatcher("request.jsp").forward(request, response);,是一次请求,转发后请求对象会保存,地址栏的URL不会改变

  • 相关阅读:
    Windows 和 Linux下使用socket下载网页页面内容(可设置接收/发送超时)的代码(用socket解释http,不错)
    HTTP协议 HttpWebRequest和 Socket的一点总结
    ASP.NET MVC基础学习
    利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
    C#中Hashtable容器的了解与使用
    lib 和 dll 的区别、生成以及使用详解
    将SQL获取的信息传递到Email中
    关于ref与out的区别
    C#中指针使用总结
    一个打包文件导入器
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/8524961.html
Copyright © 2020-2023  润新知