JSTL标签详解

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package com.xzit.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xzit.data.DataBase;

/**
 * Servlet implementation class ForeachServlet
 */
@WebServlet("/foreachServlet")
public class ForeachServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        request.getSession().setAttribute("cars", DataBase.getCarList());
//        response.sendRedirect("foreach.jsp");
        request.setAttribute("names", DataBase.getNames());
        request.getRequestDispatcher("fortokens.jsp").forward(request, response);
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" 
    import="com.xzit.domain.Student"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL foreach 标签</title>
    </head>
    
    <body>
        <c:if test="${sessionScope.cars ne null}">
        <table style="width:100%" border="1">
            <tr style="background-color:blue;color:#ffffff">
                <th>序号</th><th>产品名称</th><th>产品单价</th><th>产品颜色</th>
            </tr>
                <c:forEach items="${sessionScope.cars}" var="car" varStatus="sta">
                    <c:if test="${(sta.index+1) mod 2 eq 0}">
                        <tr style="background-color:pink">
                            <td>${sta.count}</td>
                            <td>${car.proName}</td>
                            <td>${car.price}</td>
                            <td>${car.color}</td>
                        </tr>
                    </c:if>
                    <c:if test="${(sta.index+1) mod 2 ne 0}">
                        <tr style="background-color:yellow">
                            <td>${sta.count}</td>
                            <td>${car.proName}</td>
                            <td>${car.price}</td>
                            <td>${car.color}</td>
                        </tr>
                    </c:if>
                </c:forEach>
        </c:if>
        </table>
    </body>
</html>
<%@ 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">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <%-- JSTL fortokens 标签用法 --%>
        <c:if test="${requestScope.names ne null}">
            <c:forTokens items="${requestScope.names}" delims="," varStatus="sta">
                第${sta.count}个人的名字是:${sta.current}<br>
                
            </c:forTokens>
        </c:if>
    </body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.xzit.domain.Student"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!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>JSTL 标签</title>
    </head>
    <%
        Student stu = new Student("向问天",36);
    %>
    <body>
        <%-- Set 标签的使用 --%>
        <c:set var="user" value="<h1 style='color:blue'>zidane</h1>" scope="page"></c:set>

        <%-- remove 标签移除变量 --%>
        <c:remove var="user" scope="page"/>
        
        <%-- Set 标签设置符合类型对象的某个属性 --%>
        
        学生的名字:<%=stu.getStuName()%><br>
        <c:set var="stuNewName" target="<%=stu %>" property="stuName" value="任我行"></c:set>
        学生的名字:<%=stu.getStuName()%><br>
        ${stuNewName}<br>
        <%-- out 标签显示作用域中的某个变量值 --%>
        <span style="color:red">
            ${pageScope.user}<br>
            <c:out value="${pageScope.user}" default="King" escapeXml="false"></c:out>
        </span><br>
        <a href="foreachServlet">foreach标签测试</a><br>
        <a href="redirect.jsp?location=main.jsp">测试redirect标签</a><br>
    </body>
</html>
<%@ 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">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL IF 标签</title>
    </head>
    <body>
        <%-- 使用url 标签设置一个url变量 --%>
        <c:url var="baidu" value="http://www.baidu.com" scope="session"></c:url>
    
        <%-- redirect 标签的使用 --%>
        <c:if test="${param.location ne null}">
            <c:redirect url="${param.location}">
                <c:param name="username" value="KING"></c:param>
            </c:redirect>
        </c:if>
        
        <%-- 使用redirect标签重定向到同一个服务器的其他应用程序 --%>
        <%-- 
        <c:redirect url="/login.jsp" context="/erpmms"></c:redirect>
        --%>
        
        <%-- 使用redirect标签重定向到外部url的其他网络平台 --%>
        <%-- 
        <c:redirect url="http://www.baidu.com"></c:redirect>
        --%>
    </body>
</html>
<%@ 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">
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL IF 标签</title>
    </head>
    <body>
        获取的参数username的值是:${param.username}
        <table style="width:100%">
            <tr style="height:100px">
                <td>&nbsp;</td>
                
            </tr>
            <tr>
                <td>
                    <%-- 
                    <c:import url="http://www.baidu.com" charEncoding="utf-8"></c:import>
                    --%>
                </td>
            </tr>
        </table>
        <br>
        <a href="${sessionScope.baidu}">百度</a>
    </body>
</html>
package com.xzit.data;

import java.util.ArrayList;
import java.util.List;

import com.xzit.domain.Car;

public class DataBase {

    private static String names;
    private static List<Car> carList = new ArrayList<Car>();
    
    
    static{
        
        names = "卫青,霍去病,戚继光,岳飞,项羽";
        carList.add(new Car("雷克萨斯",67,"银灰"));
        carList.add(new Car("布加迪威龙",205,"蓝色"));
        carList.add(new Car("法拉利",150,"红色"));
        carList.add(new Car("陆虎揽胜",160,"白色"));
        carList.add(new Car("拉博基尼蝙蝠",205,"黄色"));
        carList.add(new Car("奥迪幻影",99,"黑色"));
    }
    public static String getNames() {
        return names;
    }
    
    public static List<Car> getCarList() {
        return carList;
    }
    
    
}
package com.xzit.domain;

public class Car {

    private String proName;
    private double price;
    private String color;
    
    public String getProName() {
        return proName;
    }
    public void setProName(String proName) {
        this.proName = proName;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    
    public Car() {
        
    }

    public Car(String proName, double price, String color) {
        
        this.proName = proName;
        this.price = price;
        this.color = color;
    }
}

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" session="true"%>
    
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
    
<!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="UsersServlet" method="post">
    <%-- --%>    
        <c:choose>
            <c:when test="${cookie['logininfo'] ne null}">
                    用户名称:<input name="username" value="${cookie['logininfo'].value}"><br>
                    登录密码:<input name="password" type="password" value="${cookie['logininfo2'].value}">
            </c:when>
            <c:otherwise>
                用户名称:<input name="username"><br>
                登录密码:<input name="password" type="password">
            </c:otherwise>
        </c:choose>
        <input type="checkbox" name="savepwd" />保存密码
        <br>
        <input name="login" value="登录" type="submit">
    </form>
    <%
        String mess = request.getParameter("loginfaild");
        if(mess != null && mess.equals("error")){
            
        %>
            <label style="color:red">系统提示:用户名不存在或密码错误,登录失败</label>
    <%    }
    %>
</body>
</html>
package com.xzit.erpmodel.users.controller;

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;
import javax.servlet.http.HttpSession;

import com.xzit.erpmodel.users.domain.SysUsers;
import com.xzit.erpmodel.users.service.UsersService;
import com.xzit.erpmodel.users.service.UsersServiceImp;

/**
 * Servlet implementation class UsersServlet
 */
@WebServlet("/UsersServlet")
public class UsersServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        /* 获取用户提交的登录信息 */
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String savepwd = request.getParameter("savepwd");
        
        SysUsers user = new SysUsers();
        user.setName(name);
        user.setPassword(pwd);
        
        UsersService service = new UsersServiceImp();
        SysUsers current = service.validateLogin(user);
        
        if(current !=null) {//用户登录成功
            
                
                Cookie[] cookies = request.getCookies();//获取用户未过期的所有Cookie
                if(cookies != null && cookies.length !=0) {//有Cookie
                    int count = 0;    //计数器
                    for(int i=0;i<cookies.length;i++) {//遍历数组查找目标Cookie
                        
                        if(cookies[i].getName().equals("logininfo") || cookies[i].getName().equals("logininfo2")) {//如果找到了目标有效的Cookie
                            
                            if(savepwd == null) {
                                cookies[i].setMaxAge(0);//不保存Cookie
                                response.addCookie(cookies[i]);
                            }
                            count++;
                        }
                    }
                    if(count == 0) {//没有找到Cookie
                        if(savepwd != null && savepwd.equals("on")) {//用户欲保存密码及用户信息
                            Cookie newCookie = new Cookie("logininfo",name);
                            Cookie newCookie2 = new Cookie("logininfo2",pwd);
                            newCookie.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                            newCookie2.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                            response.addCookie(newCookie);
                            response.addCookie(newCookie2);
                        }
                    }
                }else {
                    if(savepwd != null && savepwd.equals("on")) {//用户欲保存密码及用户信息
                        Cookie newCookie = new Cookie("logininfo",name);
                        Cookie newCookie2 = new Cookie("logininfo2",pwd);
                        newCookie.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                        newCookie2.setMaxAge(3600*24*7);//设置Cookie的生命周期为7天
                        response.addCookie(newCookie);
                        response.addCookie(newCookie2);
                    }
                }
            
            //将用户对象存储到会话域中
            HttpSession sess = request.getSession();//如果有Session直接返回,没有则创建新的Session对象
            sess.setAttribute("currentUser", current);
            response.sendRedirect("worker.jsp");
        }else {
            response.sendRedirect("login.jsp?loginfaild=error");//登录失败跳转到登录页面
        }
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" 
    import="com.xzit.domain.Student"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSTL foreach 标签</title>
    </head>
    
    <body>
        <c:if test="${sessionScope.cars ne null}">
        <table style="width:100%" border="1">
            <tr style="background-color:blue;color:#ffffff">
                <th>序号</th><th>产品名称</th><th>产品单价</th><th>产品颜色</th>
            </tr>
                <c:forEach items="${sessionScope.cars}" var="car" varStatus="sta">
                    <c:if test="${(sta.index+1) mod 2 eq 0}">
                        <tr style="background-color:pink">
                            <td>${sta.count}</td>
                            <td>${car.proName}</td>
                            <td>${car.price}</td>
                            <td>${car.color}</td>
                        </tr>
                    </c:if>
                    <c:if test="${(sta.index+1) mod 2 ne 0}">
                        <tr style="background-color:yellow">
                            <td>${sta.count}</td>
                            <td>${car.proName}</td>
                            <td>${car.price}</td>
                            <td>${car.color}</td>
                        </tr>
                    </c:if>
                </c:forEach>
        </c:if>
        </table>
    </body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>erpmms</display-name>
  
  <!-- 在当前web应用程序配置文件web.xml中设置Session过期时间(分钟) -->
  <session-config>
        <session-timeout>1</session-timeout>
  </session-config>
  
  <listener>
      <listener-class>com.xzit.erpmodel.listeners.HttpSessionListenerImp</listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.xzit.erpmodel.users.domain.*"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!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>企业资源计划管理系统</title>
        <link type="text/css" rel="stylesheet" href="css/base.css" />
    </head>
    <%-- 判断session中是否有当前用户对象 --%>
        <c:if test="${sessionScope.currentUser eq null}">
            <c:redirect url="login.jsp"></c:redirect>
        </c:if> 
    
    <frameset rows="145px,*" border="0" style="margin-left:100px;margin-top:0px">
        <frame src="top.jsp" scrolling="no" style="margin-left:500px"/>
        <frameset rows="85%,*" border="0" bordercolor="#fff" style="margin-left:500px;margin-top:20px">
            <frameset cols="18%,*" border="0" style="background-color:skyblue" >            
                <frame src="left.jsp" scrolling="no" style="margin-left:0px;margin-top:10px"/>
                <frame src="welcome.html" scrolling="no" style="margin-left:0px"/>
            </frameset>
            <frame src="foot.jsp" scrolling="no" 
                style="margin-left:20px;background-color:#eeeeff;border:0;border-color: #fff"
                 />
        </frameset>
        
    </frameset>
</html>

 

posted @ 2022-02-26 23:19  伊万  阅读(178)  评论(0编辑  收藏  举报