JSP简单学习

发布时间:2024年01月18日

jsp是在html中嵌入java代码
jsp也是在j2ee服务端中的java组件

第一次运行

在第一次运行jsp代码时会经历以下步骤,将jsp转为java代码,将java代码转为class文件。
所以通常会比较慢,编译后就好多了。

四大作用域

  • request
  • session
  • page
  • application

部署jsp

<servlet>
    <servlet-name>a<servlet-name/>
    <jsp-file>/a.jsp<jsp-file/>
    <servlet-mappiing>
        <servlet-name><servlet-name/>
        <url-pattern><url-pattern/>
    <servlet-mappiing/>
<servlet/>

jsp作为servlet的继承者,自然是可以部署的,但是一般不会这么做。

内嵌java语法

  • <%@ %>jsp指令的语法
  • <%= %>jsp表达式的语法
  • <%! %>jsp声明的语法
  • <%-- --%>jsp注释的语法
  • <% %>jsp中可以放置任何合法的java代码,其最终会被转换到service代码内。

jsp指令

  • <%@ page 属性列表%> 定义页面的依赖属性,如脚本语言,error页面,缓存需求等
  • <%@ include file="afile" %> 包含其它文件
  • <%@ taglib uri="" prefix="a" %> 标签库的定义

例子

<%@ page import="java.util.*,java.net.*,java.io.*" session="true" isErrorPage="false" errorPage="/error.jsp"%>
<%@ include file="copyright.html" %>

Jsp声明

声明函数或变量,供后续使用。

<%! int i = 0;%>
<%! Circle a = new Circle(2.0);%>

或者

<jsp:declaration>
    int i = 0;
<jsp:declaration/>

Jsp的action行为标签

在这里插入图片描述
在这里插入图片描述

9个隐式变量(内置变量)

HttpServletRequest request
HttpServletEesponse response
ServletContext application
PageContext pagecontext
ServletConfig config
HttpSession session
JspWriter out
Exception exception
page,等价于this

考试重点JspWriter,PageContext

package javax.servlet.jsp

public abstract class JspWriter extends Writer {
    public static final int NO_BUFFER = 0;
    public static final int DEFAULT_BUFFER = -1;指示 Writer 已缓冲并使用实现默认缓冲区大小的常量。
    public static final int UNBOUNDED_BUFFER = -2;常量表示 Writer 是缓冲的并且是无界的;这在 BodyContent 中使用。
    protected int bufferSize;
    protected boolean autoFlush;

    protected JspWriter(int bufferSize, boolean autoFlush) {
        this.bufferSize = bufferSize;
        this.autoFlush = autoFlush;
    }

    public abstract void newLine() throws IOException;

    public abstract void print(boolean var1) throws IOException;

    public abstract void print(char var1) throws IOException;

    public abstract void print(int var1) throws IOException;

    public abstract void print(long var1) throws IOException;

    public abstract void print(float var1) throws IOException;

    public abstract void print(double var1) throws IOException;

    public abstract void print(char[] var1) throws IOException;

    public abstract void print(String var1) throws IOException;

    public abstract void print(Object var1) throws IOException;

    public abstract void println() throws IOException;

    public abstract void println(boolean var1) throws IOException;

    public abstract void println(char var1) throws IOException;

    public abstract void println(int var1) throws IOException;

    public abstract void println(long var1) throws IOException;

    public abstract void println(float var1) throws IOException;

    public abstract void println(double var1) throws IOException;

    public abstract void println(char[] var1) throws IOException;

    public abstract void println(String var1) throws IOException;

    public abstract void println(Object var1) throws IOException;

    public abstract void clear() throws IOException;

    public abstract void clearBuffer() throws IOException;

    public abstract void flush() throws IOException;

    public abstract void close() throws IOException;

    public int getBufferSize() {
        return this.bufferSize;
    }

    public abstract int getRemaining();此方法返回缓冲区中未使用的字节数。

    public boolean isAutoFlush() {
        return this.autoFlush;
    }
}
package javax.servlet.jsp

public abstract class PageContext extends JspContext {
    public static final int PAGE_SCOPE = 1;
    public static final int REQUEST_SCOPE = 2;
    public static final int SESSION_SCOPE = 3;
    public static final int APPLICATION_SCOPE = 4;
    public static final String PAGE = "javax.servlet.jsp.jspPage";
    public static final String PAGECONTEXT = "javax.servlet.jsp.jspPageContext";
    public static final String REQUEST = "javax.servlet.jsp.jspRequest";
    public static final String RESPONSE = "javax.servlet.jsp.jspResponse";
    public static final String CONFIG = "javax.servlet.jsp.jspConfig";
    public static final String SESSION = "javax.servlet.jsp.jspSession";
    public static final String OUT = "javax.servlet.jsp.jspOut";
    public static final String APPLICATION = "javax.servlet.jsp.jspApplication";
    public static final String EXCEPTION = "javax.servlet.jsp.jspException";

    public PageContext() {
    }

    public abstract void initialize(Servlet var1, ServletRequest var2, ServletResponse var3, String var4, boolean var5, int var6, boolean var7) throws IOException, IllegalStateException, IllegalArgumentException;

    public abstract void release();

    public abstract HttpSession getSession();

    public abstract Object getPage();页面对象的当前值(在 Servlet 环境中,这是 javax.servlet.Servlet 的实例)。

    public abstract ServletRequest getRequest();请求对象 (ServletRequest) 的当前值。

    public abstract ServletResponse getResponse();响应对象 (ServletResponse) 的当前值。

    public abstract Exception getException();

    public abstract ServletConfig getServletConfig();

    public abstract ServletContext getServletContext();

    public abstract void forward(String var1) throws ServletException, IOException;

    public abstract void include(String var1) throws ServletException, IOException;

    public abstract void include(String var1, boolean var2) throws ServletException, IOException;

    public abstract void handlePageException(Exception var1) throws ServletException, IOException;

    public abstract void handlePageException(Throwable var1) throws ServletException, IOException;

    public BodyContent pushBody() {
        return null;
    }

    public ErrorData getErrorData() {
        return new ErrorData((Throwable)this.getRequest().getAttribute("javax.servlet.error.exception"), (Integer)this.getRequest().getAttribute("javax.servlet.error.status_code"), (String)this.getRequest().getAttribute("javax.servlet.error.request_uri"), (String)this.getRequest().getAttribute("javax.servlet.error.servlet_name"));
    }
}

JSP EL表达式

<%
    request.setAttribute("a","hello")
%>
${a}

会在四大作用域中依次搜索a这个键名。
显示hello

变量定义使用setAttribute()方法进行设置,可以设置在四大作用域中的任意一个域中。

预定义11个对象

为了方便使用,定义了可以直接供我们使用的11个对象。
大多是都是用Map存储,采用键值对的形式。

  • pageContext
  • param
  • paramValues
  • header
  • headerValues
  • cookie
  • initParam
  • pageScope
  • requestScope
  • sessionScope
  • applicationScope

只有pageContext是PageContext类型。

文章来源:https://blog.csdn.net/m0_63288666/article/details/135661822
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。