当前位置:网站首页>JSP learning 2

JSP learning 2

2022-04-23 16:06:00 If you believe

1、 Built-in objects

<%@ page contentType="text/html; charset=UTF-8" pageEnconding="UTF-8" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title> Built-in objects </title>
    </head>
    <body>
        <%
        String number="number";
        pageContext.setAttribute("number",number);
        //setAttribute Method to assign a value to the specified element attribute ,pageContext For built-in objects 
        %>
    </body>
    
</html>

<%@ page contentType="text/html; charset=UTF-8" pageEnconding="UTF-8" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
    </head>
    <body>
        <%String u=(String)pageContext.getAttribute("number",number);%>
        //getAttribute Method acceptance value 
        <%=u%>

        <%--
        out Method 
        out.print(); Output 
        --%>
        <%
        String a="528";
        out.print(a);
        %>
    </body>
    
</html>

2、jsp Instructions

<%@ page contentType="text/html; charset=UTF-8" pageEnconding="UTF-8" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
        <title></title>
    </head>
    <body>
        <%--
        jsp Instructions :
             Format :<%@  Instruction names    Property name = Property value   ......%>
            1、page Instructions :
                         Example 1<%@ page contentType="text/html; charset=UTF-8" pageEnconding="UTF-8" %>
                        pageEnconding And contentType Is to set the character set 
                         The difference is :pageEnconding The default is text/html,contentType Can be set to other types 
                        
                         Example 2import The attribute is import , You can call one of the methods 
                        (1)<%@ page import="java.util.Date"%> Import util Under bag Date
                        (2)<%@ page import="java.util.*"%> Import util Everything under the bag 
                        (3)<%@ page import="java.util.*,java.text.*"%> Import two at the same time , Separated by commas 

                         Example 3:errorPage attribute , When an error occurs on the page, jump to the specified page 
                        <%@ page errorPage="/error.jsp"%>     The jump page can be customized , And add a slash in the dynamic 

                         Example 4:isErrorPage attribute , Set whether to display the error type 
                        <%@ page isErrorPage="true"%>
                        <body>
                            <%=exception.getMessage()%>// The page displays the error type 
                        </body>

            2、include Instructions :
                         Example 1: Will others jsp The content of the page is combined with the current page , Show it together 
                        <body>
                            <%@ include file="/error.jsp"%>// take error.jsp The content of is displayed 
                        </body>
                        
                         Example 2:include Instructions can use the data transmitted from the page 
                         stay error.jsp:
                            <body>
                                <%
                                String a="11";
                                %>
                            </body>
                         Use... On your current page include Instructions 
                            <body>
                                <%@ include file="/error.jsp"%>
                                <%=a%>// Output error.jsp Medium a The value of the variable , Can output and use  
                            </body>

                         Example 3:
                         stay error.jsp:
                            <body>
                                <%
                                out.println(b);
                                %>
                            </body>
                         Use... On your current page include Instructions 
                            <body>
                                <%
                                String b="11";
                                %>
                                <%@ include file="/error.jsp"%>// Can output b The value of the variable  
                            </body>
                            
        ----------- Be careful : Cannot use... On the current page and include Instructions came from jsp The page defines the same variable name and different values , An error will be reported during output 
        --%>
    </body>
    
</html>

版权声明
本文为[If you believe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231404212141.html