2.9 jsp+el+jstl基础示例
1.基础示例
<%@ page contentType="text/html;charset=UTF-8" import="com.javasm.entity.MyUser" language="java" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
jsp java server page 生成动态页面
jsp本质上 就是servlet
1.访问地址由web服务器自动配置 仿照静态资源地址进行配置 /项目名/xxx.jsp
2. 翻译 编译 运行
xxx.jsp xxx_jsp.java xxx_jsp.class _jspService(req,resp)
3.jsp中 html代码和jsp代码混合
I.<%@ 指令 %>
page指令 做页面基本配置 响应头 导包
include指令 引入页面
引入公共html页面
引入公共java代码
II.<% java代码 %> java脚本
<% if(user!=null){ %> java代码
<span style="color: red">欢迎</span>你<%=user.getUserName()%> html代码+java代码
<% }else{ %> java代码
没有登录 <a href="/day3/index.jsp">请先登录</a> html代码
<% } %>java代码
III.<%= 变量 %> 输出到页面
IV.<%! %> 定义方法
V.<% -- -- %> 注释
--%>
测试jsp
<p>请你耗子尾汁</p>
<% String a = "abc";
System.out.println(a);
out.print(a);
%>
<%=a%>
<%
MyUser user = new MyUser(1,"aa","aa","aa","aa");
MyUser user2 = new MyUser(2,"aa2","2","aa","aa");
MyUser user3 = new MyUser(3,"aa3","a3","aa","aa");
//user = null;
List<MyUser> lm = new ArrayList<MyUser>();
lm.add(user);
lm.add(user2);
lm.add(user3);
%>
<% if(user!=null){ %>
<%--out.print("欢迎你"+user.getUserName());--%>
<span style="color: red">欢迎</span>你<%=user.getUserName()%>
<% }else{ %>
<%--out.print("没有登录 请先登录");--%>
没有登录 <a href="/day3/index.jsp">请先登录</a>
<% } %>
<table border="1">
<thead>
<tr><th>用户编号</th><th>用户名</th><th>用户电话</th><th>家庭住址</th></tr>
</thead>
<tbody>
<% for(MyUser myuser:lm){ %>
<tr><td><%=myuser.getUserId()%></td>
<td><%=myuser.getUserName()%></td>
<td><%=myuser.getUserPhone()%></td>
<td><%=myuser.getUserAddr()%></td>
</tr>
<% } %>
</tbody>
</table>
<%! int getSum(int a,int b){
return a+b;
}
%>
<%=getSum(2,3)%>
</body>
</html>
2.内置对象和作用域
<%@ page contentType="text/html;charset=UTF-8" import="com.javasm.entity.MyUser" language="java" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
jsp内置对象
xxx.jsp文件中没有定义过 xxx_jsp.java文件中 有的对象 内置对象
1.request 请求对象
2.response 响应对象
3.session 会话对象
4.application 服务器对象 ServletContext
5.out 输出流对象
6.pageContext 页面对象集合
7.config 配置信息对象
8.page this 当前页面
9.exception 异常信息对象
jsp作用域
page pageContext 当前页面生效
request
session
application
--%>
demo2页面
<%@ include file="common1.jsp" %>
<%@ include file="common2.jsp" %>
<%=test%>
<%=request.getParameter("testkey")%>
<%session.setAttribute("sessionkey","sessionval");%>
<%=session.getAttribute("sessionkey")%>
<%application.setAttribute("testkey","abc");%>
<%pageContext.setAttribute("pagekey","pageval");%>
</body>
</html>
3.登录界面示例
<%@ page import="com.javasm.entity.MyUser" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.css">
<style>
.logindiv{
width: 400px;
height: 300px;
border: 1px solid lightblue;
background-color: lightgray;
border-radius: 10px;
margin: 250px auto;
padding: 80px 50px;
}
.alertmsg{
color: lightcoral;
}
</style>
</head>
<body>
<% MyUser insertUser = (MyUser) request.getAttribute("insertUser");%>
<div class="logindiv">
<form action="<%=request.getContextPath()%>/login" method="post">
<div class="form-group">
<label for="uname">用 户 名</label>
<input type="text" class="form-control" id="uname" name="uname"
value="<%if(insertUser!=null) out.print(insertUser.getUserName());%>"
placeholder="请输入用户名">
</div>
<div class="form-group">
<label for="upwd">Password</label>
<input type="password" class="form-control" id="upwd" name="upwd"
value="<%if(insertUser!=null) out.print(insertUser.getUserPass());%>"
placeholder="Password">
</div>
<button type="submit" class="btn btn-default">登录</button>
<%-- <%String loginfailed = request.getParameter("loginfailed");
if(loginfailed!=null){%>
<span class="alertmsg">用户名或密码错误</span>
<% } %>--%>
<% String loginfailed = (String) request.getAttribute("loginfailed");
if(loginfailed!=null){%>
<span class="alertmsg">用户名或密码错误</span>
<% } %>
</form>
</div>
</body>
</html>
4.el表达式
<%@ page import="com.javasm.entity.MyUser" %><%--
Created by IntelliJ IDEA.
User: JAVASM
Date: 2020/12/3
Time: 9:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%request.setAttribute("testnum","abcd"); %>
<script src="js/jquery-3.5.1.min.js"></script>
<script>
$(function(){
var temp = "${testnum}";
console.log(temp);
})
</script>
</head>
<body>
<%session.setAttribute("testkey","sessionval"); %>
<%request.setAttribute("testkey","testval"); %>
<% MyUser mu = new MyUser("jack","abc");
request.setAttribute("myuser",mu); %>
${not empty sessionScope.testkey}
${param.testparam}
${pageContext.request.contextPath}
<%--
el表达式
1.良好的非空判断 500
2.自动类型转换
3.可以使用运算符 empty 检测取出的数据是否为空
not empty 是否存在
主要功能
1.从域中取值 并输出到页面
2.从参数对象中取值
3.使用pageContext对象
作用域访问对象 如果不指明域 从上到下
pageScope page域
requestScope request域
sessionScope session域
applicationScope application域
参数访问对象
param
paramValues
获取pageContext
获取项目根
${pageContext.request.contextPath} /day4_jsp
--%>
</body>
</html>
5.jstl
<%@ page import="com.javasm.entity.MyUser" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<body>
<%--
jstl标签
模仿html标签结构 编写java代码
通用标签
//在域对象中设置键值对
c:set var 表示key
value 表示值
secope 表示域
c:out
//在域对象中清除键值对
c:remove var 表示key
secope 表示域
//逻辑判断 如果为true 执行标签体中的代码
c:if test 传入bol值 判断条件
var 表示key 把判断结果存入域中
scope 表示域
c:redirect url 跳转地址
--%>
<%-- session.setAttribute("testkey","testval") --%>
<c:set var="testkey" value="testval" scope="session"></c:set>
<%-- session.removeAttribute("testkey");--%>
<c:remove var="testkey" scope="session"/>
${sessionScope.testkey}
<br>
<c:set var="loginuser" value="testval" scope="session"></c:set>
<c:remove var="loginuser" scope="session"/>
欢迎你登录
<%-- <c:if test="${empty loginuser}">
<c:redirect url="login.jsp"/>
</c:if>--%>
<%
MyUser user = new MyUser(1,"aa","aa","aa","aa");
MyUser user2 = new MyUser(2,"aa2","2","aa","aa");
MyUser user3 = new MyUser(3,"aa3","a3","aa","aa");
//user = null;
List<MyUser> lm = new ArrayList<MyUser>();
lm.add(user);
lm.add(user2);
lm.add(user3);
request.setAttribute("listuser",lm);
%>
<table class="table">
<thead>
<tr><th>用户编号</th><th>用户名</th><th>用户电话</th><th>家庭住址</th></tr>
</thead>
<tbody>
<c:forEach items="${listuser}" var="myuser">
<tr><td>${myuser.userId}</td>
<td>${myuser.userName}</td>
<td>${myuser.userPhone}</td>
<td>${myuser.userAddr}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>