JSP文件和JSP文件或者HTML之间跳转方式
1、重定向跳转 response.sendRedirect("hello.jsp")
2、转发跳转 request.getRequestDispatcher("hello.jsp").forward(request,response);
JSP文件中的页面元素
1、<%@ %> 指令
2、<%%>小脚本
3、<%=name %>表达式
表单提交的执行步骤
1、实现login.html界面,其中设置form表单用于提交请求数据,action到check检查界面
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp">
用户名:<input type="text" name = "name">
密码:<input type = "password" name = "pwd">
<input type = "submit" value = "提交">
</form>
</body>
</html>
2、实现check.jsp界面,使用request.getParameter方法获取数据,对数据进行判断,并在这里设置用于记录是否登录的属性
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
if("wn".equals(name)&&"123".equals(pwd)){
session.setAttribute("name", "wn");
request.getRequestDispatcher("success.jsp").forward(request, response);
}
%>
</body>
</html>
3、实现success.jsp界面,包含可访问个人信息的a标签
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.lanou.practice.User"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="info.jsp">个人信息</a>
<%
User use1 = new User("wn","1");
User use2 = new User("musk","2");
List<User> list = new ArrayList<User>();
list.add(use1);
list.add(use2);
request.setAttribute("list", list);
request.setAttribute("name", "musk");
session.setAttribute("age", 10);
request.setAttribute("age", 15);
%>
<h1>${sessionScope.name }</h1>
<c:if test="${age>14 }">
<h1>笨小孩</h1>
</c:if>
<c:choose>
<c:when test="${age>20 && age < 30}">
男人
</c:when>
<c:when test="${age>10 && age < 20 }">
青年
</c:when>
<c:when test="${age<10 }">
男孩
</c:when>
<c:otherwise>老年人</c:otherwise>
</c:choose>
<c:forEach items="${list}" var= "li">
<div>
用户名:<span>${li.name}</span>
密码:<span>${li.pwd}</span>
</div>
</c:forEach>
</body>
</html>
4、实现info.jsp界面,对session中用于记录是否登录的属性进行判断,进而决定是否显示用户信息
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object object = session.getAttribute("name");
if(object == null){
response.sendRedirect("login.jsp");
}else{
out.println("已经登录");
}
%>
</body>
</html>
EL表达式
EL表达式书写格式
1、${name}
2、${sessionScope.name}
3、${ age > 10}
EL表达式查找顺序
page => request => session => application
EL表达式关系和逻辑运算
1、关系运算
== != > < >= <=
2、逻辑运算
&& || !
JSTL标签
JSTL标签所需准备
1、导入standard.jar和jstl.jar包
2、在JSP文件头部写入<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
JSTL标签常用标签
JSTL标签常用标签
1、if标签
<c:if test="${age>14 }">
<h1>笨小孩</h1>
</c:if>
2、choose when标签
<c:choose>
<c:when test="${age>20 && age < 30}">
男人
</c:when>
<c:when test="${age>10 && age < 20 }">
青年
</c:when>
<c:when test="${age<10 }">
男孩
</c:when>
<c:otherwise>老年人</c:otherwise>
</c:choose>
3、forEach标签
<c:forEach items="${list}" var= "li">
<div>
用户名:<span>${li.name}</span>
密码:<span>${li.pwd}</span>
</div>
</c:forEach>