jsp:通过Session控制登陆时间和内部页面的访问

一,通过session的关闭浏览器销毁,和使用getAttribute设置session对象值来控制页面用户是否有权限进入。

1,以下是登陆页面的代码,使用表单提交数据给一个servlet进行数据的处理

<form action="com.in.User" method="post">
<div class="loginbox">
<div class="errorbox"><i class="error"></i>用户名或密码错误,请重新输入!</div>
<div class="logongroup">
<input class="intext" type="text" name="UserName" placeholder="用户名" />
<i class="user"></i>
</div>
<div class="logongroup">
<input class="intext" type="password" name="PassWord" placeholder="密码" />
<i class="password"></i>
</div>
<div class="logongroup submitbox">
<button class="submitbtn" type="submit">登&nbsp;&nbsp;录</button>
</div>
</div>
</form>

2,创建一个名为User的servlet页面

    在doget方法中创建session对象,使用setAttribute方法为其赋值,

package com.in;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class User extends HttpServlet {

    /**
     * Constructor of the object.
     */
    public User() {
        super();
    }

    /**  
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session=request.getSession();
        session.setAttribute("user","yes");
        request.getRequestDispatcher("default.jsp").forward(request, response);

    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }

}

3,在登陆之后的页面中加入以下代码,以控制页面的访问权限。

    放在<body>的下方,如果session为空或不等于上面赋的值,便跳转到登陆页面,重新为session赋值。

<%
        String aa=(String)session.getAttribute("user");
        if(!"yes".equals(aa)&&aa==null){
        response.sendRedirect("longin.jsp");
        }
 %>
时间: 2024-08-03 05:21:07

jsp:通过Session控制登陆时间和内部页面的访问的相关文章

jsp的session完成登陆功能

login.jsp: 1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getSe

Session控制登陆

Session控制登陆 <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <script> function on_load() { console.log("welcome!"); } function on_focus() { var name = document.getEleme

黑马day05 session实现登陆&amp;注销小案例

本案例主要使用session实现登录与注销的功能. 1.登录页面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title&g

jsp之session对象

jsp之session对象:一:概念session对象可以在应用程序的web页面之间跳转时保存用户的信息,使整个用户会话一直存在,直到关闭浏览器或是销毁session.session的生命周期:20~30分钟之间二:session常用的方法session.setAttribute(String name,Objectobj); //往session会话中存放数据session.getAttribute(String name); //获取session中的值session.removeAttri

jsp的Session 和Servlet的Session的区别

1.session机制    http是无状态的协议,客户每次读取web页面时,服务器都打开新的会话,而且服务器也不会自动维护客户的上下文信息,session就是一种保存上下文信息的机制,它是针对每一个用户的,变量的值保存在服务器端,通过SessionID来区分不同的客户,session是以cookie或URL重写为基础的,默认使用cookie来实现,系统会创造一个名为JSESSIONID的输出返回给客户端Cookie保存. 2.jsp和Servlet的关系    jsp是servlet的一种简

jsp:param和meta来控制导航选中与否,页面组装器sitemesh

A.jsp B.jsp C.jsp decorators.xml 其中A.jsp是某项功能具体的jsp,A包含B,C是默认模板包含B 注:需要了解sitemesh和decorators页面组装器的主要知识http://www.cnblogs.com/luotaoyeah/p/3776879.html 需要下载sitemesh的jar包,不用组装器也可以实现 http://blog.csdn.net/liu251/article/details/2797488 decorators.xml中主要内

关于session的过期时间的方法

session的的过期时间的设置,通用的三种方法: 1.  直接在后台代码中设计,使用session.setMaxInactiveInterval(60*60); 注意这个时间的单位是秒,60*60就是一小时. 如果将时间设置为负数,则表示用不过期. 2.在项目的web.xml文件中添加配置过期时间的代码即可. <session-config> <session-timeout>400</session-timeout> </session-config>

thinkphp3.2设置session的过期时间

thinkPHP3.2中session的过期时间配置是不能使用的,我们需要修改一下它的配置文件thinkPHP>common>functions.php,找到这一行: if(isset($name['expire'])) ini_set('session.gc_maxlifetime', $name['expire']); 改成: if(isset($name['expire'])) ini_set('session.gc_maxlifetime', $name['expire']);ini_

Linux记录用户账户的登陆时间和行为信息

??? 公司审计部门要求我们对所有的主机系统添加用户行为审计功能,记录用户账户的登陆时间和行为信息,我这边用了一个简单的方法实现以下,下面我简单介绍一下实现的方法 实施步骤: 1.创建用户行为日志存放目录/var/audit,目录赋予777权限(因为所有用户都要向这个目录写入文件) mkdir?-p?/var/audit;chmod?777?/var/audit ? ? 2.修改系统环境变量文件/etc/profile,文件新增用户登录记录信息,添加如下内容 #Add?log?user?beha