简单的session实例

1.session.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.test.bean.Person" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<jsp:directive.page import="java.text.DateFormat" />
<%!
 DateFormat dataFormate = new SimpleDateFormat("yyyy-MM-dd");
 %>
<%
 response.setCharacterEncoding("UTF-8");
 
 Person[] persons = {
  new Person("java","password1",dataFormate.parse("1982-03-11")),
  new Person("Hello","password2",dataFormate.parse("1988-01-11")),
  new Person("Hello2","password3",dataFormate.parse("1988-01-11"))
 };
 
 String message = "";
 if(request.getMethod().equals("POST")){
  for(Person person : persons){
   if(person.getName().equalsIgnoreCase(request.getParameter("username"))
    && person.getPassword().equals(request.getParameter("password"))
   ){
    session.setAttribute("person",person);
    session.setAttribute("loginTime",new Date());
    
    response.sendRedirect(request.getContextPath() +"/welcome.jsp");
    return;
   }
  }
  message = "用户名密码不正确";
 }
 
 %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'session.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
   <body>
    <div align="center" style="margin:10px; ">
     <fieldset>
      <legend>登录</legend>
      <form action="session.jsp" method="post">
       <table>
      <%-- <tr>
        <td></td>
        <td><span></span>
         <span style="color:red;"><%= exception.getMessage() %></span>
          </td>
       </tr>
      --%>
      <tr>
       <td>账号:</td>
       <td><input type="text" name="username" style="width:200px;"></td>
      </tr>    
      
      <tr>
       <td>密码:</td>
       <td><input type="password" name="password" style="width:200px;">
       </td>
      </tr>
      <tr>
      <td></td>
      <td><input type="submit" value="登  录"></td>
      </tr>
      
      </table>
      </form>
     
     </fieldset>
  </body>
</html>

2.Person.java

package com.test.bean;

import java.util.Date;

public class Person
{
 String name;
 String password;
 Date date;
 public Person(String name,String password,Date date){
  this.name = name;
  this.password = password;
  this.date = date;
 }
 
 public String getName()
 {
  return name;
 }
 public void setName(String name)
 {
  this.name = name;
 }
 public String getPassword()
 {
  return password;
 }
 public void setPassword(String password)
 {
  this.password = password;
 }
 public Date getDate()
 {
  return date;
 }
 public void setDate(Date date)
 {
  this.date = date;
 }
}

3.welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<jsp:directive.page import="com.test.bean.Person" />
<jsp:directive.page import="java.text.SimpleDateFormat" />
<jsp:directive.page import="java.text.DateFormat" />
<%!
 DateFormat dataFormate = new SimpleDateFormat("yyyy-MM-dd");
 %>
 <%
  Person person = (Person)session.getAttribute("person");
  Date loginTime = (Date)session.getAttribute("loginTime");
  %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'welcome.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    <table>
     <tr>
      <td>您的名字:</td>
      <td><%=person.getName() %></td>
     </tr>
     <tr>
      <td>登录时间:</td>
      <td><%= loginTime %></td>
     </tr>
     <tr>
      <td>您生日:</td>
      <td><%= person.getDate() %></td>
     </tr>
     
    </table>
  </body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Flask 应用中,上下文是一个重要的概念,它是用来存储 Flask 应用中的一些数据和配置信息的容器。上下文分为两种类型:应用上下文和请求上下文。 应用上下文是全局的,它存储了应用的配置信息、注册的插件、数据库连接等信息。而请求上下文则是针对每个请求的,它存储了请求的相关信息,如请求参数、请求头、请求方法等。 在 Flask 应用中,我们可以使用 session 来存储用户的会话信息。session 是一个字典,它可以在不同的请求之间传递数据,以实现用户身份验证、缓存等功能。 在 Flask 应用中,session 实例是通过 Flask 应用上下文的 g 对象来获取的。我们可以在视图函数中通过 g 对象来获取 session 实例,然后使用 session 实例来存储和获取数据。 以下是一个简单的 Flask 应用上下文 session 实例的示例: ```python from flask import Flask, request, g, session app = Flask(__name__) app.secret_key = 'secret_key' @app.route('/login', methods=['POST']) def login(): username = request.form['username'] password = request.form['password'] if username == 'admin' and password == 'admin': session['logged_in'] = True return 'Login success' else: return 'Login failed' @app.route('/logout') def logout(): session.pop('logged_in', None) return 'Logout success' @app.route('/dashboard') def dashboard(): if not session.get('logged_in'): return 'Please login first' return 'Welcome to dashboard' if __name__ == '__main__': app.run() ``` 在上面的示例中,我们使用了 Flask 应用上下文的 g 对象来获取 session 实例,并使用 session 实例来存储和获取数据。在登录视图函数中,我们将用户登录状态存储在 session 中,然后在仪表板视图函数中检查用户是否已登录。如果用户没有登录,则返回错误消息。如果用户已经登录,则返回欢迎消息。 要注意的是,我们必须设置 Flask 应用的密钥(secret_key),以便在使用 session 时保持数据的安全性。在上面的示例中,我们设置了一个名为 secret_key 的密钥。请确保您的密钥是安全的,并且不要公开它。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值