JSP隐式对象

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/2
  Time: 0:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page errorPage="error.jsp" %>
<html>
<head>
    <title>exception 隐式对象</title>
</head>
<body>
    <%!
       int div = 0;
       int res = 0;
    %>
    <%
        res = 23 / div;
    %>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/2
  Time: 0:03
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>error</title>
</head>
<body>
    出错了<br>
    <%=
        exception.getMessage()
    %>
    <%
        exception.printStackTrace();
    %>
</body>
</html>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page pageEncoding="UTF-8" buffer="16kb" %>
<%-- 使用taglib 指令设置引入自定义标签信息 --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%@ include file="view/top.jsp" %>
  <%-- 声明变量定义方法 --%>
  <%!
    String mess = "";//
    void sayHello(String name,String sex){
      if (sex.equals("男"))
        mess = "你好!"+name+" 先生";
      else
        mess = "你好!"+name+" 女士";
    }
  %>
  <%
    sayHello("King","男");
    Date dat = new Date();
    SimpleDateFormat sim = new SimpleDateFormat();
  %>
  <%=mess%>

  <c:if test="${17 >=18}">
    <h2>您是成年人</h2>
  </c:if>

  <c:if test="${17 < 18}">
    <h2>你是未成年人</h2>
  </c:if>

  </body>
</html>

 

 

<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录界面</title>
</head>
<body>
    <form action="doLogin.jsp" method="post">
        <input name="userName" type="text"/><br>
        <input name="password" type="password"/><br>
        <input type="submit" value="登录">
    </form>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 15:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="com.xzit.pojo.Users" %>
<html>
<head>
    <title>处理用户登录界面</title>
</head>
<body>
    <%
        /*使用request对象获取用户提交的请求参数*/
        String name = request.getParameter("userName");
        String pwd = request.getParameter("password");
        if (name!=null && !name.equals("") && name.equals("admin")
         && pwd!=null && !pwd.equals("") && pwd.equals("123456")){
            Users current = new Users(name,pwd);
            //将对象存放在会话Session作用域中
            session.setAttribute("currentUser",current);
        }
    %>
        <a href="index.jsp">访问主页面</a>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 13:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>out 和 response 隐式对象</title>
</head>
<body>
    <%!
        String language = "";
        String hy = "";
        String checkLanguage(String lan){
            if (lan.equals("python"))
                return "人工智能";
            else if (lan.equals("java"))
                return "企业级开发";
            else
                return "其它领域";
        }
    %>
    <%
        hy = checkLanguage(language);
        //使用response隐式对象实现请求的重定向动作
        if (hy.equals("人工智能"))
            response.sendRedirect("ygzn.jsp");
        else if (hy.equals("企业级开发"))
            response.sendRedirect("qyjkf.jsp");
        else
            response.sendRedirect("other.jsp");
    %>
    适合的领域是:<%out.write(hy);%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 13:52
  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>
    欢迎来到其它语言学习页面
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 16:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>pageContext作用域</title>
</head>
<body>
    <%
        pageContext.setAttribute("mess","welcome");
        //request请求域中存储对象,在同一个请求域中是可以获取到的
        request.setAttribute("mess2","King");
    %>
</body>
    当前从pageContext作用域中取出对象:<%=pageContext.getAttribute("mess")%><br>
    当前从request作用域中取出对象:<%=request.getAttribute("mess2")%>
    <%
        request.getRequestDispatcher("page2.jsp").forward(request,response);
    %>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 16:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>pageContext作用域</title>
</head>
<body bgcolor="blue">

</body>
    <%=
        pageContext.getAttribute("mess")
    %>

    当前从request作用域中取出对象:<%=request.getAttribute("mess2")%>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 13:52
  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>
    欢迎来到企业级开发页面
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: zengy
  Date: 2022/2/1
  Time: 13:52
  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>
    欢迎来到人工智能学习页面
</body>
</html>

 

posted @ 2022-02-02 00:28  伊万  阅读(41)  评论(0编辑  收藏  举报