JSP-基本语法

JSP注释

<%--注释--%>

JSP表达式

用来将程序的输出,输出到客户端

<%=变量或者表达式%>

JSP脚本片段

<%
    for (int i = 0; i < 10; i++) {
%>
<p>中间 <%=i%></p>
<%
    }
%>
<%
    out.print("脚本片段");
%>

JSP声明

会被编译到JSP生成Java的类中!其他的,就会被生成到_jspService方法中

<%!
    private int i = 10;
    public void sayHello() {
        // 不能调用out,不在同一个方法里
        System.out.println(i);
    }
%>
<%
    sayHello();
%>

JSP指令

错误页面

指定页面

<%@ page errorPage="/error/500.jsp" %>

<%@ page isErrorPage="true" %>

指定代码

修改了web.xml就要重启tomcat

<!--错误代码定向页面-->
<error-page>
    <error-code>500</error-code>
    <location>/error/500.jsp</location>
</error-page>

导入其他页面

第一种

<body>
<%@ include file="/common/header.jsp"%>
<p>主体</p>
<%@ include file="/common/fotter.jsp"%>
</body>

第二种

作为第三方导入,变量命名不会冲突

<body>
<jsp:include page="/common/header.jsp"/>
<p>主体</p>
</body>

转发

转发页

<body>
<%
    String URLEncodeStr1 = URLEncoder.encode("张三", "utf-8");
    String URLEncodeStr2 = URLEncoder.encode("李四", "utf-8");
%>
<jsp:forward page="/test.jsp">
    <jsp:param name="name1" value="<%=URLEncodeStr1%>"/>
    <jsp:param name="name2" value="<%=URLEncodeStr2%>"/>
</jsp:forward>
</body>

接受页

<body>
<%
    String URLEncodeStr1 = URLDecoder.decode(request.getParameter("name1"), "utf-8");
    String URLEncodeStr2 = URLDecoder.decode(request.getParameter("name2"), "utf-8");
%>
<h1><%=URLEncodeStr1%></h1>
<h1><%=URLEncodeStr2%></h1>
</body>

创建对象设置属性

<body>
<jsp:useBean id="zhangsan" scope="page" class="com.demo.entity.Student" />
<jsp:setProperty name="zhangsan" property="id" value="1" />
<jsp:setProperty name="zhangsan" property="name" value="张三" />
<jsp:setProperty name="zhangsan" property="age" value="15" />

id:<jsp:getProperty name="zhangsan" property="id"/> <br/>
姓名:<jsp:getProperty name="zhangsan" property="name"/><br/>
年龄:<jsp:getProperty name="zhangsan" property="age"/><br/>
</body>

EL表达式

获取值,跟<%=%>差不多,好处在于可以省略getXXX获取值

<body>
<%
    application.setAttribute("name", "张三");
    Student student = new Student(1, "张三", 15);
    pageContext.setAttribute("student", student);
%>
<h1>${pageScope.get("name")}</h1>
<h1>${requestScope.get("name")}</h1>
<h1>${sessionScope.get("name")}</h1>
<h1>${applicationScope.get("name")}</h1> <%--只能同级范围内取到值--%>

ID:${student.id} <br/>
姓名:${student.name} <br/>
年龄:${student.age} <br/>
</body>

与JSP表达式的区别

一个是直接获取,一个从pageContext里拿:

JSTL表达式

核心标签库

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<c:out>

用来显示一个表达式的结果,与<%= %>作用相似,它们的区别就是<c:out>标签可以直接通过"."操作符来访问属性

<body>
<%
    String name = "张三";
%>
<c:out value="name" default="默认值"/> <br/>
<c:out value="<%=name%>" default="默认值"/> <br/>
<c:out value="${null}" default="默认值"/> <%--值为null时取默认值--%>
</body>

<c:set>

设置变量值和对象属性

<body>
<c:set var="name" value="张三" scope="page"/>
<c:out value="${pageScope.name}" default="默认值"/>
</body>

<c:remove>

用于移除一个变量,可以指定这个变量的作用域,若未指定,则默认为变量第一次出现的作用域

<body>
<c:set var="name" value="张三" scope="page"/>
<c:out value="${pageScope.name}" default="默认值"/> <br/>
<c:remove var="name" scope="page"/>
<c:out value="${pageScope.name}" default="默认值"/>
</body>

<c:catch>

主要用来处理产生错误的异常状况,并且将错误信息储存起来

<body>
<c:catch var ="catchException">
    <% int x = 5/0;%>
</c:catch>
<c:if test = "${catchException != null}">
    异常为 : ${catchException} <br/>
    发生了异常: ${catchException.message}
</c:if>
</body>

<c:if>

<body>
<c:set var="salary" scope="page" value="${2000*2}"/>
<c:if test="${salary > 2000}">
    <p>我的工资为: <c:out value="${salary}"/><p>
</c:if>
</body>

<c:chose>

if-elseif-else 类似

<body>
<c:set var="score" value="79" scope="page"/>
<c:choose>
    <c:when test="${score>90}">
        <h1>A</h1>
    </c:when>
    <c:when test="${score>80}">
        <h1>B</h1>
    </c:when>
    <c:when test="${score>70}">
        <h1>C</h1>
    </c:when>
    <c:otherwise>
        <h1>D</h1>
    </c:otherwise>
</c:choose>
</body>

<c:when>

<c:choose>的子标签,用来判断条件是否成立

<c:otherwise>

<c:choose>的子标签,接在<c:when>标签后,当<c:when>标签都判断为false时被执行

<c:forEach>

<c:forEach var="i" begin="1" end="5" step="2">
    Item <c:out value="${i}"/>
</c:forEach>
<%
    int[] nums = {1, 2, 3, 4};
    pageContext.setAttribute("nums", nums);
%>
<c:forEach items="${nums}" var="item" varStatus="status">
    <p>num <c:out value="${item}"/> <c:out value="${status.index+1}"/></p>
</c:forEach>

原文地址:https://www.cnblogs.com/shenleg/p/14259935.html