一:MVC设计模式
MVC是一个设计模式,它强制的使应用层的输入,处理和输出分开。使用MVC设计模式被分为三个核心层:模型层,视图层,控制层。它们各自处理自己的任务。
显示层(View):此层主要是负责将内容显式给用户。比如:JSP
控制层(Controller):负责判断所有的用户请求参数是否合法,根据请求的类型调用模型层执行操作,再讲处理结果交给显示层显示。eg:servlet
模型层(Model):操作数据库的独立的操作组件,或使用lavaBean(POJO)保存数据。
二:JSP内置对象
JSP中提供了九个内置对象,这些内置对象由容器为用户进行实例化,用户直接使用即可。
eg:
- (1):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>response内置对象title>
- head>
- <body>
- <h3>3秒之后跳转到index页面h3>
- <%
- // response.setHeader("refresh","3"); 定时刷新
- response.setHeader("refresh","3;URL=index.jsp"); // 定时跳转
- %>
- body>
- html>
- (2):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>index.jsptitle>
- head>
- <body>
- <h3>跳转过来了h3>
- body>
- html>
- (3):<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" isErrorPage="true"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>处理错误的页面title>
- head>
- <body>
- <%
- String errorMsg=exception.getMessage();
- out.println("<h3>有点问题:"+errorMsg+"h3>");
- %>
- body>
- html>
- (4):xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
- <display-name>JSPProjectdisplay-name>
- <welcome-file-list>
- <welcome-file>index.htmlwelcome-file>
- <welcome-file>index.htmwelcome-file>
- <welcome-file>index.jspwelcome-file>
- <welcome-file>default.htmlwelcome-file>
- <welcome-file>default.htmwelcome-file>
- <welcome-file>default.jspwelcome-file>
- welcome-file-list>
-
-
- <context-param>
- <param-name>ctxparam-name>
- <param-value>ContextParamValueparam-value>
- context-param>
-
- <jsp-config>
- <taglib>
- <taglib-uri>http://www.mycompany.comtaglib-uri>
- <taglib-location>/WEB-INF/c.tldtaglib-location>
- taglib>
- jsp-config>
- web-app>
三:EL表达式语言
使用表达式语言(EL)可以在JSP页面进行方便的输出内容:
EL语法:${表达式}
EL有自己的内置对象:
PageContext ,pageScope,requestScope,sessionScope,applicationScope,param,paramValues,header,headerValues,cookie,initParam
1. 使用EL访问不同的属性范围:
${pageScope.属性名},${requestScope.属性名},${sessionScope.属性名},${applicationScope.属性名},这四种属性访问范围由小到大。
Eg:
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL访问不同属性范围的值title>
- head>
- <body>
- <%
- pageContext.setAttribute("info","page范围的属性值");
- request.setAttribute("info","request范围的属性值");
- session.setAttribute("info","session范围的属性值");
- application.setAttribute("info","application范围的属性值");
- %>
-
- <h3>page===> ${pageScope.info}h3>
- <h3>request===> ${requestScope.info}h3>
- <h3>session===> ${sessionScope.info}h3>
- <h3>application===> ${applicationScope.info}h3>
- <h3>${info}=====> ${info}h3>
- body>
- html>
2. EL访问JSP内置对象
使用EL的pageContext内置对象访问JSP的内置对象:${pageContext.对应的jsp内置对象}
Eg:EL获取上下文路径:特殊:${pageContext.servletContext.contextPath}
EL访问session的ID:${pageContext.session.id}
Eg::
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL访问jsp内置对象title>
- head>
- <body>
- <%
- String contextPath=application.getContextPath();
- %>
- <h3>直接通过JSP内置对象获取上下文路径:<%=contextPath%>h3>
- <h3>通过EL获取上下文路径:${pageContext.servletContext.contextPath}h3>
- <h3>通过EL获取sessionID: ${pageContext.session.id}h3>
- <h3>通过EL判断当前的请求方式: ${pageContext.request.method}h3>
- body>
- html>
3. EL访问参数(访问客户端发送的参数。全局参数,一组参数)
用途1:使用EL的param内置对象访问客户端发送的参数${param.参数名}
eg:
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL访问客户端发送的请求参数title>
- head>
- <body>
- <h3>客户端发送来的username参数是:${param.username}h3>
- <h3>客户端发送来的pwd参数是:${param.pwd}h3>
- body>
- html>
用途2:使用EL的initParam内置对象访问上下文参数(全局参数),在web.xml中配置上下文参数:
admin param-name >
Obama param-value>
eg:
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL访问全局参数(上下文参数)title>
- head>
- <body>
- <h3>全局参数为ctx的值是:${initParam.ctx}h3>
- body>
- html>
用途3:使用EL的paramValues内置对象访问一组参数
${paramValues.参数名[0]}访问一组参数值
${paramValues.参数名[n]}访问n+1组参数值
eg:
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>兴趣爱好页面title>
- head>
- <body>
- <form action="paramValues.jsp" method="post">
- 运动: <input type="checkbox" name="hobby" value="sport"/> <br/>
- 电影: <input type="checkbox" name="hobby" value="movie"/> <br/>
- 读书: <input type="checkbox" name="hobby" value="reading"/> <br/>
- 音乐: <input type="checkbox" name="hobby" value="music"/> <br/>
- <input type="submit" value="提交"/>
- form>
- body>
- html>
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL访问一组参数title>
- head>
- <body>
- <h3>爱好是:h3>
- <h3> ${paramValues.hobby[0]} h3>
- <h3> ${paramValues.hobby[1]} h3>
- <h3> ${paramValues.hobby[2]} h3>
- <h3> ${paramValues.hobby[3]} h3>
- body>
- html>
4. 访问cookie
通过EL的cookie内置对象访问JSESSIONID名称的cookie的语法:
${cookie[“JSESSIONID”].name},${cookie[“JSESSIONID”].value}
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL的cookie内置对象访问Cookietitle>
- head>
- <body>
- <h3>Cookie名为JSESSIONID的值为: ${cookie["JSESSIONID"].value}h3>
- <h3>sessionID为:${pageContext.session.id}h3>
- body>
- html>
5. 访问header
${header[“cookie”] }
EL的运算符
1. 算数运算符
+ - * /(div) %(mod)
eg: ${param.num1+param.num2} 参数相加
2. 关系运算符
< (lt) > (gt) == (eq) !=(ne) >=(ge) <=(le)
3. 逻辑运算符
&&(and) ||(o r ) !(not)
4. empty运算符
${empty 表达式} 判断是否为null或空字符串””
5. 三目运算符
${返回true或false的表达式 ? "为true时输出的内容":"为false时输出的内容"}
6. 括号运算符
()用来改变运算顺序的
eg:
- <%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
- >
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>EL运算符title>
- head>
- <body>
- <h3>num1+num2= ${param.num1+param.num2}h3>
- <h3>num1/num2= ${param.num1 / param.num2} 也可以div ${param.num1 div param.num2}h3>
- <h3>num1是否等于num2: ${param.num1 == param.num2}h3>
- <h3>true && true: ${true && true}h3>
- <h3>true || false: ${true || false}h3>
- <h3>${empty param.abc}h3>
- <h3>三目运算符:${param.num1>param.num2 ? "num1真的大于num2":"num1不大于num2" }h3>
- body>
- html>
四:JSTL(JSP Standard Tag Libraries)
引入JSTL的步骤:
第一步:将jstl-xx.jar导入/WEB-INF/lib文件夹下。
第二步:将jstl-xx.jar解压后的META-INF文件夹下的xxx.tld文件拷贝到/WEB-INF/的某目录下 第三步:在JSP页面使用taglib指令引入xxx.tld文件。
JSP页面使用taglib指令引入xxx.tld文件
<%@ taglib uri="指向xxx.tld文件的路径" prefix="JSTL标签前缀"%>注意:uri中指向xxx.tld文件的路径有两种写法: 第一种:直接指向xxx.tld文件的路径第二种:在web.xml中配置taglib的uri http://www.xxx.com /WEB-INF/c.tld 1.输出标签 2.设置标签 设置属性范围的属性值 设置对象的属性值
3.捕获异常标签
// 有可能发生异常的代码
4.判断标签
// 判断结果为true,执行此处
5.forEach标签(遍历list集合或Map集合)
${msg}
6.choose标签
=90}">
优秀
=80}">
良好
不及格,要加油!