1.JSP
1.1概述
Java Server Pages:java服务器页面,也和Servlet一样,用于动态Web技术。
最大特点:
- 写jsp就像写html一样
- 区别:
- html只给用户提供静态的数据。html的注释在客户端可以显示
- jsp页面中可以嵌入java代码,为用户提供动态数据。jsp的注释在客户端看不见,但是可以抓包。
1.2原理
服务器内部工作
- tomcat中有一个work目录
- idea中使用tomcat会在idea的tomcat中生产一个work目录
发现页面转换成了一个java程序,JSP最终也会被转换成为一个Java类。浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet。
JSP本质上也是Servlet:
部分内部源码:
//初始化
public void _jspInit() {}
//销毁
public void _jspDestroy() {}
//JSPService
public void _jspService(HttpServletRequest request, HttpServletResponse response)
public void _jspService(HttpServletRequest request, HttpServletResponse response)
-
判断请求
-
内置一些对象
final jakarta.servlet.jsp.PageContext pageContext;//页面上下文 final jakarta.servlet.ServletContext application; //application final jakarta.servlet.ServletConfig config; //config jakarta.servlet.jsp.JspWriter out = null; //out final java.lang.Object page = this; //page当前页 HttpServletRequest request; //请求 HttpServletResponse response; //响应
-
输出页面前增加的代码
response.setContentType("text/html; charset=UTF-8");//设置响应的页面类型 pageContext = _jspxFactory.getPageContext(this, request, response, null, false, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); out = pageContext.getOut(); _jspx_out = out;
-
以上这些对象我们可以在JSP页面上直接使用
-
在jsp页面中:
-
只要是java代码就会原封不动的输出
-
html代码,会被转换成下面的格式,输出到前端
out.write("<html lang=\"en\">\r\n");
-
1.3语法
任何语言都有自己的语法,JSP作为java技术的一种应用,它拥有一些自己的扩充语法,它支持java的所有语法。
1.3.1jsp表达式
<%--
用来将程序的输出,输出到客户端
<%= 变量或者表达式%>
--%>
<%= new java.util.Date()%>
1.3.2jsp脚本片段
<%
int sum=0;
for (int i = 1; i < 100; i++) {
sum+=i;
}
out.println("<h1>sum="+sum+"</h1>");
%>
1.3.3jsp脚本片段再现
<%
int x=1;
out.println(x);
%>
<p>这是一个jsp文档</p>
<%
int y=2;
out.println(y);
%>
<hr>
<%--在代码中嵌入html元素--%>
<%
for (int i = 1; i <=3; i++) {
%>
<h1>hello world!<%= i%></h1>
<%
}
%>
1.3.4jsp声明
全局变量:
<%!
static{
System.out.println("loading Servlet");
}
private int globalvar = 0;
public void m(){
System.out.println("进入了方法m");
}
%>
jsp声明会被编译到jsp生成的java类中,上面的那些则会被生成到_jspService方法中。
1.4指令
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 解决中文乱码
<%@ page errorPage="error/500.jsp" %> 定制错误页面
<%@page isErrorPage="true" %>
提取公共页面:
<%--会将三个页面合成一个页面--%>
<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@include file="common/footer.jsp"%>
<%--jsp标签:拼接页面,本质上还是三个页面--%>
<jsp:include page="/common/header.jsp"/>
<h1>1111111</h1>
<jsp:include page="/common/footer.jsp"/>
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
1.5九大内置对象
1.5.1概述
- PageContext 存东西
- Request 存东西
- Response
- Session 存东西
- Application 【ServletContext】存东西
- Config 【ServletConfig】
- Out
- Page 【不用】
- Exception
原理图:
request:客户端向服务器发送请求,产生数据,用户看完就没用了,比如:新闻
session:客户端向服务器发送请求,产生数据,用户用完一会还有用,比如:购物车
application:客户端向服务器发送请求,产生数据,一个用户用完,另一个用户还可以使用,比如:聊天数据
<body>
<%--内置对象--%>
<%
pageContext.setAttribute("name1","x1");//保存的数据只在一个页面中有效
request.setAttribute("name2","x2");//保存的数据只在一次请求中有效,求请转发会携带这个数据
session.setAttribute("name3","x3");//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","x4");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>
<%--脚本片段中的代码,会被原封不动的生成到.jsp.java
要求:这里的代码必须保证java语法的正确性,注释也是用java的注释// --%>
<%
//从pageContext中取出,我们通过寻找的方式来
//从低层到高层page-->request-->session-->application
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
String name5 = (String) pageContext.findAttribute("name5");//不存在
%>
<%--使用EL表达式输出结果 ${} --%>
<h1>取出的值为:</h1>
<h2>${name1}</h2>
<h3>${name2}</h3>
<h4>${name3}</h4>
<h5>${name4}</h5>
<h6>${name5}</h6>
<h6><%= name5%></h6>
</body>
输出结果:
1.5.2作用域
<%--作用域(scope)的改变:--%>
<%pageContext.setAttribute("a","b",pageContext.SESSION_SCOPE);%>
源码:
public void setAttribute(String name, Object attribute, int scope) {
switch(scope) {
case 1:
this.mPage.put(name, attribute);
break;
case 2:
this.mRequest.put(name, attribute);
break;
case 3:
this.mSession.put(name, attribute);
break;
case 4:
this.mApp.put(name, attribute);
break;
default:
throw new IllegalArgumentException("Bad scope " + scope);
}
}
<%--请求转发--%>
<%
pageContext.forward("/index.jsp");
request.getRequestDispatcher("/index.jsp").forward(request,response);
%>
1.6标签
1.6.1JSP标签
- jsp:include
- jsp:forward
- jsp:param
<%--http://localhost:8080/jsp/jsptag1.jsp?name=小明&age=18--%>
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="小明"/>
<jsp:param name="age" value="18"/>
</jsp:forward>
<%--取出参数--%>
名字:<%= request.getParameter("name")%>
年龄:<%= request.getParameter("age")%>
1.6.2JSTL标签
1.6.2.1概述
JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和java代码一样!
使用jstl标签库的步骤:
- 导入对应的jstl标签库
- 使用其中的方法
- 在tomcat中也需要引入我们的jstl包否则会报错!
pom.xml里需要导入的包:
<!--jstl表达式的依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--standard标签库-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
1.6.2.2核心标签
c:if
<%--引入jstl核心标签库,我们才能使用jstl标签--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<h4>if测试</h4>
<hr>
<form action="coretag.jsp" method="post">
<%--EL表达式获取表单中的数据 ${param.参数名} --%>
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>
<%--判断如果提交的用户名是管理员,则登陆成功--%>
<c:if test="${param.username == 'Admin'}" var="isAdmin">
<c:out value="管理员欢迎您"></c:out>
</c:if>
<c:out value="${isAdmin}"></c:out>
输出结果:
c:choose
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--定义一个变量score,值为85--%>
<c:set var="score" value="85"></c:set>
<c:choose>
<c:when test="${score>=90}">
<c:out value="优秀"/>
</c:when>
<c:when test="${score>=80}">
一般
</c:when>
<c:when test="${score>=70}">
<c:out value="良好"/>
</c:when>
<c:when test="${score>=60}">
<c:out value="及格"/>
</c:when>
</c:choose>
c:foreach
<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"赵一");
people.add(1,"李二");
people.add(2,"那三");
people.add(3,"王四");
request.setAttribute("list",people);
%>
<%--
var:每次遍历得到的变量
items:要遍历的对象
--%>
<c:forEach var="people" items="${list}">
<c:out value="${people}"/><br>
</c:forEach>
<hr>
<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
<c:out value="${people}"></c:out>
</c:forEach>
输出结果:
1.6.3EL表达式
${ }
- 获取数据
- 执行运算
- 获取web开发常用的对象
1.7JavaBean
实体类JavaBean有特定的写法:
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get、set方法
- 一般用来和数据库的字段做映射,ORM
ORM:对象映射关系
- 表—>类
- 字段—>属性
- 行记录—>对象
<%@ page import="moli.jsp.pojo.People" %>
<%
People people = new People();
people.setAddress("黑龙江");
people.setAge(18);
people.setId(1);
people.setName("小明");
String name = people.getName();
out.println(name);
%>
<hr/>
<jsp:useBean id="peole" class="moli.jsp.pojo.People" scope="page"/>
<jsp:setProperty name="peole" property="address" value="黑龙江"/>
<jsp:setProperty name="peole" property="name" value="小明"/>
<jsp:setProperty name="peole" property="age" value="18"/>
<jsp:setProperty name="peole" property="id" value="1"/>
<jsp:getProperty name="peole" property="name"/>
输出结果:
2.MVC三层架构
MVC:model view controller 模型视图控制器
2.1早些年
用户直接访问控制层,控制层可以直接操作数据库
servlet-->CRUD-->数据库
弊端:程序十分臃肿,不利于维护
servlet的代码中:处理请求,响应,视图跳转,处理JDBC,处理业务代码,处理逻辑代码
架构:没有什么是加一层解决不了的
程序员调用--JDBC--MySQL,Oracle,sqlServlet...
2.2现在
View
- 展示数据
- 提供连接,发起Servlet请求(a,form,img……)
Controller(Servlet)
- 接受用户请求(req请求参数,session信息……)
- 交给业务层处理对应代码
- 控制视图跳转
Model
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao)
登录---接收用户登录请求---处理用户的请求(获取用户登陆的参数,username,password)---交给业务层处理登陆业务(判断用户名和密码是否正确;事务)---Dao层查询用户名和密码是否正确---数据库
3.Filter和监听器
3.1Filter
过滤器: 用来过滤网站的数据
- 解决中文乱码
- 登录验证……
filter开发步骤:
-
导包(一定是javax.servlet下的Filter接口)
-
编写过滤器
chain链,让我们的请求继续走,让程序能够交接给下一部分;如果不写,我们的程序到这里将会被拦截停止。
package filter; import javax.servlet.*; import java.io.IOException; public class characterEncodingFilter implements Filter { //初始化与web服务器一同启动,随时等待过滤对象出现 @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("characterEncodingFilter已经初始化"); } //过滤器中的所有代码在过滤特定请求的时候都会执行 //必须要让过滤器继续前行chain.doFilter(request,response); @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); System.out.println("characterEncodingFilter执行前"); chain.doFilter(request,response); System.out.println("characterEncodingFilter执行后"); } //销毁,web服务器关闭的时候,过滤器才会被销毁 @Override public void destroy() { System.out.println("characterEncodingFilter已经销毁"); } }
public class showServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("你好"); } }
-
配置过滤器的web.xml
<servlet> <servlet-name>showServlet</servlet-name> <servlet-class>moli.showServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>showServlet</servlet-name> <url-pattern>/show</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>showServlet</servlet-name> <url-pattern>/moli/show</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>filter.characterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <!--只要是/moli下的请求都经过过滤器--> <url-pattern>/moli/*</url-pattern> </filter-mapping>
此时在浏览器输入地址:http://localhost:8080/f/show依旧是乱码,但是输入http://localhost:8080/f/moli/show就不会再乱码了。
3.2监听器
实现一个监听器的接口(N种):
//统计网站在线人数:统计session
public class OlinerCountListener implements HttpSessionListener {
//创建session监听:看客户端的一举一动
//一旦创建session就会触发一次这个事件
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext sc = se.getSession().getServletContext();
System.out.println(se.getSession().getId());
Integer onlineCount = (Integer) sc.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(1);
}else {
onlineCount = new Integer(onlineCount+1);
}
sc.setAttribute("OnlineCount",onlineCount);
}
//销毁session监听
//一旦销毁session就会触发一次这个事件
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext sc = se.getSession().getServletContext();
Integer onlineCount = (Integer) sc.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(0);
}else {
onlineCount = new Integer(onlineCount-1);
}
sc.setAttribute("OnlineCount",onlineCount);
}
}
<h1>
当前有<span style="color: bisque">
<%= this.getServletConfig().getServletContext().getAttribute("OnlineCount")%>
</span>人在线
</h1>
<!--注册监听器-->
<listener>
<listener-class>listener.OlinerCountListener</listener-class>
</listener>
输出结果:
监听的应用:
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame("立冬");//新建一个窗体
Panel panel = new Panel(null);//面板
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(0,99,23));
panel.setBounds(50,50,300,300);
panel.setBackground(new Color(100,0,0));
frame.add(panel);
frame.setVisible(true);
//监听关闭事件
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
3.3应用
用户登录之后才能进入主页,用户注销后就不能进入主页了。
登录页面:
<form action="/moli/login" method="post">
<input type="text" name="username">
<input type="submit">
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取前端请求的参数
String username = req.getParameter("username");
if (username.equals("admin")){
req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
resp.sendRedirect("/sys/success.jsp");
}else {
resp.sendRedirect("/error.jsp");
}
}
}
登陆成功页面
<h1>主页</h1>
<p><a href="/moli/logout">注销</a></p>
注销操作
public class logoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object user_session = req.getSession().getAttribute("USER_SESSION");
if (user_session!=null){
req.getSession().removeAttribute("user_session");
resp.sendRedirect("/login.jsp");
}
}
}
登陆失败页面
<h1>错误</h1>
<h2>没有权限或者用户名错误</h2>
<a href="/login.jsp">返回登陆页面</a>
过滤器
public class SysFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
if (request.getSession().getAttribute("user_session") == null) {
response.sendRedirect("/error.jsp");
}
chain.doFilter(req,resp);
}
@Override
public void destroy() {}
}