zoukankan      html  css  js  c++  java
  • 第2章 JSP数据交互(一)

    1.JSP内置对象:JSP内置对象是 Web 容器创建的一组对象,不用通过手动new就可以使用
    2.JSP9大内置对象:
    对象名称 类型 全路径
    request
    response
    out
    session
    application
    page
    pageContext
    config
    execption
    3.JSP内置对象:out
    <%
    int[] value = { 60, 70, 80 };
    for (int i : value) {
    //将输出信息输出到控制台
    System.out.println("控制台:"+i);
    //out作为JSP最简单的内置对象,主要用于将信息输出到页面上
    out.println(i);

    }
    %>
    4.JSP内置对象:request:客户端向服务器端发送请求数据,我们通过request对象接收数据
    <%
    //获取数据之前解决乱码 解决表单POST提交方式的乱码
    request.setCharacterEncoding("UTF-8");

    //request用于获取客户端向服务器提交的数据
    String username=request.getParameter("username");
    String password=request.getParameter("password");

    //获取表单组件对应多个值时的请求数据
    String [] hobbys=request.getParameterValues("hobby");
    for(int i=0;i<hobbys.length;i++){
    //获取数据之前解决乱码 解决表单Get提交方式的乱码
    /* hobbys[i]=new String(hobbys[i].getBytes("ISO-8859-1"),"UTF-8"); */
    out.print(hobbys[i]);
    }
    out.println();
    out.println(username+" "+password);

    //性别
    String sex=request.getParameter("sex");
    out.println(sex);

    //下拉框地址
    String address=request.getParameter("address");
    out.println(address);
    %>

    POST和GET的区别:
    1.从安全角度考虑 post更安全一些 get不安全
    2.post方式提交数据可以提交大概几GB的数据 get方式提交数据最大也就到几KB


    5.解决乱码的方案:
    post乱码解决:request.setCharacterEncoding("UTF-8");
    get乱码解决: hobbys[i]=new String(hobbys[i].getBytes("ISO-8859-1"),"UTF-8");

    6.HTTP状态码:
    1** 信息,服务器收到请求,需要请求者继续执行操作
    2** 成功,操作被成功接收并处理
    3** 重定向,需要进一步的操作以完成请求
    4** 客户端错误,请求包含语法错误或无法完成请求
    5** 服务器错误,服务器在处理请求的过程中发生了错误

    404和500错误状态码配置页面:
    在当前项目的web.xml当中加入如下配置信息:
    <error-page>
    <error-code>500</error-code>
    <location>/500.jsp</location>
    </error-page>
    <error-page>
    <error-code>404</error-code>
    <location>/404.jsp</location>
    </error-page>
    7.response:响应
    转发(是在服务器内部进行,无法访问到除内部以外的资源):request.getRequestDispatcher("/response/welcome.jsp").forward(request, response);
    重定向(全路径):response.sendRedirect("/Chap02/response/Login.jsp");


    区别:1.转发是在服务器内部进行,重定向是客户端完成的,需要写入全路径,地址xxxxxxx
    2.转发请求1次,其余的操作都是在服务器内部进行的;重定向请求至少2次,其余的请求命令客户端再次请求一个URL
    3.转发可以携带这次请求的数据,重定向不带数据
    8.session会话对象:
    会话:在一段时间内,客户端和服务器建立连接的过程,只要会话时间不过期,只要会话不关闭,那么会话就一直存在,那么会话中保存的数据就一直存在
    不同浏览器的会话是不相等的:
    可以设置Session的会话时长:
    1.session.setMaxInactiveInterval(秒数)
    2.在项目web.xml当中去设置Session的实效时长:
    <session-config>
    <session-timeout>1</session-timeout>
    </session-config>
    手动设置Session实效:
    session.invalidate();

    会话保存数据:
    session.setAttribute(String name, Object value);
    Object value=session.getAttribute(String name);
    删除会话当中保存的数据:
    session.removeAttribute(String name);




    9.incloud
    <%@ include file="loginControl.jsp" %>

  • 相关阅读:
    CodeForces 706C Hard problem
    CodeForces 706A Beru-taxi
    CodeForces 706B Interesting drink
    CodeForces 706E Working routine
    CodeForces 706D Vasiliy's Multiset
    CodeForces 703B Mishka and trip
    CodeForces 703C Chris and Road
    POJ 1835 宇航员
    HDU 4907 Task schedule
    HDU 4911 Inversion
  • 原文地址:https://www.cnblogs.com/Chencheno/p/11136862.html
Copyright © 2011-2022 走看看