JSP复习小结

花了点时间整理了JSP

  1 package com.lwx;
2 /**
3 JABA BEAN
4 */
5 public class UserBean {
6
7 private String username;
8 private int userid;
9 private String sex;
10
11 public UserBean() {
12 }
13
14 public String getUsername() {
15 return username;
16 }
17
18 public void setUsername(String username) {
19 this.username = username;
20 }
21
22 public int getUserid() {
23 return userid;
24 }
25
26 public void setUserid(int userid) {
27 this.userid = userid;
28 }
29
30 public String getSex() {
31 return sex;
32 }
33
34 public void setSex(String sex) {
35 this.sex = sex;
36 }
37 }
38
39 <%
40 UserBean userBean = new UserBean();
41 userBean.setUserid(1);
42 userBean.setUsername("lwx");
43
44 pageContext.setAttribute("userBean", userBean);
45 %>
46 <%
47 List list = new ArrayList();
48
49 for (int i = 0; i < 5; i++) {
50 UserBean bean = new UserBean();
51
52 bean.setSex(i / 2 == 0 ? "" : "");
53 bean.setUserid(i);
54 bean.setUsername("lwx" + i);
55 list.add(bean);
56 }
57 pageContext.setAttribute("userlist", list);
58 %>
59 <body>
60 <h1>
61 脚本元素
62 </h1>
63 <h2>
64 一、注释元素
65 </h2>
66 <!-- 这是客户端注释元素<%=new Date()%> -->
67 <%-- 这是程序员注释<%=new Date12333() %> --%>
68 <h2>
69 二、指令元素
70 </h2>
71 <p>
72 指令元素包括page,include,taglib
73 </p>
74 <p>
75 page定义的属性有language,pageEncoding import isELIgnored isErrorPage=true
76 才可以在页面使用exception对象 errorPage
77 </p>
78 <p>
79 inlclude包含其他页面file属性,不可以包含servlet
80 </p>
81 <p>
82 taglib比如taglib uri="url" prefix="简写"
83 </p>
84 <h2>
85 三、脚本元素
86 </h2>
87 <p>
88 脚本元素包括代码段,表达式=(无分号),申明(!全局)
89 </p>
90 <p>
91 九大内置对象page,request,response,session,application,config,exception,out,pageContent(findAttribute)
92 </p>
93 <h2>
94 四、动作元素
95 </h2>
96 <p>
97 动作元素jsp:include forward param userBean setProperty getProperty plugin
98 </p>
99
100 <jsp:useBean id="user" class="com.lwx.UserBean">
101 <jsp:setProperty name="user" property="username" value="lwx" />
102 <%--jsp:setProperty name="user" property ="userid" param="id"/>
103 <jsp:param name="id" value="1" / --%>
104 </jsp:useBean>
105 用户名:<jsp:getProperty name="user" property="username" /><br>
106 用户编号:<jsp:getProperty name="userBean" property="userid" /><!-- 在pageContext中查找name为userBean的对象 -->
107 <br>
108 EL表达式${userBean.userid}
109 <h1>
110 EL表达式
111 </h1>
112 <p>
113 EL:expression language 由标识符(11个保留字内置对象),存取器(.[]),文字与运算符组成
114 </p>
115 <p>
116 EL运算符
117 </p>
118 2+3:${2+3 }
119 <br>
120 2%3:${2 mod 3 }
121 <br>
122 3<2:${3 lt 2 }
123 <br>
124 empty:${empty userBean.username }
125 <h1>
126 JSTL表达式
127 </h1>
128 <h2>
129 c:if
130 </h2>
131 <c:if test="${userBean.userid==1}">
132 管理员,${userBean.username }
133 </c:if>
134 <h2>
135 c:out
136 </h2>
137 <c:out value="${userBean.sex}" default="未知性别"></c:out>
138
139 <h2>
140 c:set
141 </h2>
142 <c:set var="newVal" value="c:set"></c:set>
143 <!--如果使用了target则不需要scope 指定了scope则必须配合var -->
144 <c:set property="username" target="${userBean}" value="lwx2" />
145 c:out后newVal值:
146 <c:out value="${newVal}" />
147 <br>
148 <h2>
149 c:remove
150 </h2>
151 <c:remove var="newVal" />
152 c:remove后的newVal值:
153 <c:out value="${newVal}" />
154 <br>
155 ${userBean.username }
156 <br>
157 <jsp:getProperty name="userBean" property="username" /><br>
158 <h2>
159 c:choose
160 </h2>
161 <c:choose>
162 <c:when test="${userBean.userid==1}">
163 您好管理员
164 </c:when>
165 <c:otherwise>
166 您好会员
167 </c:otherwise>
168 </c:choose>
169
170 <h2>
171 c:import
172 </h2>
173 <%--c:import url="http://hao123.com"></c:import--%>
174 <h2>
175 c:redirect
176 </h2>
177 <%--c:redirect url="url.jsp" ></c:redirect--%>
178 <h2>
179 c:url
180 </h2>
181 <c:url value="url.jsp" var="url_1">
182 <c:param name="newVal" value="88888"></c:param>
183 </c:url>
184 <a href="${url_1 }">c:url</a>
185 <br>
186 <a href="${pageContext.request.contextPath }/url.jsp?newVal=99">pageContent</a>
187 <br>
188 <h2>
189 c:foreach
190 </h2>
191
192 <table border="1" cellpadding="0" cellspacing="1">
193 <th>
194 用户编号
195 </th>
196 <th>
197 用户姓名
198 </th>
199 <th>
200 用户性别
201 </th>
202
203 <c:if test="${!empty userlist }">
204 <c:forEach items="${userlist}" var="user">
205 <tr>
206 <td>
207 ${user.userid }
208 </td>
209 <td>
210 ${user.username }
211 </td>
212 <td>
213 ${user.sex }
214 </td>
215 </tr>
216 </c:forEach>
217 </c:if>
218 <c:if test="${empty userlist }">
219 <tr>
220 <td colspan="3">
221 查无数据
222 </td>
223 </tr>
224 </c:if>
225 </table>
226 <c:forEach begin="0" step="2" end="10" var="cn">${cn}<br></c:forEach>
227 </body>

jsp头部忘记引入包了

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@page import="com.lwx.UserBean"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

原文地址:https://www.cnblogs.com/draem0507/p/2107455.html