zoukankan      html  css  js  c++  java
  • J2EE基础

    ### 1.获取真实路径

    String path = this.getServletContext().getRealPath("/index.jsp");
    ### 2.获得初始化参数

    String params = this.getServletContext().getInitParameter("param-key");
    ### 3.获得资源路路径后再创建输入流对象

    InputSream input = this.getServletContext().getResourceAsStream("index.jsp");
    ### 4.获得文件夹下所有路径

    Set<String> paths = this.getServletContext().getResourcePaths("/WEB-INF");
    ### 5.两个响应流

    ServletOutputStream 与 PrintWriter两个响应流不能同时使用
    ServletOutputStream = response.getOutputStream();
    ### 6.http://localhost:8080/web/Aservlet?username=xxx&password=ooo

    String getSchema(): 获取协议,http
    String getServerName(): 获取服务器名,localhost
    String getContextPath(): 获取项目名, /web
    String getServerPort(): 获取服务器端口,8080
    String getServletPath():获取Servlet路径,/Aservlet
    String getQueryString():获取参数部分,即问号后面的部分 username=xxx&password=ooo
    String getRuquestURI():获取请求URI,等于项目名+Servlet路径 /web/Aservlet
    String getRequestURL(): 获取请求URL,等于不包含参数的整个请求路径 http://localhost:8080/web/Aservlet
    ### 7.请求头设置

    ```java
    response.setHeader("Cache-Control","no-cache");
    response.setHeader("pragma","no-cache");
    response.setDateHeader("expires",-1);//立即过期 不缓存
    ```

    ### 8.解决乱码(tomcat8默认为utf-8编码)

    服务器发送内容编码默认为ISO,发送内容时必须设置头

    ~~~~~~java
    response.setContentType("text/html;charset=utf-8");
    ~~~~~~

    客户端post请求的参数编码与页面编码一致,服务器段默认使用ISO解码此时,会产生乱码,此时服务器端在获取参数之前需设置

    ~~~~java
    request.setCharacterEncoding("utf-8");
    request.getParamater("name");//获取参数
    ~~~~

    客户端地址栏请求都是GBK编码 tomcat默认使用ISO解码肯定乱码

    超链接get请求时编码与页面编码一致,需要先反编码回来再使用utf-8解码

    ~~~~java
    String name = request.getParamater("name");
    byte[] bytes = name.getBytes("ISO-8859-1");
    name = new String(bytes,"utf-8");
    ~~~~

    tomcat 9 中服务器端已经默认使用 urf-8来解码 不再需要反编

    严格来讲 地址栏和超链接中都必须使用url编码不能使用中文字符

    ~~~~~java
    //get请求 无论是地址栏还是超链接 都是utf-8编码 无需处理
    // post表单请求 只需要设置请求头
    request.setCharacterEncoding("utf-8");
    ~~~~~

    ### 9.路径问题

    + 转发和包含路径

    + 以 ”/“开头 相对当前项目路径
    + 不以”/“开头 相对当前Servlet路径

    + 重定向路径

    + 以 ”/“开头 相对当前主机

    + 页面中超链接和表单路径

    + 与重定向相同,都是客户端路径!需要添加项目名

    + 不以”/“开头,相对于当前页面路径

    ##### 建议都已”/“开头

    + ServletContext 获取资源路径

    + 相对于当前项目路径

    + ClassLoader获取资源路径

    + 相对Classes的路径

    + Class获取资源路径

    + 以”/“开头相对于classes目录
    + 不以”/“开头相对于当前.class文件所在目录


    #####10.页面表单默认传递空字符串 “” servlet要用双重判断 != null && != ""

    ##### getAttribute()没定义返回null值 用 !=null 判断即可

    ###11.URL重写

    ~~~~~java
    out.println(response.encoder("/project/Servlet")
    ~~~~~

    在所有该页面上的子路径后面带上JSSESSIONID参数传送给服务器

    ### 12.超链接中项目名

    ~~~~~java
    ${pageContext.request.contextPath}
    ~~~~~

    ### 13.EL表达式11个内置对象,有十个Map

    pageScope、requestScope、sessionScope、applicationScope、header、headerValues、param、paramValues、initParam、cookie、pageContext,其中只有pageContext不是Map

    ~~~~java
    Map<String, Cookie> cookie;
    ${cookie.JSESSIONID.value}
    cookie.JSESSIONID 获得Cookie对象 cookie.JESSIONID.value获得value
    Map键为Cookie对象key,值为Cookie对象
    ~~~~

    ### 14.EL函数库

    ~~~~java
    ${fn:join(arr,";")} 输出a;b;c

    ${fn:substring("0123456789"),5,-1} 输出56789

    ${fn:substringAfter("hello-world","-")} 输出world

    ${fn:substringBefore("hello-world"),"-"} 输出 hello

    ${fn:escapeXml("<html></html>")} 输出 <html></html>

    ~~~~

    ### 15.jsp标签库 fmt格式化标签 主要用来格式化日期和数字

    ~~~~~java
    <fmt:formatDate value="${requestScope.date}" pattern="yyyy-MM-dd HH:mm:ss"/>
    <fmt:formatNumber value="${requestScope.num1}" pattern="0.00"/> 输出 5.9 ——> 5.90 5.96 ——> 6.00
    <fmt:formatNumber value="${requestScope.num2}" pattern="#.##"> 输出 5.9 ——> 5.9 5.96 ——> 6.00
    ~~~~~

  • 相关阅读:
    leetcode-5-Longest Palindromic Substring
    leetcode-3-Longest Substring Without Repeating Characters
    leetcode-2-Add Two Numbers
    正则匹配-题型总结
    Kmp简单运用
    最长回文子串
    树-leetcode做题笔记
    java对象与类
    【LeetCode & 剑指offer刷题】查找与排序题13:Merge Intervals
    【LeetCode & 剑指offer刷题】查找与排序题12:Top K Frequent Elements
  • 原文地址:https://www.cnblogs.com/agasha/p/11302572.html
Copyright © 2011-2022 走看看