jsp登陆校验演示 servlet、login、success

栏目: JSP · 发布时间: 6年前

内容简介:jsp登陆校验演示 servlet、login、success

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据库索引设计与优化

数据库索引设计与优化

【美】Tapio Lahdenmaki、【美】Michael Leach / 曹怡倩、赵建伟 / 电子工业出版社 / 2015-6 / 79.00元

《数据库索引设计与优化》提供了一种简单、高效、通用的关系型数据库索引设计方法。作者通过系统的讲解及大量的案例清晰地阐释了关系型数据库的访问路径选择原理,以及表和索引的扫描方式,详尽地讲解了如何快速地估算SQL 运行的CPU 时间及执行时间,帮助读者从原理上理解SQL、表及索引结构、访问方式等对关系型数据库造成的影响,并能够运用量化的方法进行判断和优化,指导关系型数据库的索引设计。 《数据库索......一起来看看 《数据库索引设计与优化》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具