html和jsp
html文件是静态的,jsp是动态的,此处的静态动态是指页面的展示是否能处理变化的对象信息。
jsp文件的使用需要导入jsp、jstl、standard三个坐标,在jsp文件中还需要开启jstl标签库和EL表达式:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!--引入jstl标签库-->
<%@ page isELIgnored="false"%> <!--注意在jsp中要开启EL表达式的方法,才能使用-->
可以使用EL表达式:${},括号中放入servlet等传过来的动态数据,例如重定向时,session中的对象的属性、servlet中设置的request域中的一些变量等。
if(user != null) {
//将用户存储如session
HttpSession session = request.getSession();
session.setAttribute("user", user);
//跳转到查询所有 重定向
String contextPath = request.getContextPath();
response.sendRedirect(contextPath + "/selectAllServlet");//jsp中可以访问到session中的user对象
} else {
//跳转到login页面
request.setAttribute("login_msg", "用户名或密码错误");//jsp中可以访问到request域中的变量login_msg
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
html+ajax替换jsp
现代编程中,通常将html+ajax替换jsp,来实现前后端的分离。避免servlet通过request域携带参数,直接转发到jsp页面通信的复杂性