JSP简单学习

jsp是在html中嵌入java代码

jsp也是在j2ee服务端中的java组件

第一次运行

在第一次运行jsp代码时会经历以下步骤,将jsp转为java代码,将java代码转为class文件。

所以通常会比较慢,编译后就好多了。

四大作用域

  • request
  • session
  • page
  • application

部署jsp

xml 复制代码
<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个隐式变量(内置变量)

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

考试重点JspWriter,PageContext

java 复制代码
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;
    }
}
java 复制代码
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类型。

相关推荐
m0_571957581 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
并不会1 小时前
常见 CSS 选择器用法
前端·css·学习·html·前端开发·css选择器
龙鸣丿1 小时前
Linux基础学习笔记
linux·笔记·学习
魔道不误砍柴功3 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python
NiNg_1_2343 小时前
SpringBoot整合SpringSecurity实现密码加密解密、登录认证退出功能
java·spring boot·后端
Nu11PointerException3 小时前
JAVA笔记 | ResponseBodyEmitter等异步流式接口快速学习
笔记·学习
闲晨3 小时前
C++ 继承:代码传承的魔法棒,开启奇幻编程之旅
java·c语言·开发语言·c++·经验分享
测开小菜鸟5 小时前
使用python向钉钉群聊发送消息
java·python·钉钉
P.H. Infinity6 小时前
【RabbitMQ】04-发送者可靠性
java·rabbitmq·java-rabbitmq
生命几十年3万天6 小时前
java的threadlocal为何内存泄漏
java