zoukankan      html  css  js  c++  java
  • JavaWeb -jsp文件和内置对象的解析

     jsp文件和内置对象的解析

    对page解析

     

    JSP九大内置对象(自带,无需new)

    1 out:输出对象

    2 request:请求对象,存储“客户端像服务端发送的请求信息”

    3 response:响应对象

    4 pageContext

    5 session

    6 application  全局对象

    7 config

    8 page

    9 exception

    两种提交文件方式(推荐post)

      get 提交方式:method=“get”,地址栏、超链接(<a href="xx">)请求方式 默认都为get

    1.在地址栏显示请求信息(但容量有限 只有4-5KB)

    2.出现乱码时:修改server.xml 更改tomcat的get编码 在post端口后加URIEncoding=“utf-8”

      post 提交方式:文件上传操作必须是此方式

    1 出现乱码时:  request.setCharacterEncoding("utf-8");

    =====================================================================================

    request的常见方法:

      1 request.getParameter(String str);      按str作为key返回对应value

      2 request.getParameterValues(String str);  按str作为key返回对应多个value 用数组存储(例如checkbox按钮)

         3  request.setCharacterEncoding("utf-8");  设置请求编码

      4 request.getRequestDispatcher("b.jsp").forward(request,response); 请求转发  a-》b(页面跳转的一种方式)

      5 request.getServerContext();            获取项目的ServletContext对象

    以下是对上述方法的综合示例(实现注册与显示页面)

    注册页面

    <%@ 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>
        <form action="show.jsp">
            用户名:<input type="text" name="uname"/><br/>
            密码:   <input type="password" name="upwd"/><br/>
            年龄:   <input type="text" name="uage"/><br/>
            爱好: <br/>
            <input type="checkbox" name="uhobbies" value="足球"/>足球    
            <input type="checkbox" name="uhobbies" value="篮球"/>篮球
            <input type="checkbox" name="uhobbies" value="乒乓球"/>乒乓球<br/>
            <input type="submit" value="注册">
        </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>
        <%
            request.setCharacterEncoding("utf-8");
            String name=request.getParameter("uname");
            int age=Integer.parseInt( request.getParameter("uage"));
            String pwd=request.getParameter("upwd");    
            String[] hobbies=request.getParameterValues("unhobbies");
        %>
        register complete!info as follow:
        姓名:<%=name %><br/>
        年龄:<%=age %><br/>
        爱好:<br/>
        <%
            if(hobbies!=null){
                for(String hobby:hobbies)
                    out.print(hobby);
            }
        %>
    </body>
    </html>

     response的常见方法:

    1 response.addCookie(Cookie cookie)服务端向客户端增加Cookie对象

    2 response.sendRedirect(String location)  重定向 (页面跳转的一种方式)

    3 response.setContentType(String type):设置服务端响应编码(设置服务端的ContentType类型)

     =====================================================================================================

    两种页面跳转的方式

    1.response.sendRedirect(String location)               重定向

    2.request.getRequestDispatcher("b.jsp").forward(request,response);  请求转发

    区别示意图:

  • 相关阅读:
    Cookie 干货
    element-ui 框架中使用 NavMenu 导航菜单组件时,点击一个子菜单会出现多个子菜单同时展开或折叠?
    数组遍历的方法
    前端网页字体
    样式小收藏:完成、错误、提示动态图标样式
    多语言网站利器 rel="alternate" hreflang="x"
    网页中文章显示一部分,然后“查看全文”
    仿水滴筹中快捷留言祝福、随机生成祝福
    TypeScript知识点
    前端项目经验
  • 原文地址:https://www.cnblogs.com/cc123nice/p/10691961.html
Copyright © 2011-2022 走看看