JavaWeb-Servlet06

3 篇文章 0 订阅

一. el表达式

el表达式:为了在jsp页面更方便的接受后台传递的数据

发起一个请求,展示学生列表信息

  1. 在java中拼接html
    1. java不要那么麻烦了
  2. 转发到jsp页面
    1. 需要在html中编写java代码

EL就是解决在html中不要再写java代码

package com.qf.servlet;

import com.qf.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = {"/my"})
public class MyServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int age = 18;
        double pi = 3.14;
        String name = "jackma";

        int score = 88;

        Date birthday = new Date();

        double pecent = 0.75;

        Student stu = new Student(1,"pony","0","18655103611","哈弗");

        List<Student> list =new ArrayList<Student>();
        list.add(new Student(1,"pony","1","18655103612","sz"));
        list.add(new Student(2,"tomlei","1","18655103613","wh"));


        req.setAttribute("age",age);
        req.setAttribute("pi",pi);
        req.setAttribute("name",name);
        req.setAttribute("stu",stu);
        req.setAttribute("list",list);
        req.setAttribute("score",score);

        req.setAttribute("birthday",birthday);
        req.setAttribute("pecent",pecent);

        req.getRequestDispatcher("my.jsp").forward(req, resp);
    }

}

页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2022/5/24
  Time: 10:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--jstl核心标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--jstl格式化标签--%>
<%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
<%--jstl 函数--%>
<%@ taglib prefix="fn"
           uri="http://java.sun.com/jsp/jstl/functions" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--
    el表达式:
        ${参数名} 获取参数值
        1. 如果参数值为基本类型,字符串  ${参数名}  直接获取值
        2. 参数为自定义对象  ${参数名.属性名}  获取对象的属性值
        3. 参数为集合   ${参数名[下标]} 获取指定位置元素
--%>
    age:${age}   <br>
    pi : ${pi}   <br>
    name:${name} <br>
    student对象name属性 : ${stu.name} <br>
    list的第一个元素对象phone属性值:${list[0].phone}   <br>

<%--
    可以对获取的到的数据进行运算
    1. 算数运算   + - * / %
    2. 关系运算
    3. 三元表达式
--%>

 age-1:${age-1}   <br>
 age-1 >= 18:${age-1 >=18}   <br>
 name是否为空:${empty name}   <br>
 gender : ${stu.gender == '1'?'男':"女"} <br>

</body>
</html>

二. jstl标签

jstl: JSP 标准标签库,操作通过EL表达式获取到的数据

  • 核心标签

    • 引入标签

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

      <%--
          items: 需要遍历的集合或者数组
          var : 定义变量接受每次遍历出的元素
              在循环中使用${自定义变量} 操作获取的元素
      --%>
      <c:forEach items="${list}" var="student">
          ${student.studentId}, ${student.name}  <br>
      </c:forEach>
      
    • 分支结构

      <%--判断分数的等级
          90-100 A
          60-80  B
          0-60   C
        --%>
      <c:choose>
          <c:when test="${score>90}">A</c:when>
          <c:when test="${score>60}">B</c:when>
          <c:otherwise>C</c:otherwise>
      </c:choose>    <br>
      
      
      <c:if test="${score>60}">
          及格
      </c:if>
      
  • 格式化标签

    • 引入标签

      <%@ taglib prefix="fmt"  uri="http://java.sun.com/jsp/jstl/fmt" %>
      
    • 日期格式化

      <fmt:formatDate type="both"
                      dateStyle="long" timeStyle="long"
                      value="${birthday}" />
      
    • 数字格式化

      <fmt:formatNumber type="percent"
                        maxIntegerDigits="3" value="${pecent}" />
      
  • JSTL 函数

    • 引入标签

      <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
      
    • 操作字符串

      <%--jstl函数   操作字符串--%>
      
      <c:if test="${fn:contains(name, 'jack')}">
          包含jack
      </c:if>
      

使用流程:

  1. 添加jstl包

    jstl.jar
    standard.jar

  2. 在jsp页面添加需要使用jstl标签

三. 欢迎页面

当我们请求我们部署的项目,后面没有跟任何请求路径时,展示的页面的就叫做欢迎页面

默认:web/index.jsp

可以在web.xml中定义欢迎页面

    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

四. 保持回话状态

http请求是没有状态请求

让服务器知道当前用户是谁,就叫做保持回话状态

办会员:

卡是超市

​ 记录你的信息

给你

下次来的时候,你要把卡带着

超市通过刷卡,知道你是谁

1. cookie

cookie是在服务器(java后台)创建的

​ 把当前用户的信息,记录在cookie上

​ 把cookie相应给浏览器
下一次浏览器向服务器发起请求,会把cookie自动带着,在后可以通过request对象获取。

package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/cookie01"})
public class CookieServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
            1. cookie在服务器创建的
            2. 记录当前用户信息
            3. 给浏览器
         */
        Cookie cookie = new Cookie("phone","13912345678");
        Cookie cookie2 = new Cookie("name","xsg");

        resp.addCookie(cookie); //给浏览器
        resp.addCookie(cookie2); //给浏览器

        req.getRequestDispatcher("cookie01.jsp").forward(req, resp);
    }
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
  this is cookie01.jsp
</body>
</html>
package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/cookie02"})
public class Cookie02Servlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取来自浏览器的cookie
        Cookie[] cookies = req.getCookies();
        for(Cookie cookie : cookies) {
            String name = cookie.getName();
            String value = cookie.getValue();
            System.out.println(name+":"+value);
        }

        req.getRequestDispatcher("cookie01.jsp").forward(req, resp);
    }
    
}

2. session

session也是保持回话状态的一门技术

当客户端(浏览器)第一次向服务器发起http请求时,web服务器会创建一个HttpSession对象方法web容器,这个HttpSession对象的唯一标识(id)会以cookie的形式传递浏览器。(值得name为JSESSIONID,value为session的id属性),当浏览器再次向服务器发起请求时,服务器会接受此cookie值,然后在web容器中找有没有此id的HttpSession对象,如果有的话就继续使用该对象,如果没有的话,那就立马创建一个新的HttpSession对象,然后把id以cookie的形式传给浏览器…

package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/session01"})
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取session对象(web容器中)
        HttpSession session = req.getSession();
        System.out.println(session.getId());

        //保存信息
        session.setAttribute("name", "xsg");

        req.getRequestDispatcher("session01.jsp").forward(req, resp);
    }

}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<a href="session02"> session02</a>
</body>
</html>
package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = {"/session02"})
public class Session02Servlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session对象(web容器中)
        HttpSession session = req.getSession();
        System.out.println(session.getId());

        String name = (String) session.getAttribute("name");

        System.out.println(name);

        req.getRequestDispatcher("session01.jsp").forward(req, resp);
    }
}

3. cookie和session的区别(保存位置,保存类型,保存大小)

  1. cookie保存在客户端

  2. session保存在服务端

  3. cookie只能保存字符串类型数据

  4. session可以保存任意类型数据

  5. cookie保存数据大小有限制

  6. session保存数据没有限制

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值