在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Servlet HttpSession登錄注銷實例
Servlet web.xml welcome-file-list
Servlet從數據庫讀取記錄性能優(yōu)化
Servlet URL重寫帶參數
War文件
Web技術基礎
Servlet GenericServlet類
Servlet API
Servlet ServletInputStream類
使用MyEclipse創(chuàng)建Servlet
Servlet增刪改查
Servlet ServletConfig配置信息
Servlet Cookies
Servlet重定向
Servlet生命周期
Servlet HttpSession會話
Servlet HttpServlet類
Servlet注冊表單示例
Servlet表單隱藏字段
Servlet下載文件
Servlet教程
Servlet身份驗證過濾器
Servlet ServletOutputStream類
Servlet HttpSession登錄注銷實例
Servlet啟動時加載
Servlet事件和監(jiān)聽器
使用Eclipse創(chuàng)建Servlet
Servlet ServletContextEvent事件
Servlet HttpSessionEvent統計在線用戶數實例
Servlet RequestDispatcher請求轉發(fā)
Servlet使用注釋
Servlet過濾器示例
Servlet過慮器
Servlet ServletContext配置信息
Servlet登錄注銷Cookies實例
Servlet工作流程
Servlet會話跟蹤
Servlet登錄實例
Servlet ServletRequest接口
Servlet ServletRequestEvent類和接口
Servlet入門程序
Servlet查詢搜索數據示例
Servlet FilterConfig應用示例
Servlet顯示所有頭信息
Servlet屬性設置
使用NetBeans創(chuàng)建Servlet
Servlet接口實現
Servlet上傳文件

Servlet HttpSession登錄注銷實例

在前面一節(jié)教程文章中,已經介紹了什么是HttpSession,如何存儲和從會話對象獲取數據。
我們可以綁定HttpSession實例上的對象,并使用setAttributegetAttribute方法獲取對象的數據信息。
在這里,將演示一個在不使用數據庫連接的情況下,創(chuàng)建一個真實的登錄和注銷應用程序。假設有一個用戶名是:admin,它密碼是admin123

注: 訪問這里查看:使用cookies登錄和注銷的應用程序示例 ,有助于學習本節(jié)文章的內容。

在這個例子中,我們創(chuàng)建了3個鏈接頁面:登錄,注銷和個人資料。實現功能:用戶在登錄之前不能進入個人資料頁面。如果用戶已經注銷,則需要再次登錄才能訪問個人資料頁面。

在這個應用程序中,創(chuàng)建了以下幾個文件。

  • index.html - Web應用程序首頁。
  • link.html - 鏈接頁面。
  • login.html - 登錄頁面。
  • LoginServlet.java - 登錄Servlet處理。
  • LogoutServlet.java - 注銷Servlet處理。
  • ProfileServlet.java - 用戶個人資料Servlet。
  • web.xml - Servlet配置文件。

打開Eclipse,創(chuàng)建一個動態(tài)Web項目:SessionLoginout,其完整的目錄結構如下所示 -

以下是這個項目中的幾個主要的代碼文件。

文件:index.html -

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session登錄注銷實例</title>
</head>
<body style="text-algin: center;">
    <h2>歡迎Session登錄注銷應用程序</h2>
    <a href="login.html">登錄</a>|
    <a href="logout">注銷</a>|
    <a href="profile">個人信息</a>
</body>
</html>

文件:link.html -

<a href="login.html">登錄</a>|
<a href="logout">注銷</a>|
<a href="profile">個人信息</a>
<hr>

文件:login.html -

    <div style="text-algin: center; padding-top:12px;">
        <form action="login" method="post">
            用戶名:<input type="text" name="username">  密碼:<input type="password"
                name="password"><input type="submit" value="登錄">
        </form>
    </div>
</body>
</html>

文件:LoginServlet.java -

package com.yiibai;

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 LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        request.getRequestDispatcher("link.html").include(request, response);

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }

        if (username.equals("admin") && password.equals("admin123")) {
            out.print("Welcome, " + username);
            HttpSession session = request.getSession();
            session.setAttribute("username", username);
            session.setAttribute("nickname", "蘇小牛");
            session.setAttribute("age", "21");
        } else {
            out.print("<font style='color:rec;'>對不起,用戶名或密碼錯誤!</font>");
            request.getRequestDispatcher("login.html").include(request, response);
        }
        out.close();
    }
}

文件:logoutServlet.java -

package com.yiibai;

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 LogoutServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        request.getRequestDispatcher("link.html").include(request, response);
        HttpSession session = request.getSession();
        // 清除數據
        session.invalidate();
        out.print("您已成功注銷退出系統!");
        out.close();
    }
}

文件:ProfileServlet.java -

package com.yiibai;

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 ProfileServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        request.getRequestDispatcher("link.html").include(request, response);

        HttpSession session = request.getSession(false);
        if (session != null) {
            String name = (String) session.getAttribute("username");
            String nickname = (String) session.getAttribute("nickname");
            String age = (String) session.getAttribute("age");
            out.print("您好, " + name + " 歡迎您來到個人信息中心!");
            out.print("<hr/>個人信息如下, 昵稱:" + nickname + " ,年齡:"+age);
        } else {
            out.print("請登錄系統!");
            request.getRequestDispatcher("login.html").include(request, response);
        }
        out.close();
    }
}

文件:web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>SessionLoginout</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <description></description>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.yiibai.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>ProfileServlet</servlet-name>
        <servlet-class>com.yiibai.ProfileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ProfileServlet</servlet-name>
        <url-pattern>/profile</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>LogoutServlet</servlet-name>
        <servlet-class>com.yiibai.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LogoutServlet</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>
</web-app>

在編寫上面代碼后,部署此Web應用程序(在項目名稱上點擊右鍵->”Run On Server…”),打開瀏覽器訪問URL: http://localhost:8080/SessionLoginout/ ,如果沒有錯誤,應該會看到以下結果 -

點擊“登錄”,如下界面 -

使用用戶名:admin ,密碼:admin123,點擊登錄成功后,如下界面 -

用戶名或密碼錯誤時,顯示如下 -

點擊“個人信息”鏈接,顯示如下 -

點擊“注銷”鏈接,顯示如下 -