前言
只有光头才能变强。
文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y
什么是JSP
JSP全名为Java Server Pages,java服务器页面。JSP是一种基于文本的程序,其特点就是HTML和Java代码共同存在!
为什么需要JSP
JSP是为了简化Servlet的工作出现的替代品,Servlet输出HTML非常困难,JSP就是替代Servlet输出HTML的。
简单使用一下JSP
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>简单使用JSP</title></head><body></body></html>
<html>
<head>
<title>简单使用JSP</title>
</head>
<body>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>简单使用JSP</title></head><body><% String s = "HelloWorld"; out.println(s);%></body></html>
<html>
<head>
<title>简单使用JSP</title>
</head>
<body>
<%
String s = "HelloWorld";
out.println(s);
%>
</body>
</html>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/2f62115b98ab430529edbdc735bab8fa.png)
JSP的工作原理
package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.util.Date;public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List<String> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.List<String> getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } public void _jspDestroy() { } public void _jspService(final HttpServletRequest request, final HttpServletResponse response) throws java.io.IOException, ServletException { final PageContext pageContext; HttpSession session = null; final ServletContext application; final ServletConfig config; JspWriter out = null; final Object page = this; JspWriter _jspx_out = null; 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\n"); out.write("\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write(" <title>简单使用JSP</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); String s = "HelloWorda"; out.println(s); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>\r\n"); } catch (Throwable t) { if (!(t instanceof SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { out.clearBuffer(); } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } }}
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;
public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List<String> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.List<String> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final HttpServletRequest request, final HttpServletResponse response)
throws java.io.IOException, ServletException {
final PageContext pageContext;
HttpSession session = null;
final ServletContext application;
final ServletConfig config;
JspWriter out = null;
final Object page = this;
JspWriter _jspx_out = null;
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\n");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>简单使用JSP</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
String s = "HelloWorda";
out.println(s);
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
out.write("\r\n");out.write("\r\n");out.write("<html>\r\n");out.write("<head>\r\n");out.write(" <title>简单使用JSP</title>\r\n");out.write("</head>\r\n");out.write("<body>\r\n");
out.write("\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write(" <title>简单使用JSP</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
String s = "HelloWorda";out.println(s);
out.println(s);
JSP生命周期
JSP也是Servlet,运行时只有一个实例,JSP初始化和销毁时也会调用Servlet的init()和destroy()方法。另外,JSP还有自己初始化和销毁的方法
public void _jspInit() {_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());}public void _jspDestroy() {}
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
JSP的语法
JSP代码可以分为两部分:
模板数据:就是HTML代码
元素:JSP页面中的java代码、JSP指令、JSP标签
JSP脚本
<jsp:scriptlet> String s = "HelloWorld"; out.println(s);</jsp:scriptlet>
String s = "HelloWorld";
out.println(s);
</jsp:scriptlet>
JSP注释
<%--这是JSP注释--%><%--%>//这是java的当行注释///*这是java的多行注释*//**/
//这是java的当行注释
//
/*这是java的多行注释*/
/**/
JSP指令
JSP指令用来声明JSP页面的相关属性,例如编码方式、文档类型等等
JSP指令的语法:
<%@指令 属性名="值" %>
page指令
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/092614ec6a60256d9930c25927351c9e.jpeg)
<%@ page contentType="application/msword;charset=UTF-8" language="java" %><html><head> <title>简单使用JSP</title></head><body> 1111</body></html>"java" %>
<html>
<head>
<title>简单使用JSP</title>
</head>
<body>
1111
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %><html><head> <title>该页面出错了!</title></head><body> <%--模拟页面出错了!!!--%> <% int result = 2 / 0; %> 你好呀</body></html>"java" errorPage="error.jsp" %>
<html>
<head>
<title>该页面出错了!</title>
</head>
<body>
<%--模拟页面出错了!!!--%>
<%
int result = 2 / 0;
%>
你好呀
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %><html> <head> <title>友好提示页面</title> </head> <body> 服务器正忙着呢! </body></html>"java" isErrorPage="true" %>
<html>
<head>
<title>友好提示页面</title>
</head>
<body>
服务器正忙着呢!
</body>
</html>
![640?wx_fmt=gif](https://i-blog.csdnimg.cn/blog_migrate/aa2a74bc3d51babbee3ef683dc56711c.gif)
<error-page> <error-code>404</error-code> <location>/error.jsp</location></error-page><error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error.jsp</location></error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/error.jsp</location>
</error-page>
![640?wx_fmt=gif](https://i-blog.csdnimg.cn/blog_migrate/12b4a7f231b2d495ab68ff3bdfcce304.gif)
include指令
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head> <title>页头</title> </head> <body> 我是页头 <br> <br> <br> </body></html>"java" %>
<html>
<head>
<title>页头</title>
</head>
<body>
我是页头
<br>
<br>
<br>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>页尾</title></head><body>我是页尾</body></html>"java" %>
<html>
<head>
<title>页尾</title>
</head>
<body>
我是页尾
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>包含页头和页尾进来</title></head><body><%@include file="head.jsp" %><%@include file="foot.jsp" %></body></html>"java" %>
<html>
<head>
<title>包含页头和页尾进来</title>
</head>
<body>
<%@include file="head.jsp" %>
<%@include file="foot.jsp" %>
</body>
</html>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/27665c77652e10ac67666b6d384b079a.png)
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/06ea6043f2c2b544a44b5d53a9ce3347.jpeg)
taglib指令
JSP行为
JSP行为(JSP Actions)是一组JSP内置的标签,只书写少量的标记代码就能够使用JSP提供丰富的功能,JSP行为是对常用的JSP功能的抽象和封装。
为什么我不把它直接称为JSP标签呢?我把这些JSP内置的标签称之为JSP行为,能够和JSTL标签区分开来。当然了,你也可以把它称之为JSP标签,你不要搞混就行了。我个人喜欢把这些JSP内置标签称之为JSP行为。
include行为
<jsp:include page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>包含页头和页尾进来</title></head><body> <jsp:include page="head.jsp"/> <jsp:include page="foot.jsp"/></body></html>"java" %>
<html>
<head>
<title>包含页头和页尾进来</title>
</head>
<body>
<jsp:include page="head.jsp"/>
<jsp:include page="foot.jsp"/>
</body>
</html>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/9e8de5453aa524870ac35a4b80f89f4a.png)
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/d1a3a008f1ec1cbf67e63ee1b6406c8b.jpeg)
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head> <title>页头</title> </head> <body> <% String s = "zhongfucheng"; %> 我是页头呀 <br> <br> <br> </body></html>"java" %>
<html>
<head>
<title>页头</title>
</head>
<body>
<%
String s = "zhongfucheng";
%>
我是页头呀
<br>
<br>
<br>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>页尾</title></head><body><% String s = "zhongfucheng";%>我是页尾呀</body></html>"java" %>
<html>
<head>
<title>页尾</title>
</head>
<body>
<%
String s = "zhongfucheng";
%>
我是页尾呀
</body>
</html>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/c38ecdd09edbeaa0c816253585d19b97.png)
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/0db9b53c6c7323f290a3c07c35500333.jpeg)
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/387544aea362cb31455571d3de33b630.png)
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/684894492e605d9c378d39ca37fb3924.jpeg)
param行为
forward行为
<jsp:forward page=""/>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head> <title>访问1.jsp就跳转到head.jsp</title></head><body><jsp:forward page="head.jsp"/></body></html>"java" %>
<html>
<head>
<title>访问1.jsp就跳转到head.jsp</title>
</head>
<body>
<jsp:forward page="head.jsp"/>
</body>
</html>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/a9f1eeeed87ac64c026e8c78db42eb03.png)
<jsp:forward page="head.jsp"> <jsp:param name="username" value="zhongfucheng"/></jsp:forward>
<jsp:param name="username" value="zhongfucheng"/>
</jsp:forward>
<% String ss = request.getParameter("username");%>获取到的参数是:<%=ss%>"username");
%>
获取到的参数是:
<%=ss%>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/4eaa488fd871b958cce1e99d9f45e721.png)
directive行为
<jsp:directive.include file="head.jsp"></jsp:directive.include><jsp:directive.include file="foot.jsp"></jsp:directive.include>
<jsp:directive.include file="foot.jsp"></jsp:directive.include>
![640?wx_fmt=png](https://i-blog.csdnimg.cn/blog_migrate/738998173cc1d330cb65d65467305230.png)
javaBean行为
<jsp:useBean id=""/><jsp:setProperty name="" property=""/><jsp:getProperty name="" property=""/>
<jsp:setProperty name="" property=""/>
<jsp:getProperty name="" property=""/>
最后
乐于输出干货的Java技术公众号:Java3y。公众号内有200多篇原创技术文章、海量视频资源、精美脑图,不妨来关注一下!
![640](https://i-blog.csdnimg.cn/blog_migrate/4d6c064f75740d9ba93b0ebd0a25a4de.png)