JSP基础及el表达式和JSTL表达式

什么是JSP?

全称 Java Server Page 中文名 JAVA服务器页面

由Sun Microsystems公司倡导 许多公司参与一起建立的一种动态网页技术标准

JSP 原理

访问jsp页面时 jsp页面会被翻译成.java文件

​ 然后 .java会被编译成 .class文件(字节码文件)

  在网页中显示当前时间
<%@ page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        // 正常写 Java 代码

        Date date = new Date();
        out.print(date.toLocaleString());
        // 被废弃的方法,使用仅为测试
    %>
body>
html>
  实现登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <form action="/Demo/dologin.jsp" method="post">
        账号:<input type="text" name="username">
        <br>
        密码:<input type="password" name="password">
        <br>
        <input type="submit" value="登录">
        <br>
        <%
            String msg = (String)request.getAttribute("msg");
            if(msg != null){
                out.print(msg);
            }
        %>
    form>
body>
html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        // 获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 处理逻辑
        if(username.equals("test") && password.equals("123")){
            // 登录成功
            // 跳转页面 -> 欢迎页面,并且显示名字
            // 传值时,如果是非表单提交的数据,可以保存在域对象中
            //      如果是表单提交过来的数据,直接使用请求转发就行了
            request.getRequestDispatcher("/success.jsp").forward(request, response);
        } else {
            // 登录失败 返回登录页面
            // 相对于服务器内,不用添加工程名,例如:请求转发
            // 相对于服务器外(相对于网址的8080后),需要添加工程名,例如:请求重定向
            // 失败后,在页面中显示错误信息
            // 使用请求转发,比较合适
            request.setAttribute("msg", "登录失败,请确认用户名和密码!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        // 跳转页面
    %>
body>
html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
    %>
    <h1>欢迎你  
        <% 
            // String username = (String)session.getAttribute("username");
            String username = request.getParameter("username"); 
            out.write(username);
        %>
    h1>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>


<%@ page session="true" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%-- jsp注释:安全效率高 --%>
    


    
    <%!
        // 加上叹号会被翻译到类的下面,即为全局变量
        // 还可以声明方法,静态块
        // 可以这么用,但是一般没人这么去写!
        int num2 = 10;
    %>
    <%
        // 测试session开关
        session.getAttribute("123123");

        int num1 = 10;
        num1++;
        num2++;
        // out.print(num1);
    %>
    
    
    <%=num1+2 %>
    <%=num2 %>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page errorPage="/error.jsp" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        int num = 10 / 0;
    %>
body>
html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ page isErrorPage="true" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        String msg = exception.getMessage();
        out.print(msg);
    %>
    服务器正在紧张的建设当中...请稍后重试
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%--@ include file="/4.jsp" --%>


<%-- "/4.jsp"> --%>


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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <c:if test="">c:if>
    bbbbbb
body>
html>


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    aaaaaaaa
body>
html>
<%@ page import="Demo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


"Content-Type" content="text/html; charset=UTF-8">
Insert title here


    <%
        // 创建一个User对象
        User user = new User();
        user.setName("wanglong");
        user.setPwd("123");
        out.print(user.getName());
    %>
    
"user1" class="Demo.User"> <%-- "user1" class="Demo.User"/> --%> "name" name="user1" value="dingpeng" /> "pwd" name="user1" value="456" /> <%=user1 %>
"name" name="user1"/> "pwd" name="user1"/> public class User { private String name; private String pwd; public User() { super(); } public User(String name, String pwd) { super(); this.name = name; this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User [name=" + name + ", pwd=" + pwd + "]"; } }

pageContext 域

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>转发title>
    head>
    <body>
        
        <%
            /* 该域对象,只在本页面有效,出了页面就获取不到 */
            pageContext.setAttribute("page", "pageContext");
        %>
        <%=pageContext.getAttribute("page") %>

        


        <%-- 携带请求的参数(不要在这中间加注释!编译不出来!) --%>
        <jsp:forward page="/2.jsp">
            <jsp:param value="test" name="username"/>
            <jsp:param value="123" name="password"/>
        jsp:forward>
    body>
html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>title>
    head>
    <body>
        
        <%=pageContext.getAttribute("page") %>
        
        <%-- <%=request.getAttribute("username") %> --%>
        
        <%=request.getParameter("username") %>
        <%=request.getParameter("password") %>
    body>
html>

el表达式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <%
        /* 一般系统方法中,需要一个 int 参数,一般会是常量 */
        /* 只要有 pageContext 就可以向各个域中添加值,参数三表示对应域的常量 */
        // pageContext.setAttribute("page", "pageContext", PageContext.PAGE_SCOPE);
        // pageContext.setAttribute("page", "request", PageContext.REQUEST_SCOPE);
        // pageContext.setAttribute("page", "session", PageContext.SESSION_SCOPE);
        pageContext.setAttribute("page", "application", PageContext.APPLICATION_SCOPE);
    %>
    
    <%-- =pageContext.findAttribute("page") --%>

    
    
    ${page }
body>
html>
<%@page import="com.lanou3g.Address"%>
<%@page import="com.lanou3g.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <%
        User user = new User();
        user.setUsername("test");
        user.setPassword("123");
        request.setAttribute("u", user);
        // 添加地址对象
        Address address = new Address();
        address.setCity("zh");
        user.setAddress(address);
    %>
    
    
    ${u.username }
    
    ${u.address.city }

    
    
    
    ${u["username"] }
    ${u['username'] }
body>
html>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <%
        ArrayList arrayList = new ArrayList<>();
        HashMap<String,ArrayList> hashMap = new HashMap<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        request.setAttribute("list", arrayList);
        hashMap.put("a", arrayList);
        hashMap.put("b", arrayList);
        hashMap.put("c", arrayList);
        request.setAttribute("map", hashMap);
    %>
    
    ${list }
    ${list[0] }
    ${map }
    ${map["a"] }
body>
html>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <%
        String str1 = null;
        request.setAttribute("str1", str1);
        String str2 = "";
        request.setAttribute("str2", str2);

        ArrayList<String> list1 = new ArrayList<>();
        list1.add("aaa");
        ArrayList<String> list2 = new ArrayList<>();
        ArrayList<String> list3 = null;
        ArrayList<String> list4 = new ArrayList<>();
        list1.add("");
        request.setAttribute("list1", list1);
        request.setAttribute("list2", list2);
        request.setAttribute("list3", list3);
        request.setAttribute("list4", list4);

        /* 测试三木运算符 */
        String sex = "男";
        request.setAttribute("sex", sex);
    %>
    
    ${empty str1 }
    ${empty str2 }
    
    ${empty list1 }
    ${empty list2 }
    ${empty list3 }
    ${empty list4 }
    
    ${empty list3 ? "前面" : "后面" }

    
    <br><input type="radio" value="男" name="sex" ${sex=="男" ? "checked='checked'" : "" }>男
    
radio"
value="女" name="sex" ${sex=="女" ? "checked='checked'" : "" }>女

隐式对象

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <%
        pageContext.setAttribute("page", "pageContext", PageContext.PAGE_SCOPE);
        pageContext.setAttribute("page", "request", PageContext.REQUEST_SCOPE);
        pageContext.setAttribute("page", "session", PageContext.SESSION_SCOPE);
        pageContext.setAttribute("page", "application", PageContext.APPLICATION_SCOPE);
    %>
    ${pageScope.page }
    ${requestScope.page }
    ${sessionScope.page }
    ${applicationScope.page }
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    ${www }
    
    <form action="${pageContext.request.contextPath }/9.jsp" method="post">
            账号:<input type="text" name="username">
        <br>密码:<input type="password" name="password">
        <br><input type="checkbox" name="hobby" value="唱歌">唱歌
            <input type="checkbox" name="hobby" value="跳舞">跳舞
        <br><input type="submit" value="提交">
    form>
body>
html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <% 
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
    %>
    
    
    ${param.username }
    ${paramValues.hobby[0] }<br>
    
    ${header["User-Agent"] }<br>
    
    ${cookie.JSESSIONID }<br>
    ${cookie.JSESSIONID.value }<br>
body>
html>

JSTL 表达式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    
    

    
    <c:set var="num" value="10" scope="session">c:set>
    
    <c:out value="${num }">c:out> 
    <c:out value="${num }" default="aaa">c:out>
    
    <c:remove var="num" scope="session"/>
    ${num }
body>
html>

逻辑表达式

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    
    <c:set var="n" value="10">c:set>
    
    <c:if test="${n > 4 }">
        aaa
    c:if>
    <c:if test="${n < 4 }">
        bbb
    c:if>
    <c:choose>
        <c:when test="${n == 5 }">5c:when>
        <c:when test="${n < 5 }">10c:when>
        <c:otherwise>
            不知道
        c:otherwise>
    c:choose>
body>
html>

循环

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
    <%
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        list.add("eee");
        request.setAttribute("list", list);
    %>
    
    <c:forEach var="i" begin="0" end="10" step="3">
        ${i }<br>
    c:forEach>
    
    <c:forEach items="${list }" var="j" step="2">
        ${j }<br>
    c:forEach>
    <c:forEach items="${list }" var="l" varStatus="vs">
        <tr>
            <td>${l }td>
            <td>${vs.index }td>
            <td>${l }td>
            <td>${l }td>
            <td>${l }td>
        tr>
    c:forEach>
body>
html>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
<style type="text/css">
    body{
        background: purple;
        text-align: center;
        color: white;
    }
    table{
        margin: 0 auto;
    }
style>
head>
<body>
    <%
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("ddd");
        list.add("eee");
        request.setAttribute("list", list);
    %>
    <table border="1">
        <tr>
            <td width="100">td>
            <td width="100">索引td>
            <td width="100">计数td>
            <td width="100">第一个td>
            <td width="100">最后一个td>
        tr>
        <c:forEach items="${list }" var="l" varStatus="vs">
            <tr>
                <td>${l }td>
                <td>${vs.index }td>
                <td>${vs.count }td>
                <td>${vs.first }td>
                <td>${vs.last }td>
            tr>
        c:forEach>
    table>
body>
html>

pageContext

// 查看 pageContext 中的方法
public void fun(PageContext pageContext) {
    // 强大的 pageContext 可以获取任何域对象
    // 从小到大
    pageContext.getRequest();
    pageContext.getSession();
    pageContext.getServletContext();
    pageContext.getServletConfig();
    // 强大的方法(el 表达式就依赖这个方法)
    // 该方法会搜索所有域对象,用 key 去取值
    // 按域的大小,从小到大进行搜索,找到就输出对应的 value
    // request < session < context 这个顺序搜索
    pageContext.findAttribute("key");
}

你可能感兴趣的:(JAVA)