程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp登陸校驗演示 servlet、login、success,servletlogin

jsp登陸校驗演示 servlet、login、success,servletlogin

編輯:關於JSP

jsp登陸校驗演示 servlet、login、success,servletlogin


jsp的登錄校驗Demo

part_1:login.jsp:登錄頁面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
 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 'Login.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> 
 
 <% 
 String fdbkMsg = (String) request.getAttribute("fdbkMsg"); 
 if (null == fdbkMsg) { 
 fdbkMsg = ""; 
 } 
 %> 
 
 <% 
 Boolean logedIn = (Boolean) session.getAttribute("logedIn"); 
 if (null == logedIn) { 
 logedIn = false; 
 } else if (logedIn) { 
 //如果在本次會話已經登陸,直接重定向到success-page-1 
 response 
  .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); 
 } 
 %> 
 
 <% 
 String username = ""; 
 Cookie[] cookies = request.getCookies(); 
 if ((null != cookies) && (cookies.length > 0)) { 
 for (Cookie c : cookies) { 
 if ("admin".equals(c.getValue())) { 
  username = "admin"; 
  break; 
 } 
 } 
 }//end if-condition 
 %> 
 
 <body> 
 <br> 
 <div align="center"> 
 請登錄: 
 <br> 
 <form action="/ServletDemoProject/servlet/LoginVerificationServlet" 
 method="post"> 
 用戶名: 
 <input type="text" name="username" value="<%=username%>" /> 
 <br> 
 密 碼: 
 <input type="password" name="password" value="" /> 
 <br> 
 <font color='red'><%=fdbkMsg%></font> 
 <br> 
 <input type="submit" value="提交" /> 
 <br> 
 </form> 
 </div> 
 </body> 
</html> 

part_2:LoginVerificationServlet.java:校驗登錄信息,此處沒有連接數據庫,默認只有username:admin,password:888888才算登錄成功;登陸失敗時:重新轉發到Login.jsp並提示用戶登陸失敗,重新登陸;

package cn.mike.servlet.test_1209_Login; 
 
import java.io.IOException; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class LoginVerificationServlet extends HttpServlet { 
 
 private static final long serialVersionUID = -6886327892796230543L; 
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 String username = request.getParameter("username"); 
 String password = request.getParameter("password"); 
 if (("admin".equals(username)) && ("888888".equals(password))) {// 登錄成功 
 // 保存cookie到客戶端 
 Cookie userCookie = new Cookie("username", username); 
 userCookie.setMaxAge(60 * 2);// expiry : 2 minutes 
 response.addCookie(userCookie); 
 // 重定向到一個新的頁面,並提示XXX用戶登錄成功(使用session存取用戶名); 
 request.getSession().setAttribute("username", username); 
 request.getSession().setAttribute("logedIn", true); 
 response 
  .sendRedirect("/ServletDemoProject/LOGIN-DEMO/success-page-1.jsp"); 
 } else {// 登陸失敗 
 // 轉發到登錄界面,並提示錯誤信息: 
 request.setAttribute("fdbkMsg", "用戶名或密碼錯誤!"); 
 request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( 
  request, response); 
 } 
 } 
 
 public void doPost(HttpServletRequest request, HttpServletResponse response) 
 throws ServletException, IOException { 
 
 // do same as GET-method : 
 doGet(request, response); 
 } 
 
} 

part_3:success-page-1.jsp:校驗登錄成功後重定向到該頁面,提示用戶已經成功登陸;如果用戶試圖通過不正當途徑,e.g:從地址欄訪問,將會轉發到登錄界面,並作提示;

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<% 
 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 'success-page-1.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> 
 
 <% 
 String username = (String) session.getAttribute("username"); 
 if (null == username) { 
 //如果username為空值,說明不是通過正常渠道來的,轉發到Login頁面; 
 request.setAttribute("fdbkMsg", "別想走後門進來,趕緊登錄!"); 
 request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( 
  request, response); 
 } 
 %> 
 
 <body> 
 <br> 
 <%=username%>已經成功登陸。 
 <br> 
 <font>您可以選擇浏覽:</font> 
 <br> 
 <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒有精彩.</a> 
 <br> 
 <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">點這兒更精彩.</a> 
 <br /> 
 <a href="/ServletDemoProject/LOGIN-DEMO/success-page-2.jsp">你敢點這兒嗎.</a> 
 <br /> 
 </body> 
</html> 

part_4:success-page-2.jsp:登陸成功頁面2,如果已經登陸成功將用戶名保存到session,在訪問該頁面時將會校驗一下,防止從地址欄暴力訪問;

<%@ page language="java" import="java.util.Date" pageEncoding="UTF-8"%> 
<%@ page language="java" import="java.text.SimpleDateFormat"%> 
<% 
 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 'success-page-2.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> 
 
 <% 
 String username = (String) session.getAttribute("username"); 
 if (null == username) { 
 request.setAttribute("fdbkMsg", "呵呵嗒,這裡是你來的地方嗎?快登陸!"); 
 //轉發到登錄界面: 
 request.getRequestDispatcher("/LOGIN-DEMO/Login.jsp").forward( 
  request, response); 
 } 
 
 SimpleDateFormat sDateFormat = new SimpleDateFormat("a"); 
 Date today = new Date(); 
 String am_pm_str = sDateFormat.format(today); 
 String am_pm_str_in_chinese = ""; 
 if ("am".equalsIgnoreCase(am_pm_str)) { 
 am_pm_str_in_chinese = "上午"; 
 } else 
 am_pm_str_in_chinese = "下午"; 
 
 // set null; 
 sDateFormat = null; 
 today = null; 
 am_pm_str = null; 
 %> 
 
 <body> 
 <br /> 
 <font><b><%=username%><%=am_pm_str_in_chinese%>好,能來到頁面2真不簡單.</b> 
 </font> 
 </body> 
</html> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持幫客之家。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved