JSP内置对象 - config对象和session对象

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>

猜你喜欢

转载自www.cnblogs.com/GjqDream/p/11580601.html
今日推荐