JSP第一篇【JSP介绍、工作原理、生命周期、语法、指令、行为】(修订版)

 
 

前言

只有光头才能变强。

文本已收录至我的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

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,
                  nulltrue8192true);
      _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代码可以分为两部分:

  1. 模板数据:就是HTML代码

  2. 元素: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
<%@ 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
<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

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
640?wx_fmt=png

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
640?wx_fmt=png
<%@ 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
640?wx_fmt=png
640?wx_fmt=png
640?wx_fmt=png

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
<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

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

javaBean行为

 
 
<jsp:useBean id=""/><jsp:setProperty name="" property=""/><jsp:getProperty name="" property=""/>

<jsp:setProperty name="" property=""/>

<jsp:getProperty name="" property=""/>


最后

乐于输出干货的Java技术公众号:Java3y。公众号内有200多篇原创技术文章、海量视频资源、精美脑图,不妨来关注一下!

640?wx_fmt=jpeg

有帮助?好看!转发! 640


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值