1.什么是Jsp
相当于在html里写java,java部分在服务器端运行
本质上是servlet,相当于把html的部分用out打印出来了
现在主要被html + ajax取代
导入包:
<dependencies>
<!-- 导入jsp的jar包 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!-- 导入jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 导入servlet的包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
2.用<% %>来 把Java代码框起来
<%-- 类似于java中import导入包和类 --%>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%-- page指令,指定响应类型和编码 --%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>第一个JSP</title>
</head>
<body>
<h1 style="color:red;">JSP初体验</h1>
<%-- 这是JSP的注释 --%>
<%-- java代码写在代码块中 --%>
<%
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//out对象可以直接使用,这是内置对象,在JSP中有9个内置对象可以直接使用,比较常用:request response session out等
out.print(format);
%>
</body>
</html>
3.Jsp脚本,加了!就相当于全局变量
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP脚本</title>
</head>
<body>
<h2>代码段</h2>
<%
//代码会出现在_jspService()方法中,写普通的Java代码
int m = 3 + 5;
out.print(m);
%>
<h2>JSP表达式</h2>
<%-- 相当于out.print(),用于输出变量的值,后面没有分号 --%>
<%=m%>
<h2>声明</h2>
<%!
//声明的变量和方法出现在_jspService()方法之外,相当于成员(全局)变量
int m=10;
%>
<%-- 输出局部变量m --%>
<%=m%> <br/>
<%-- 输出全局变量m --%>
<%=this.m%> <br/>
</body>
</html>
4.四个作用域
5.jsp获取数据:
只能使用转发,因为数据存在servelet里使用重定向会丢失
/**
* 要先访问Servlet,在Servlet中把值放到作用域中,在JSP中显示出来
*/
@WebServlet("/Demo1ExpressionServlet")
public class Demo1ExpressionServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.创建List数据和字符串数据
List<Brand> brands = new ArrayList<>();
brands.add(new Brand(1,"三只松鼠","三只松鼠",100,"三只松鼠,好吃不上火",1));
brands.add(new Brand(2,"优衣库","优衣库",200,"优衣库,服适人生",0));
brands.add(new Brand(3,"小米","小米科技有限公司",1000,"为发烧而生",1));
//2.放在请求域中
request.setAttribute("brands", brands);
request.setAttribute("username", "NewBoy");
//3.在JSP上显示,注:不能使用重定向,要使用转发
request.getRequestDispatcher("/demo03_el.jsp").forward(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
6.使用jstl
首先使用1里面的坐标导入jstl的包
使用<c:forEach>
来开启for循环
同时开头也要使用固定写法
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%-- 导入标签库, taglib标签库指令名 prefix前缀,所有的标签以指定的前缀开头,uri 唯一标识,固定写法 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>使用EL显示请求域中数据</title>
</head>
<body>
<h2>品牌列表</h2>
${brands}
<h2>用户名</h2>
${username}
<h2>表格显示品牌</h2>
<%-- table按ctrl+j --%>
<table border="2" width="500">
<tr>
<th>序号</th>
<th>品牌名称</th>
<th>企业名称</th>
<th>排序</th>
<th>品牌介绍</th>
<th>状态</th>
</tr>
<%--
forEach作用:用于遍历集合或数组
items:指定要遍历的集合或数组,注:参数值要指定为EL ${作用域变量名}
var:保存每个元素的变量名,默认放在页面域中
--%>
<c:forEach items="${brands}" var="brand">
<tr>
<%-- 本质上是调用brand.getId()等方法获取属性值 --%>
<td>${brand.id}</td>
<td>${brand.brandName}</td>
<td>${brand.companyName}</td>
<td>${brand.ordered}</td>
<td>${brand.description}</td>
<td>
<%-- if标签作用:如果条件为真,则执行标签体中的内容 test属性:EL布尔表达式 --%>
<c:if test="${brand.status==1}">
启用
</c:if>
<c:if test="${brand.status==0}">
禁用
</c:if>
</td>
</tr>
</c:forEach>
</table>
<h2>固定次数循环</h2>
<%-- var:保存每次循环变量名 begin:起始值 end:结束的值 step:每次跳几个元素 --%>
<c:forEach var="i" begin="1" end="100" step="2">
${i}
</c:forEach>
</body>
</html>
7.使用BeanUtils工具类
<!-- BeanUtils工具类 -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.8.3</version>
</dependency>
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置请求的编码为utf-8
request.setCharacterEncoding("utf-8");
//2.获取表单所有的数据
Map<String, String[]> parameterMap = request.getParameterMap();
//3.封装成Brand对象,使用BeanUtils工具类,将map对象中同名的键复制到Brand同名的属性中
Brand brand = new Brand();
try {
//将parameterMap中的键,复制给brand中同名属性
BeanUtils.populate(brand,parameterMap);
} catch (Exception e) {
e.printStackTrace();
}
//4.调用业务层添加
BrandService brandService = new BrandService();
brandService.add(brand);
//5.重定向到SelectAllServlet中显示
response.sendRedirect("SelectAllServlet");
}