文章目录
JSP
Java Server Pages java服务器端页面
一个特殊的页面,其中既可以指定html标签,又可以指定java代码
用于简化书写
原理:
通过浏览器请求一个a.jsp资源路径—> 服务器解析请求消息 查找是否有这个名称的jsp资源 如果没有报404 如果找到了,会将这个jsp资源转换为一个.java文件 ---->编译.java 文件生成字节码文件 由字节码文件提供访问
生成一个jsp的java类继承HttpJSPBase(apache提供的)
jsp本质上是一个servlet
这里面实际上就是写的一些java 代码,所以说jsp实际上是一个servlet.
这个.class文件在tomcat 运行文件夹work中能找到
eg:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
public final class activity1199Index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("java.util");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("<meta charset=\"utf-8\">\r\n");
out.write(" \t<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\r\n");
out.write(" \t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" <title>授权页面</title>\r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write(" <style>\r\n");
out.write(" \t*,body{ margin:0; padding:0;}\r\n");
out.write(" </style>\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body οnlοad=\"returna()\">\r\n");
out.write(" </body>\r\n");
out.write(" <script type=\"text/javascript\">\r\n");
out.write(" function returna() {\r\n");
out.write(" \t//放到服务器上时 appid必须放开\r\n");
out.write(" var appid = \"wx0b175776446c091c\";\r\n");
out.write(" \t//caidan\r\n");
out.write(" var caidan = \"activity1199\";\r\n");
out.write(" window.location.href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=\"+appid+\"&redirect_uri=http://zxgh.guojianquan.com/middlelayer/zxghwx/zxghwxgetopenid/\"+caidan+\"&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect\";\r\n");
out.write(" // window.location.href = \"ghsy/zhaopin\";\r\n");
out.write(" }\r\n");
out.write("</script>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
Jsp指令
用于配置jsp页面 导入资源文件
<%@ 指令名称 属性名1=属性值 属性名2=属性值%>
分类 :1 page 配置jsp页面
contentType:等同于response.setContentType()
1 设置响应体的mime类型及字符集
2 设置当前jsp页面的编码(只能高级的IDE才能生效,如果使用低级工具则需要配置pageEncoding属性设置当前字符集)
import :导入包
jsp页面写java代码除了内置的九大对象,其他工具类对象都需要导入
errorPage: 页面发生错误时跳的错误页面
isErrorPage : 如果为true 那么页面中就有第九个对象excetion
2 include 导入资源页面文件
<%@include file="top.jsp"%>
3 taglib 导入一些标签库 如JSTL
现在本地加入JSTL标签的jar包之后引入
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
prefix 中的 c 代表是前缀
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true | false" ]
[ buffer="none | 8kb | sizekb" ]
[ autoFlush="true | false" ]
[ isThreadSafe="true | false" ]
[ info="text" ]
[ errorPage="relative_url" ]
[ isErrorPage="true | false" ]
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
[ pageEncoding="characterSet | ISO-8859-1" ]
[ isELIgnored="true | false" ]
%>
可以将数据输出到页面上,字符输出流对象 与response.getWriter()类似
response.getWriter() 与 out.write()区别
response.getWriter()输出的数据在最上侧先于out.write()输出
tomcat真正给客户端浏览器做出响应之前会先找response中有无数据,有则写到响应体中,在去out中找数据
Jsp注释
1 html 中的注释 <!--->只能注释html代码片段(注释不了java代码片段)
2 jsp 中的注释 <%----%>可以注释所有
jsp脚本
<% java代码%> 定义java代码
<% ! %> 定义的java代码在jsp转换类的成员位置(成员方法 成员变量)
<%= %> 输出语句
jsp 内置对象
在jsp页面中不需要创建获取直接拿过来用的对象,被称为内置对象
这是因为在jsp的实体类中的_jspservice方法中已经定义了这几个对象
jsp一共有九个内置对象
变量名 实类
--------------------------jsp页面的域对象(四个)用来共享数据---------------------------
pageContent javax.servlet.jsp.PageContext pageContext
当前页面共享数据,还可以获取其他八个内置对象
request javax.servlet.http.HttpServletRequest
一次请求访问的多个资源(请求转发)
session javax.servlet.http.HttpSession session
一次会话多次请求
application javax.servlet.ServletContext application
所有用户间共享数据
--------------------------域对象---------------------------
response javax.servlet.http.HttpServletResponse
响应对象
page java.lang.Object
当前页面的对象也就是servlet
out javax.servlet.jsp.JspWriter
输出对象 把数据输出到页面上
config javax.servlet.ServletConfig
servlet配置对象
exception Throwable
异常对象
EL表达式
概念:Expression Language 表达式语言
作用:替换和简化jsp页面中java代码的编写
语法:${表达式}
注意:
jsp默认支持el表达式的。如果要忽略el表达式
1. 设置jsp中page指令中:isELIgnored=“true” 忽略当前jsp页面中所有的el表达式
2. ${表达式} :忽略当前这个el表达式 用双引号包含也依然有效
5. 使用:
运算:
运算符:
1. 算数运算符: + - * /(div) %(mod)
2. 比较运算符: > < >= <= == !=
3. 逻辑运算符: &&(and) ||(or) !(not)
4. 空运算符: empty
功能:用于判断字符串、集合、数组对象是否为null或者长度是否为0
${empty list}:判断字符串、集合、数组对象是否为null或者长度为0
${not empty str}:表示判断字符串、集合、数组对象是否不为null 并且 长度>0
获取值
el表达式只能从域对象中获取值
语法:
域
名
称
.
键
名
:
从
指
定
域
中
获
取
指
定
键
的
值
域
名
称
:
p
a
g
e
S
c
o
p
e
−
−
>
p
a
g
e
C
o
n
t
e
x
t
r
e
q
u
e
s
t
S
c
o
p
e
−
−
>
r
e
q
u
e
s
t
s
e
s
s
i
o
n
S
c
o
p
e
−
−
>
s
e
s
s
i
o
n
a
p
p
l
i
c
a
t
i
o
n
S
c
o
p
e
−
−
>
a
p
p
l
i
c
a
t
i
o
n
(
S
e
r
v
l
e
t
C
o
n
t
e
x
t
)
举
例
:
在
r
e
q
u
e
s
t
域
中
存
储
了
n
a
m
e
=
张
三
获
取
:
{域名称.键名}:从指定域中获取指定键的值 域名称: pageScope --> pageContext requestScope --> request sessionScope --> session applicationScope --> application(ServletContext) 举例:在request域中存储了name=张三 获取:
域名称.键名:从指定域中获取指定键的值域名称:pageScope−−>pageContextrequestScope−−>requestsessionScope−−>sessionapplicationScope−−>application(ServletContext)举例:在request域中存储了name=张三获取:{requestScope.name}
${键名}:表示依次从最小的域中查找是否有该键对应的值,直到找到为止。
获取对象、List集合、Map集合的值
对象:${域名称.键名.属性名}
本质上会去调用对象的getter方法
List集合:${域名称.键名[索引]}
Map集合:
${域名称.键名.key名称}
${域名称.键名["key名称"]}
隐式对象:
不用创建直接拿过来用,类似于jsp的内置对象
* el表达式中有11个隐式对象
* pageContext:
* 获取jsp其他八个内置对象
* ${pageContext.request.contextPath}:在jsp页面动态获取虚拟目录
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}">
el表达式判断多个值是否相等
第一种:使用等号== !=判断
<c:if test="${v==1 && u==2}">
</c:if>
第二种:使用eq neq 判断
<c:if test="${v eq 1 and u eq 2}">
</c:if>
对数值判断相等:可以用==也可以用eq
对数值判断不相等:可以用!=也可以用neq
对字符串判断相等:用eq neq
多个值用&& 或者and 连接
对EL表达式中取到的值进行判断后显示:
<c:if test="${list.status eq 1}">正确<c:if>
<c:if test="${list.status eq 2}">错误<c:if>
总结: 没有获取到值或者值为null 则会返回空字符
JSTL标签
1. 概念:JavaServer Pages Tag Library JSP标准标签库
* 是由Apache组织提供的开源的免费的jsp标签 <标签>
2. 作用:用于简化和替换jsp页面上的java代码
3. 使用步骤:
1. 导入jstl相关jar包
2. 引入标签库:taglib指令: <%@ taglib %>
3. 使用标签
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
4. 常用的JSTL标签
1. if:相当于java代码的if语句
1. 属性:
* test 必须属性,接受boolean表达式
* 如果表达式为true,则显示if标签体内容,如果为false,则不显示标签体内容
* 一般情况下,test属性值会结合el表达式一起使用
2. 注意:
* c:if标签没有else情况,想要else情况,则可以在定义一个c:if标签
2. choose:相当于java代码的switch语句
1. 使用choose标签声明 相当于switch声明
2. 使用when标签做判断 相当于case
3. 使用otherwise标签做其他情况的声明 相当于default
<c:choose>
<c:when test="${val.activityFlag=='1'}">
<a href="javascript:void(0)" class="btn btn-default btn-xs" onclick="YClick('${val.id}',1)">可点击</a>
</c:when>
<c:otherwise>
<a href="javascript:void(0)" class="btn btn-default btn-xs" onclick="YClick('${val.id}',0)">不可点击</a>
</c:otherwise>
</c:choose>
3. foreach:相当于java代码的for语句
<c:forEach var="val" items="${list}" varStatus="status" >
<c:if test="${val.status eq 1}">
<a href="javascript:void(0)" class="btn btn-default btn-xs" onclick="closes('${val.id}')"><span id="close" class="glyphicon glyphicon-play"></span></a>
</c:if>
<c:if test="${val.status ne 1}">
<a href="javascript:void(0)" class="btn btn-default btn-xs" onclick="opens('${val.id}')"><span id="open" class="glyphicon glyphicon-pause"></span></a>
</c:if>
</c:forEach>
EL表达式 与 JSTL标签库的区别
说白了,EL是用来显示数据的,功能跟<%=表达式%> 一样,EL是不需要引入什么东西的
JSTL(JavaServerPages Standard Tag Library)JSP标准标签库
JSTL标准标签库包括核心标签库和SQL标签库,核心标签库常用的是if和forEach
说白了JSTL常实现网页 逻辑判断 和迭代显示的,需要引入
<%@ taglib uri=" http://java.sun.com/jsp/jstl/core " prefix=“c”%>
只要JSTL和EL结合,就基本可以让页面再无<% %> jsp代码了。
MVC:开发模式
1. jsp演变历史
1. 早期只有servlet,只能使用response输出标签数据,非常麻烦
2. 后来又jsp,简化了Servlet的开发,如果过度使用jsp,在jsp中即写大量的java代码,有写html表,造成难于维护,难于分工协作
3. 再后来,java的web开发,借鉴mvc开发模式,使得程序的设计更加合理性
2. MVC:
1. M:Model,模型。JavaBean
* 完成具体的业务操作,如:查询数据库,封装对象
2. V:View,视图。JSP
* 展示数据
3. C:Controller,控制器。Servlet
* 获取用户的输入
* 调用模型
* 将数据交给视图进行展示
* 优缺点:
1. 优点:
1. 耦合性低,方便维护,可以利于分工协作
2. 重用性高
2. 缺点:
1. 使得项目架构变得复杂,对开发人员要求高