JSP JavaWeb learning (b)

    JSP commonly used tags 

      What is JSP tag

       JSP tags, some places also called JSP actions, write a lot of java code JSP page will appear disorganized, it looks very uncomfortable in the JSP, JSP therefore provides some similar html tags, these tags can replace some java code realize the function, the use of a large number of JSP tags can simplify our code more readable code.

<Jsp: tag name attribute value attribute name = ... />
or
<Jsp: tag name attribute value attribute name = ...> </ jsp: tag name>

      Two common JSP tag

     <jsp:forward>

     The role of the label is to forward the request to another resource, then use the instruction page, all the content to be displayed in the current page will not be displayed, because it will be forwarded directly to another page.

     First create test.jsp page, on this page we use JSP built-in object request to pass parameters to set.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
        test.jsp page, since the label forward, so the content of the page will not be displayed
        <%
            request.setAttribute("name", "henu scm");
        %>
        <jsp:forward page="/forwardPage.jsp"/>
</body>
</html>

       Then create forwardPage.jsp , also uses built-in object parameter request is received in test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body> 
    JSP: Forward jump to a tag target page <br>
    <%=
        request.getAttribute("name")
    %>
</body>
</html>

      Access test.jsp, page output:

        jsp: forward tag jump target page
        henu scm

      note:

      1, in test.jsp page JSP tags to write complete (be careful not to forget to write an end tag), otherwise it will throw an exception org.apache.jasper.JasperException.

      2, the tag is not used to redirect JSP tags.

      3, it is seen from the above example, it is written in <jsp: forward> tag in the content of the page is not displayed.

      <jsp:include>

     The label is used to introduce additional inside a JSP file to the current, this method is called dynamic incorporated incorporated.
      Creating a page index.jsp
     

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int sum = 100;
    %>
    <br>
    <jsp:include page="/includePage.jsp"/>
    <br>
    index sum=<%= sum %>
</body>
</html>

      Then create a includePage.jsp page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int sum = 50;
    %>
    <br>
    includePage sum=<%= sum %>
</body>
</html>

     Access index.jsp page, output:

        includePage sum=50 
        index sum=100 

     Open the resulting file can see the directory java, jsp generates two files, namely index_jsp.java and left_jsp.java

     It can be concluded that the introduction of the directive is done at runtime rather than at compile time. The introduction of this instruction, in the program is running, the file in the _jspService index.jsp () method include JspRuntimeLibrary class () method calls the _jspService () method includePage.jsp file. In this run-performed the introduction, called dynamic introduction.

     Note where the <jsp: include> tag to the JSP directive < % @ the include File = "/ filename"%> separately, do not be confused.

    The difference between dynamic and static introduced introduced

<% @ Include file = "/ xxx.jsp"%> <-! Static introduced ->

     Static introduced will generate a java file, two jsp files can share the same variable, but can not define the same name variables.     

<Jsp: include page = "/ xxx.jsp" /> <- Dynamic introduced! ->

 

     Introducing two dynamically generated java file, two files jsp not share the same variable, the variable can be defined the same name.

     When the static and dynamic introduction of the introduction can be used, generally use static introduced. Because there is only a Servlet the program is running, less consumption of resources, and there are no problems calling and high efficiency.

     

Guess you like

Origin www.cnblogs.com/scm2019/p/11642233.html