zoukankan      html  css  js  c++  java
  • JSP内置对象

    1、config对象的使用

    1)xml 文件(web.xml)

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <welcome-file-list>
            <welcome-file>/showConfig.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>showConfig</servlet-name>
    <!--        jsp文件的路径-->
            <jsp-file>/showConfig.jsp</jsp-file>
    <!--        初始参数列表-->
            <init-param>
    <!--            参数以名称-值的方式存储-->
                <param-name>param1</param-name>
                <param-value>1</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
    <!--        定义servlet的访问路径-->
            <servlet-name>showConfig</servlet-name>
            <url-pattern>/showConfig.jsp</url-pattern>
        </servlet-mapping>
    </web-app>

    2)jsp 文件(showConfig.jsp)

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/9/24
      Time: 19:28
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>展示config中的初始参数</title>
    </head>
    <body>
    <%--根据名称获取初始参数的值--%>
        <%="param1's value = " + config.getInitParameter("param1")%><br />
    <%--获取servlet的名称--%>
        <%="servlet name: " + config.getServletName()%>
    </body>
    </html>

    2、session对象的使用

    1)jsp 文件(index.jsp、setSession.jsp 和 welcome.jsp)

    ①index.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/9/24
      Time: 19:41
      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="setSession.jsp" method="post">
          用户名:<input type="text" name="username" />
          密码:<input type="password" name="userpwd"/>
          <input type="submit" value="提交"/>
        </form>
      </body>
    </html>

    ②setSession.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/9/24
      Time: 19:44
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>设置session属性的界面</title>
    </head>
    <body>
        <%
            String username = request.getParameter("username");
            String userpwd = request.getParameter("userpwd");
            if (username != null && userpwd != null) {
                if (userpwd.equals("admin")) {
                    session.setAttribute("username", username);
                    response.sendRedirect("welcome.jsp");
                } else {
                    response.sendRedirect("index.jsp");
                }
            } else {
                response.sendRedirect("index.jsp");
            }
        %>
    </body>
    </html>

    ③welcome.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2019/9/24
      Time: 19:47
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>欢迎登陆</title>
    </head>
    <body>
        <%
            out.print("欢迎 " + session.getAttribute("username"));
            out.print(" 登陆!");
        %>
    </body>
    </html>
  • 相关阅读:
    归并排序(Merge Sort)
    AtCoder AGC035D Add and Remove (状压DP)
    AtCoder AGC034D Manhattan Max Matching (费用流)
    AtCoder AGC033F Adding Edges (图论)
    AtCoder AGC031F Walk on Graph (图论、数论)
    AtCoder AGC031E Snuke the Phantom Thief (费用流)
    AtCoder AGC029F Construction of a Tree (二分图匹配)
    AtCoder AGC029E Wandering TKHS
    AtCoder AGC039F Min Product Sum (容斥原理、组合计数、DP)
    AtCoder AGC035E Develop (DP、图论、计数)
  • 原文地址:https://www.cnblogs.com/GjqDream/p/11580601.html
Copyright © 2011-2022 走看看