javaweb入门基础知识jsp

根据自学网整理笔记


1.jsp指令

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="index.jsp" %>
<%@ taglib uri="xxx" prefix="c"%>

2.jsp的九大隐式对象

名称 类型 描述
out JspWriter 页面输出
request HttpServletRequest 共享一次请求中的信息
response HttpServletResponse 服务器向浏览器回应信息
config ServletConfig 服务器配置,可以取得初始化参数
session HTTPSession 会话信息共享
application ServletContext 所有用户的共享信息
page Object 指当前页面转换后的Servlet类的实例
pageContext pageContext JSP的页面容器 上下文的对象
exception Throwable 在错误页面中才起作用

out.write(); = response.getWriter().write();

pageContext对象只在当前jsp页面有效

pageContext.setAttribute("name","xiaohong");
pageContext.setAttribute("name","xiaohong",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("name","xiaohong",PageContext.SESSION_SCOPE);
pageContext.setAttribute("name","xiaohong",PageContext.APPLICATION_SCOPE);

pageContext.getAttribute("name", PageContext.SESSION_SCOPE);
//findAttribute会依次从PageContext域,request域,session域,application域中获取属性。
pageContext.findAttribute("name");

3.jsp标签

常用jsp标签

页面动态包含:
<jsp:include page="hello.jsp"></jsp:include>
请求转发:
<jsp:forward page="hello.jsp"></jsp:forward>

4.EL表达式获取域中数据

EL全名为Expression Language。它提供了在JSP中简化表达式的方法,让Jsp的代码更加简化,可以嵌入在jsp页面内部,减少jsp脚本的编写

从域中获取数据
${requestScope.name}
${sessionScope.user.name}
${applicationScope.list[0].name}

//会依次从PageContext域,request域,session域,application域中获取值。
${key}

4.1 EL内置对象

获取jsp中域中的数据
requestScope
sessionScope
applicationScope
pageScope

pageContext WEB开发中的pageContext
pageContext获取其他八大对象


不常用
param,paramValues 接收参数
相当于request.getParameter() request.getParameterValues()

header,headerValues 获取请求头信息
相当于request.getHeader(name)

cookie WEB开发中cookie
相当于request.getCookies()–>cookie.getName()—>cookie.getValue()

initParam 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)

4.2 EL的执行表达式

基本运算,三元都支持
1 + 1 {empty user}判断对象是否为空

5 JSTL

JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在isp页面中使用标签的形式完成业务逻辑等功能。jst1出现的目的同EL一样也是要代替jsp页面中的脚本代码。JSTL标准标准标签库有5个子库,但随着发展,目前常使用的是他的核心库。
这里写图片描述

*********if
//使用jstl表达式的时候往往需要EL表达式的配合
<c:if test="${1+1==2}">
//若test为true 则会执行内部的代码,反之false不执行
</c:if>

**********forEach
<c:forEach begin="0" end"${list.size()}" var="i">
    ${i}
</c:forEach>

<c:forEach items="${list}" var="usre">
    ${user.name}
</c:forEach>

猜你喜欢

转载自blog.csdn.net/weizhengzhou520/article/details/82153024