zoukankan      html  css  js  c++  java
  • El和标准标签

    EL表达式针对于四大作用域:application,session,request,pagecontext(作用域由大倒小)${作用域获取内容的名字}
    是根据作用域最小的取,指定作用域${sessionscope.名字}
    从类中取值${User["name"]}或者${User.name}都可以,但是当类中的成员变量中存在特殊字符必须用第一种取值
    EL表达式类型转换,不需要自己转,可以直接运算${param.num+20}就是num加20;
    empty对于空字符串和空null的判断都为true
    标准标签库
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    如果scope属性不写,默认设置 在pagecontext中
    <c:set scope="session"></c:set>

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="jstl.jsp" method="post">
            请输入一个成绩:<input type="text" name="num"/>
                            <input type="submit" value="提交"/>
        
        </form>
        <c:choose>
            <c:when test="${param.num>60&&param.num<70}">
                <c:out value="及格了"></c:out>
            </c:when>
            <c:when test="${param.num>70&&param.num<80}">
                <c:out value="良好"></c:out>
            </c:when>
            <c:when test="${param.num<60}">
                <c:out value="不及格"></c:out>
            </c:when>
            <c:otherwise>
                <c:out value="很棒"></c:out>
            </c:otherwise>
        </c:choose>
        
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%@ page import="com.hanqi.Dal.User,java.util.ArrayList" %>
        <%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <style type="text/css">
    .aa {
        border: 1px red solid;
         80%;
        margin: 10px auto;
    }
    </style>
    </head>
    <body>
        <%
            session.setAttribute("text", "山东淄博");
            request.setAttribute("text", "淄博");
            
            User u=new User();
            u.setName("小明");
            u.setAge(20);
            session.setAttribute("User",u);
            
            
            ArrayList<String> list=new ArrayList<String>();
            list.add("山东");
            list.add("黑龙江");
            list.add("北京");
            list.add("河北");
            list.add("江苏");
            list.add("贵州");
            session.setAttribute("dis", list);session.setAttribute("teststring", "0533-1234567890-110");
            
            session.setAttribute("test", "你好, 我是session一行字");
            request.setAttribute("test", "你好, 我是request一行字");
            session.setAttribute("score", 100);
        %>
    <div class="aa">    
        <h2>EL表达式通过[]取属性</h2>
        ${User["name"]}<br>
        ${User["age"]}<br>
        <h2>EL表达式通过.取属性</h2>
        ${User.name}<br>
        ${User.age}
    </div>
    <div class="aa">
        <h2>EL表达式类型转换</h2>
        <form action="EL.jsp" method="post">
            <input type="text" name="num"/>
            <input type="submit" value="提交"/>
        </form>
        ${param.num }<br>
        num+20:${param.num+20 }<br>
        ${dis[2] }<br>
        ${paramValues.num[0] }<br>
        ${cookie.username.name }--->${cookie.username.value }<br>
        ${initParam.zhangsan }
    </div>
    <div class="aa">
        <h1>EL运算符</h1>
        ${7/2 }<br>
        ${5+3 }<br>
        ${7%2}<br>
        返回的值为布尔类型${5>2 }<br>
        返回的值为布尔类型${5<2 }<br>
    </div>
    <div class="aa">
        ${empty text }<br>
        <h1>empty对null和空字符串的判断</h1>
        empty对null的判断:${empty null }<br>
        empty对空字符串的判断:${empty "" }<br>
    </div>
    <div class="aa">
        <h1>c:forTokens标签</h1>
            <hr>
            <!-- items写接受的${name},delims相当于split,用什么字符分割,var别名 -->
            <c:forTokens items="${teststring}" delims="-" var="r">
                <c:out value="${r }"></c:out>
            </c:forTokens>
    </div>

    cookie

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
     <% 
        String username="";
        String password="";
        
        //获取cookie对象
        Cookie[] cs=request.getCookies();
        //如果cs不为空
        if(cs!=null){
            //遍历cs
            for(Cookie c:cs){
                //如果username在cookie对象中
                //看一下username是否在cookie中存取的键值对中存在名字叫username的,这里的username是一个字符串
                if("username".equals(c.getName())){
                    //将username对应的值赋给username
                    username=c.getValue();
                }
                //如果password在cookie对象中
                if("password".equals(c.getName())){
                    //将password对应的值赋给password
                    password=c.getValue();
                }
            }
        }
    
    %> 
    <form action="loginServlet" method="post">
        用户名:<input type="text" name="username"  value="<%=username%>"/>
          密码:<input type="text" name="password"  value="<%=password%>"/><br>
         记住密码:<input type="checkbox" name="rememberme" value="do"/><br>
         <input type="submit" value="登陆"/>
    </form>
    </body>
    </html>
    package com.hanqi.Servlet;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class loginServlet
     */
    @WebServlet("/loginServlet")
    public class loginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public loginServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //接受用户登陆是的用户名和密码
            String username=request.getParameter("username");
            String password=request.getParameter("password");
            String rememberme=request.getParameter("rememberme");
            
            System.out.println(username);
            System.out.println(password);
            System.out.println(rememberme);
            
            //判断用户名和密码不为空并且去掉空格长度大于0
            if(username!=null&&username.trim().length()>0&&password!=null&&password.trim().length()>0){
                //判断记住密码不为空并且长度去掉空格大于0
                if(rememberme!=null&&rememberme.trim().length()>0){
                    //获取cookie对象
                    Cookie[] cs=request.getCookies();
                    for(Cookie c:cs){
                        System.out.println(c.getName());
                    }
                    
                    //实例化cookie对象并将用户名和密码传入
                    Cookie c_username=new Cookie("username", username);
                    Cookie c_password=new Cookie("password",password);
                    
                    //设置cookie存取时间
                    c_username.setMaxAge(3600);
                    c_password.setMaxAge(3600);
                    
                    //添加cookie对象
                    response.addCookie(c_username);
                    response.addCookie(c_password);
                    System.out.println("cookie对象已经添加");
                }
                    response.sendRedirect("index.jsp");
            }else{
                response.sendRedirect("fail.jsp");
            }
            
            
            
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    JSTL(JSP Standard Tag Library)标准标签库:
    1, 核心标签(最常用, 最重要的)
    表达式控制标签
    out
    输出常量
    value---直接赋值
    输出变量
    default---默认值
    escapeXml---控制转义字符(默认为true, 如果需要转义, 设置为false)
    set
    三个属性: var value scope---将一个值存到*scope中(默认是pageContext)
    赋值的时候可以将值放在value后面, 也可以放在两个标签中间
    <c:set var="t" value="山东淄博" />
    <c:set var="t1">安徽黄山</c:set>
    target 对应javaBean的name值(target要使用EL表达式的形式)
    property 对应定义的类里面的属性
    remove----只能用于页面内的变量移除
    移除某个变量
    var: 指定需要remove的那个变量名
    scope: 两个scope中的变量名有重复的时候, 可以通过这个属性来确定移除的是哪个, 如果不指定, 则全部remove掉
    catch
    相当于try...catch...
    可以在这个标签中放其他标签, 只有一个属性var
    流程控制标签
    if
    <c:if test="EL表达式" var="变量名(指的是test属性的返回值:true或者false)" scope="作用域范围" ></c:if>
    choose---以下三个标签通常一起使用, 类似于switch
    when
    otherwise--可以不写
    循环控制标签
    forEach
    var: 设定一个变量值来存储从数组或者集合中遍历出来的值
    items: 指定需要遍历的数组或者集合
    begin, end: 指定需要遍历的起始位置
    step: 每次遍历跳过的个数, 默认值是1
    varStatus: 通过index(在原集合中的索引值), count(当前第几个), first(是否是第一个), last(是否是最后一个)来描述begin和end子集中的状态
    注意first和last, 是判断的是否在输出结果中的第一个或者最后一个, 并不是原来的集合中
    forTokens(回忆一下字符串的split()方法)
    输出的结果中同样也有index, count, first, last这四个属性

  • 相关阅读:
    sql处理数据库锁的存储过程
    SQL语句
    partial 函数
    map函数
    python命令行上下 退格,左右键不能用
    postgresql 在linux上的源码安装
    python字典操作
    根据key存不存在查询json
    精典博文
    python解析XML之ElementTree
  • 原文地址:https://www.cnblogs.com/NCL--/p/7446085.html
Copyright © 2011-2022 走看看