一.转发和重定向
1.它们的相同点
- 都是解决2个WEB组件之间的跳转问题
WEB组件: Servlet/JSP
2.它们的区别(*)
![这里写图片描述](https://img-blog.csdn.net/20170626095326427?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzgxMzE2Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
3.使用建议
- 通常查询时用转发
- 通常增加、修改、删除后重定向到查询
二.EL和JSTL
1.它们的作用
2.演示案例
3.JSTL原理
FindStudentServlet.java
package web;import java.io.IOException;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import entity.Course;
import entity.Student;public class FindStudentServlet extends HttpServlet {
@Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {Student stu = new Student();stu.setName("zhangsan");stu.setAge(23);stu.setSex("M");stu.setInterests(new String[]{
"篮球","足球","排球"});Course c = new Course();c.setId(1);c.setName("Java");c.setDays(82);stu.setCourse(c);req.setAttribute("stu", stu);req.getRequestDispatcher("student.jsp").forward(req, res);}}
student.jsp
<%@page pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!doctype html> <html><head><meta charset="utf-8"/><title>查询学生</title></head><body><p><c:if test="${stu.sex=='M' }">男</c:if><c:if test="${stu.sex=='F' }">女</c:if></p><p><c:choose><c:when test="${stu.sex=='M' }">男</c:when><c:otherwise>女</c:otherwise></c:choose></p><p><c:forEach items="${stu.interests }" var="i">${i }</c:forEach></p><p>姓名:${stu.name }</p><p>年龄:${stu.age }</p><p>性别:${stu["sex"] }</p><p>课程ID:${stu.course.id }</p><p>兴趣:${requestScope.stu.interests }</p><p>年龄+5:${stu.age+5 }</p><p>介于20-30间:${stu.age>20 && stu.age<30 }</p><p>是否为空:${empty stu.interests }</p><p>参数:${param.user }</p></body> </html>