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

JSP学习2

2022-04-23 14:05:00 你若信

1、内置对象

<%@ 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 number="number";
        pageContext.setAttribute("number",number);
        //setAttribute方法为指定的元素属性赋值,pageContext为内置对象
        %>
    </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方法接受值
        <%=u%>

        <%--
        out方法
        out.print();输出
        --%>
        <%
        String a="528";
        out.print(a);
        %>
    </body>
    
</html>

2、jsp指令

<%@ 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指令:
            格式:<%@ 指令名称  属性名=属性值  ......%>
            1、page指令:
                        例子1<%@ page contentType="text/html; charset=UTF-8" pageEnconding="UTF-8" %>
                        pageEnconding与contentType都是设置字符集
                        不同的是:pageEnconding默认是text/html,contentType可以设置成其他类型
                        
                        例子2import属性为导入,就可以调用其中的方法
                        (1)<%@ page import="java.util.Date"%>导入util包下的Date
                        (2)<%@ page import="java.util.*"%>导入util包下的所有
                        (3)<%@ page import="java.util.*,java.text.*"%>同时导入两个,以逗号隔开

                        例子3:errorPage属性,当页面发生错误时跳转到指定页面
                        <%@ page errorPage="/error.jsp"%>    其中跳转的页面可以自定义,且在动态中加斜杠

                        例子4:isErrorPage属性,设置是否显示错误类型
                        <%@ page isErrorPage="true"%>
                        <body>
                            <%=exception.getMessage()%>//页面中显示错误类型
                        </body>

            2、include指令:
                        例子1:将其他jsp页面的内容与当前的页面组合,一起显示出来
                        <body>
                            <%@ include file="/error.jsp"%>//将error.jsp的内容显示出来了
                        </body>
                        
                        例子2:include指令可以能使用页面传过来的数据
                        在error.jsp:
                            <body>
                                <%
                                String a="11";
                                %>
                            </body>
                        在自己的当前页面用include指令
                            <body>
                                <%@ include file="/error.jsp"%>
                                <%=a%>//输出error.jsp中的a变量的值,可以输出和使用 
                            </body>

                        例子3:
                        在error.jsp:
                            <body>
                                <%
                                out.println(b);
                                %>
                            </body>
                        在自己的当前页面用include指令
                            <body>
                                <%
                                String b="11";
                                %>
                                <%@ include file="/error.jsp"%>//可以输出b变量的值 
                            </body>
                            
        -----------注意:不能在当前页面和用include指令传过来的jsp页面定义相同的变量名且值不一样,输出时会报错
        --%>
    </body>
    
</html>

版权声明
本文为[你若信]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_44152890/article/details/123940517