jsp的session完成登陸功能

来源:http://www.cnblogs.com/shouce/archive/2016/02/25/5215694.html
-Advertisement-
Play Games

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


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.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>用戶登錄</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <h1>用戶登錄</h1>
27     <form action="check" method="post">
28         <table border="1" width="250px">
29             <tr><td>用戶名:</td><td><input type="text" name="userId" /></td></tr>
30             <tr><td>密碼:</td><td><input type="password" name="passwd" /></td></tr>
31             <tr><td></td><td><input type="submit" value="提交" /></td></tr>
32         </table>
33     </form>
34   </body>
35 </html>
複製代碼

welcom.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.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'welcome.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">    
19   </head>
20      <%
21         String user=(String) session.getAttribute("user");
22         if(user==null){
23         %>
24         <jsp:forward page="login.jsp" />
25         <%} %>
26   <body>
27  熱烈歡迎您:<%=user %>
28   </body>
29 </html>
複製代碼

web.xml

複製代碼
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <display-name></display-name>    
 8   <welcome-file-list>
 9     <welcome-file>index.jsp</welcome-file>
10   </welcome-file-list>
11   <servlet>
12       <servlet-name>CheckUser</servlet-name>
13       <servlet-class>servlet.CheckUser</servlet-class>
14   </servlet>
15   <servlet-mapping>
16       <servlet-name>CheckUser</servlet-name>
17       <url-pattern>/check</url-pattern>
18   </servlet-mapping>
19   
20 </web-app>
複製代碼

CheckUser.java

複製代碼
 1 package servlet;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.RequestDispatcher;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 import javax.servlet.http.HttpSession;
11 
12 public class CheckUser extends HttpServlet {
13     @Override
14     protected void doPost(HttpServletRequest request,
15             HttpServletResponse response) throws ServletException, IOException {
16             request.setCharacterEncoding("UTF-8");
17             String userId=request.getParameter("userId");
18             String passwd=request.getParameter("passwd");
19             
20             if(userId!=null&&passwd!=null&&userId.equals("gys")&&passwd.equals("gys")){
21                 HttpSession session=request.getSession();
22                 session.setAttribute("user", userId);
23                 RequestDispatcher dispatcher=request.getRequestDispatcher("/welcome.jsp");
24                 dispatcher.forward(request, response);
25             }
26             else{
27                 RequestDispatcher dispatcher=request.getRequestDispatcher("/login.jsp");
28                 dispatcher.forward(request, response);
29             }
30     }
31 }
複製代碼

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 原文地址:http://www.manongjc.com/article/491.html const是用於類成員常量的定義了,定義之後不可改,而define我們定義的是全局常量了, 這樣我們在其它地方訪問但不能改變了,具體還有一些細節我們下麵給各位列出來吧 註意:define不能定義在類中,而co
  • 原文地址:http://www.manongjc.com/java_example/java_method.html Java方法重載 Java輸出數組元素 Java漢諾塔演算法 Java斐波那契數列 Java階乘 Java方法覆蓋 Java instanceOf 關鍵字用法 Java break 關
  • #include <iostream> int main(int argc, char** argv) { int i = 0; int *p = &i; int **pp = &p; **pp = 100; printf("%d\n",i); printf("i的地址值:%p\n",&i); pr
  • #include <iostream> int main(int argc, char** argv) { char *s[10];//指針數組 char (*s1)[10];//數組指針 //前面一個是 指針數組,也就是 指針是形容詞用來修飾數組,可見,重點落在,這是一個數組上。 //那麼數組的元
  • HandlerMapping將url分發到對應的handler處理器,並添加攔截器. 實現上,經典的介面+抽象類+具體演算法實現.
  • 一、什麼是301重定向 301重定向(或叫301跳轉、301轉向)是用戶或搜索引擎蜘蛛向網站伺服器發出訪問請求時,服務返回的HTTP數據流中頭信息(header)部分狀態碼的一種,表示本網址永久性轉移到另一個地址。其他常見的狀態碼還包括:200(表示一切正常);404(網頁不存在);302(臨時性轉
  • 在php中,訪問類的方法/變數有兩種方法:1. 創建對象$object = new Class(),然後使用”->”調用:$object->attribute/function,前提是該變數/方法可訪問。2. 直接調用類方法/變數:class::attribute/function,無論是靜態/非靜
  • 先上題:下列運算符都可以被友元函數重載的是: A)=,+,-,\ B)[],+,(),new C)->,+,*,>> D)<<,>>,+,* 正確答案為D 我們知道,在運算符重載,友元函數運算符重載函數與成員運算符重載函數的區別是:友元函數沒有this指針,而成員函數有,因此,在兩個操作數的重載中友
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...