Web Learning History Record (11) - JSP

JSP

overview

The essence of Java server page
jsp is Servlet

Execution principle
jsp will be translated into Servlet (.java). Servlet will be compiled into class file

Process
When accessing a jsp file for the first time, the server receives a request, and jspServlet will search for the corresponding jsp file.
After finding it, the server will convert the jsp file into a java file (Servlet). The
server compiles the java file and generates a class file.
The server runs the class The file generates dynamic content
and the server returns the content to the browser after receiving the content

basic grammar

<% ... %> java program fragment
<%=...%> output expression
<%! ...%> declares member variables

HE

${…}
get data
and perform operation

Get data
${requestScope|sessionScope|applocationScope.property name}

<%--
  Created by IntelliJ IDEA.
  User: 10341
  Date: 2020/9/9
  Time: 9:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<% request.setAttribute("rkey", "rrr");
    session.setAttribute("skey", "sss");
    application.setAttribute("akey","aaa");
%>
获得request里面存的数据:<br/>
<%=request.getAttribute("rkey")%><br/>
${
    
    requestScope.rkey}<br/>
获得session里面存的数据:<br/>
<%=session.getAttribute("skey")%><br/>
${
    
    sessionScope.skey}<br/>
获得application里面存的数据<br/>
<%=application.getAttribute("akey")%><br/>
${
    
    applicationScope.akey}<hr/>
简单写法<br/>
${
    
    rkey}<br/>
${
    
    akey}<br/>
${
    
    skey}<br/>
</body>
</html>

Get the array
${array property name[index]};
where key is the array property name

Get list
${list attribute name[index]} or ${list attribute name.get(index)}
where the attribute name is the key stored in the domain object

Get Map
${map attribute name.key} or ${map attribute name.get(key)}
attribute name is key

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
  Created by IntelliJ IDEA.
  User: 10341
  Date: 2020/9/9
  Time: 19:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    String[] array = {
    
    "aaa","bbb","ccc"};
    request.setAttribute("a",array);

    List<String> list = new ArrayList<>();
    list.add("aaa");
    list.add("bbb");
    list.add("ccc");
    session.setAttribute("l",list);

    Map<String,String> map = new HashMap<String,String>();
    map.put("akey","aaa");
    map.put("bkey","bbb");
    map.put("ckey","ccc");
    request.setAttribute("m",map);
%>

${
    
    a[1]}
${
    
    l[1]}
${
    
    m.akey}
</body>
</html>

Get bean
${bean property name.javabean property}
is a bit like map

The difference between [] and .
As long as you can use ., you can use []
with subscript (array, list) to use []
with special characters to use []

Obtain the values ​​of the three domains,
and only if they are stored in the three domains, they can be obtained. If they are not obtained, return "string, instead of returning null"
$(property name in the domain): search and specify from requestScope|sessionScope|applicationScope in sequence If the attribute is found, it will return immediately and end the search. If it cannot find the attribute, return "" "
If "." "+" "-" and other symbols appear in the attribute name, the quick way to get it is not easy, you must use The following way {xxxScope["property name"]}

JSTL tag library

In order to simplify displaying data on jsp pages, traversing data and judging data

Five major tag libraries,
core tag library,
xml tag library,
internationalization/formatting tag library,
database tag
EL custom function

Guess you like

Origin blog.csdn.net/qq_49658603/article/details/108481871