标签:direct 直接 raft 字符数组 stack set 相同 逻辑运算符 efault
<%-- 分别往request域,session域,servletContext域和pageContext中存值 --%> <% request.setAttribute("requestName","requestScope中的值"); request.getSession().setAttribute("sessionName","sessionScope中的值"); request.getServletContext().setAttribute("servletContextName", "servletContext中的值"); pageContext.setAttribute("pageName","page域中的值"); %> <%-- 使用EL表达式从上述3个域中取值 --%> 从request域中取值:${requestScope.requestName}<br> 从session域中取值:${sessionScope.sessionName}<br> 从servletContext域中取值:${applicationScope.servletContextName}<br> 从page域中取值:${pageScope.pageName}
${cookie.JSESSIONID} <%--根据cookie的名字获取cookie对象 --%> ${cookie.JSESSIONID.name} <%--获得cookie的名称,对应方法getName()--%> ${cookie.JSESSIONID.value} <%--获得cookie的值,对应方法getValue() --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% pageContext.setAttribute("n1", "10"); pageContext.setAttribute("n2", "20"); %> 加法运算:${n1+n2}<br> 减法运算:${n1-n2}<br> 乘法运算:${n1*n2}<br> 除法运算:${n1/n2}或${n1 div n2}<br> 取余运算:${n1%n2}或${n1 mod n2}<br> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% pageContext.setAttribute("n1", "10"); pageContext.setAttribute("n2", "20"); %> ==:${n1 == n2}或 ${n1 eq n2}<br> !=:${n1 != n2}或 ${n1 ne n2}<br> >:${n1 > n2}或 ${n1 gt n2}<br> >=:${n1 >= n2}或 ${n1 ge n2}<br> <:${n1 < n2}或 ${n1 lt n2}<br> <= :${n1 <= n2}或 ${n1 le n2}<br> </body> </html>
${true && false}
${true || false}
${!false}
${n1 >0 ?"正数":"负数"}
<%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page import="com.heima.domain.Student" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String str = ""; List list = new ArrayList(); Student student = null; request.setAttribute("str", str); request.setAttribute("list", list); request.setAttribute("student",student); %> 空字符串:${empty str}<br> 集合size=0:${empty list}<br> 空对象null:${empty student}<br> </body> </html>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out value="hello world"></c:out>
<body> <h1>首页 -- JSP页面</h1> <%-- 判断session中的loginUser是否为空: 1.如果不为空,获取session中的信息,并将用户名显示在页面上; 2.如果为空,则提示用户登录; --%> <c:if test="${not empty loginUser}"> <h3>欢迎您!${loginUser.userName}</h3> </c:if> <c:if test="${empty loginUser}"> <h3>对不起,请先<a href="login.html">登录</a></h3> </c:if> </body>
<%-- 需求一:遍历普通数组 --%> <% String[] strArr = new String[5]; strArr[0] = "hello"; strArr[1] = "world"; strArr[2] = "你好"; strArr[3] = "世界"; strArr[4] = "世界你好"; request.setAttribute("strArr",strArr); %> <c:forEach items="${strArr}" var="str"> ${str} <br> </c:forEach>
<%-- 需求二:遍历Student的list集合 --%> <% List<Student> stuList = new ArrayList<>(); Student stu1 = new Student(1, "张三1", "123"); Student stu2 = new Student(2, "张三2", "123"); Student stu3 = new Student(3, "张三3", "123"); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); request.setAttribute("stus",stuList); %> <c:forEach items="${stus}" var="stu"> ${stu.name} <br> </c:forEach>
<% Map<String, String> map = new HashMap<>(); map.put("张三", "123"); map.put("李四", "123"); map.put("王五", "123"); request.setAttribute("maps",map); %> <c:forEach items="${maps}" var="stuMap"> ${stuMap.key} = ${stuMap.value}<br> </c:forEach>
<% request.setAttribute("num", 4); request.setAttribute("flag", 1); %> <!‐‐ c:choose 表示那些when 和 otherwise是一组的 ‐‐> <c:choose> <c:when test="${num == 1 }">星期一</c:when> <c:when test="${num == 2 }">星期二</c:when> <c:when test="${num == 3 }">星期三</c:when> <c:when test="${num == 4 }">星期四</c:when> <c:when test="${num == 5 }">星期五</c:when> <c:when test="${num == 6 }">星期六</c:when> <c:when test="${num == 7 }">星期日</c:when> <c:otherwise>参数不合法</c:otherwise> </c:choose> <c:choose> <c:when test="${flag == 1 }">白天</c:when> <c:when test="${flag == 2 }">黑夜</c:when> <c:otherwise>参数不合法</c:otherwise> </c:choose>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UserService userService = new UserServiceImpl(); List<User> userList = userService.queryAll(); request.setAttribute("userList",userList); request.getRequestDispatcher("/list.jsp").forward(request,response); }
<script> function deleteUser(id){ var flag = window.confirm("确定删除"); if(flag){ location.href = "deleteUserServlet?id=" + id; } } </script> <script> function queryUserUp(id){ location.href = "queryUserById?id=" + id; } </script> <c:forEach items="${userList}" var="user"> <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.sex}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td><a class="btn btn-default btn-sm" href="javascript:void(0);" onclick="queryUserUp(${user.id})">修改</a> <a class="btn btn-default btn-sm" href="javascript:void(0);" onclick="deleteUser(${user.id})">删除</a></td> </tr> </c:forEach>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Map<String, String[]> properties = request.getParameterMap(); User user = new User(); try { BeanUtils.populate(user,properties); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserService service = new UserServiceImpl(); boolean result = service.queryUserByName(user); if(result){ response.sendRedirect("/tiaozhuan.html"); }else{ request.setAttribute("msg","重名了,注册失败"); request.getRequestDispatcher("add.jsp").forward(request,response); } }
<div><%=request.getAttribute("msg")==null?"":request.getAttribute("msg")%></div>
标签:direct 直接 raft 字符数组 stack set 相同 逻辑运算符 efault
原文地址:https://www.cnblogs.com/cyxy31521/p/9417010.html