JSP

JSP

动态网页技术

  • 静态网页 .html 网页一旦制作完成,内容不变
  • 动态网页 .jsp .asp .net 网页内容可以进行动态渲染

1.1.JSP简介

  • JSP(java server page)
  • JSP和servlet一样是Java EE的组件
  • JSP文件内容包含HTML+JAVA代码
  • JSP本质是一个Java类 而且是一个servlet;

1.2.JSP的语法

  • jsp 中使用Java代码 都是以<%开头 以%>结束

1.2.1.变量声明和使用

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--变量的声明 --%>
<%
    int a = 10;
    System.out.println(a);
%>

<%-- 方法的声明--%>
<%!
    public void add1(int a, int b) {
        System.out.println(a + b);
    }

    public int add2(int a, int b) {
        return a + b;
    }
%>

<%-- 取变量的值  --%>
a的值是  <%=a%> <br>
方法的调用 <% add1(10, 20);%> <br>
方法的调用有返回值: <%=add2(10, 20)%> <br>
</body>
</html>

1.2.2.指令

​ 1.page 指令 用来指定网页的基本属性

​ 2.include指令 把其他的JSP网页引入进来

​ 3.taglib 有关JSTL标签的使用

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

1.3.JSP九大内置对象四大作用域

1.3.1.内置对象

request 请求对象

response 响应对象

session session对象

application 全局的应用对象

out对象 输出到浏览器

pageContext对象 当前的page域

config 配置对象

page 表示JSP本身,相当于this

exception对象 异常信息类

<%
    String method = request.getMethod();
    String username = request.getParameter("username");
    response.getWriter().write("hello world");
    request.getRequestDispatcher("/b.jsp").forward(request, response);
    response.sendRedirect("/b.jsp");
    session.setAttribute("user", "1");
    //ServletContext
    application.setAttribute("user", "1");
    out.print("hello");
    pageContext.setAttribute("", "");
    ServletConfig servletConfig = pageContext.getServletConfig();
    String servletName = config.getServletName();
    
%>

1.3.2.四种作用域(域对象)

pageContext

request

session

application

案例:

a.jsp

<body>
<%
    pageContext.setAttribute("page", "page");
    request.setAttribute("req", "request");
    session.setAttribute("sess", "session");
    application.setAttribute("app", "application");
    //请求转发 ,
//    request.getRequestDispatcher("/b.jsp").forward(request, response);
    response.sendRedirect("/b.jsp");
%>
这是 a.jsp内容
</body>

b.jsp

<body>
page 域的值 : <%=pageContext.getAttribute("page")%> <br>
request 域的值 : <%=request.getAttribute("req")%> <br>
session 域的值 : <%=session.getAttribute("sess")%> <br>
app 域的值 : <%=application.getAttribute("app")%>
</body>

1.4.JSP与servlet间传值

​ 1.servlet代码

@WebServlet("/demo")
public class DemoServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("req", "hello");
        HttpSession session = req.getSession();
        session.setAttribute("sess", "world");

        getServletContext().setAttribute("app", "content");
        req.getRequestDispatcher("/b.jsp").forward(req, resp);
    }
}

​ 2.JSP 代码

<body>
page 域的值 : <%=pageContext.getAttribute("page")%> <br>
request 域的值 : <%=request.getAttribute("req")%> <br>
session 域的值 : <%=session.getAttribute("sess")%> <br>
app 域的值 : <%=application.getAttribute("app")%>
</body>

1.5.EL表达式

  • EL (expression language) 表达式语言
  • 主要作用是从域对象取值显示到页面中
  • EL表达式中可以使用运算符,结合JSTL标签使用

内置对象

pageScope

requestScope

sessionScope

applicationScope

<%
    Student tom = new Student("tom", 12);
    pageContext.setAttribute("stu", tom);
%>
<%-- EL 的内置对象 EL主要作用就是从域对象中取值 --%>
page 域的值 : ${pageScope.get("page")} <br>
<%-- 三种方式 get方法 中括号 点操作符 --%>
request 域的值 : ${requestScope.get("req")} <br>
session 域的值 : ${sessionScope["sess"]} <br>
session 域的值 : ${sessionScope.sess} <br>
app 域的值: ${applicationScope.app} <br>

<%--  --%>
学生信息 姓名: ${stu.getName()} <br>
学生信息 年龄: ${stu.getAge()} <br>

1.6.JSTL标签

  • El表达式主要是取值和输出功能,需要配合jstl标签来完成逻辑控制(条件判断、循环)
  • JSTL(JavaServerPages Standard Tag Library) jsp基本标签库,主要用来替换页面中跟逻辑判断相关的java代码,经常和el表达式配合使用

1.6.1.使用步骤

​ 1.jar包的引入

在WEB-INF目录下新建一个文件夹lib,把jstl.jar和standard.jar加入进去,lib转为库

​ 2.指令的添加 taglib指令

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

​ 3.if标签的使用

<body>
<%
    Student tom = new Student("tom", 19);
    pageContext.setAttribute("stu", tom);
%>

<%--搜索顺序是 page-->request-->session-->application--%>
学生信息 姓名: ${stu.getName()} <br>
学生信息 年龄: ${stu.getAge()} <br>

<%-- --%>
<c:if test="${stu.getAge()>18}">
    <h1>学生成年了</h1>
</c:if>

</body>

​ 4.循环标签的使用,页面展示学生信息

<table align="center" width="300" hight="200" border="1" cellspacing="0">
    <tr>
        <th>姓名</th>
        <th>年龄</th>
    </tr>
    <c:forEach items="${requestScope.stus}" var="stu">
        <tr>
            <td>${stu.getName()}</td>
            <td>${stu.getAge()}</td>
        </tr>
    </c:forEach>
</table>

​ servlet 代码

  @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student tom = new Student("tom", 12);
        Student jerry = new Student("jerry", 13);
        Student jason = new Student("jason", 15);
        Student tim = new Student("tim", 16);

        ArrayList<Student> students = new ArrayList<>();
        students.add(tom);
        students.add(jerry);
        students.add(jason);
        students.add(tim);
        req.setAttribute("stus", students);
        req.getRequestDispatcher("/b.jsp").forward(req, resp);
       }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值