普通的Java Web項目中使用JSTL來簡化JSP, 以及使用<c:out> 標簽處理Cross-Site Scripting安全問題。
1. 下載JSTL必要的jar包
官方下載地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
下載 jakarta-taglibs-standard-1.1.2.zip 包並解壓,將 jakarta-taglibs-standard-1.1.2/lib/ 下的兩個 jar 文件:standard.jar 和 jstl.jar 文件拷貝到 /WEB-INF/lib/ 下,將 tld下的需要引入的 tld 文件復制到 WEB-INF/lib目錄下。
2. 引入<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
在需要使用JSTL的JSP文件最開始處加入
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3. 使用
Demo項目創建了2個頁面,index.jsp 和welcome.jsp,以及用於接受數據的mode ---User.java
三個文件的code:
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<FORM NAME="form" METHOD="post" ACTION="welcome.jsp">
<input type="text" name ="name" />
<input type="text" name="password" />
<input type="submit" value="submit"/>
</FORM>
</body>
</html>
index頁面通過提交按鈕將表單信息傳到welcome頁面。
welcome.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<jsp:useBean id="user" scope="request" class="test.bean.User"/>
<jsp:setProperty name="user" property="name"/>
<jsp:setProperty name="user" property="password"/>
<body>
<%
String nm = request.getParameter("name");
String pwd = request.getParameter("password");
%>
<%= request.getParameter("name")%>
<c:out value="${param['name']}"/>
</html>
welcome.jsp接收index頁面傳遞過來得方式有兩種
User.java
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
1、User bean接收
<jsp:useBean id="user" scope="request" class="test.bean.User"/>
<jsp:setProperty name="user" property="name"/>
<jsp:setProperty name="user" property="password"/>
property的值需要和index.jsp中輸入框的name值對應。
2、通過變量接收
String nm = request.getParameter("name");
String pwd = request.getParameter("password");
jsp中嵌套的java code,將request中的參數存到兩個變量中(nm, pwd)
那么如何將index頁面傳遞過來的參數顯示呢?
1、EL表達式
${xxx}
2、<c:out>標簽
直接輸出到頁面
<c:out value="${param['name'] }" />
將值填充到輸出框中
<input type="text" value="<c:out value="<%= nm%>"/>" />
<input type="text" value="<c:out value="${param['name']}"/>" />
但是為什么EL表達式中不直接用nm變量?因為${nm}拿到的值為null。
EL表達式中的內容必須在PageScope、RequestScope、SessionScope、ApplicationScope四個范圍之一,才能取到值。
而nm定義在Java中,屬於局部變量。
3、<jsp:getProperty>標簽
<jsp:getProperty name="user" property="name"/>
4、<%= 表達式%>
<%= nm%>
或者
<%= request.getParameter("name")%>
但是<%= 表達式%>和<c:out>這兩種輸出方式是有區別的,即:輸出內容中含html或者js標簽時,前者會影響html頁面格式或者執行js內容,后者會直接輸出,不會影響頁面和執行js
例如:
輸入<script>alert('hello');</script>
<%= request.getParameter("name")%>, welcome頁面會出現彈框
而<c:out value="${param['name']}">就不會,而是直接輸出內容,不執行js。
因此,JSTL的<c:out>標簽可以用來處理Cross-Site Scripting 安全問題。