1、JSP的注释:
作用:注释Java脚本代码 语法:<%--这是注释--%>
2、JSP的Java脚本表达式:
作用:输出数据到页面上 语法:<%=表达式%>(实际上就是调用输出流打印到页面上) out.print(表达式);
3、JSP中的Java脚本片段:(实际开发中,应做到JSP中不能出现一行Java脚本片段)
作用:书写Java代码逻辑 语法:<% 语句1; 语句2;%>
原理:其中的语句会原封不动的被服务器翻译到对应的Servlet的_jspService方法中。
4、JSP的声明:
作用:定义类的成员 语法:<%! Java代码 %>
二、EL表达式
对比:
采用表达式之前
<%
pageContext.setAttribute("msg", "pageContext");
request.setAttribute("msg", "pageContext");
session.setAttribute("msg", "pageContext");
application.setAttribute("msg", "pageContext");
%>
pageContext<%=pageContext.getAttribute("msg") %><br/>
request<%=request.getAttribute("msg") %><br/>
session<%=session.getAttribute("msg") %><br/>
application<%=application.getAttribute("msg") %>
采用表达式后:
<%
pageContext.setAttribute("msg", "pageContext");
request.setAttribute("msg", "pageContext");
session.setAttribute("msg", "pageContext");
application.setAttribute("msg", "pageContext");
%>
pageContext: ${pageScope.msg}<br/>
request: ${requestScope.msg}<br/>
session: ${sessionScope.msg}<br/>
application:${applicationScope.msg}
${msg}
结合JSTL以及EL表达式练手项目:
拿之前shopping 作业来练手:
1.初始化项目。
2.实现思路:
效果:
部分源码:
view:list
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<h2>订单信息列表</h2>
<a href="/orders/edit">新增</a>
<table cellpadding="0" cellspacing="0" width="60%" border="1">
<tr style="background-color: yellow">
<th>序号</th>
<th>用户ID</th>
<th>用户真实姓名</th>
<th>商品名称</th>
<th>商品价格</th>
<th>添加的时间</th>
<th>是否支付</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="ord" varStatus="vs">
<tr style="background-color:${vs.count%2==0?'pink':''};">
<td>${pageScope.ord.id}</td>
<td>${pageScope.ord.users.id}</td>
<td>${pageScope.ord.users.realName}</td>
<c:forEach items="${ord.details}" var="orderDetail" begin="0" end="0">
<td>${pageScope.orderDetail.goods.goodsName}</td>
<td>${pageScope.orderDetail.goods.price}</td>
</c:forEach>
<td>${pageScope.ord.addDate}</td>
<td>${pageScope.ord.isPay}</td>
<td>修改</td>
</tr>
</c:forEach>
</table>
</body>
</html>
package com.shopping.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shopping.dao.OrderDao;
import com.shopping.dao.OrderDaoImpl;
import com.shopping.model.Orders;
@WebServlet("/orders/list")
public class OrdersListServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private OrderDao dao;
@Override
public void init() throws ServletException {
dao= new OrderDaoImpl();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取所有的订单信息
List<Orders> list=dao.getAllByUser(2);
req.setAttribute("list", list);
req.getRequestDispatcher("/WEB-INF/views/orders/list.jsp").forward(req,resp);
}
}