主页 > 知识库 > jsp登陆校验演示 servlet、login、success

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

热门标签:Linux服务器 电子围栏 Mysql连接数设置 服务器配置 团购网站 银行业务 阿里云 科大讯飞语音识别系统

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> 

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

您可能感兴趣的文章:
  • js实现登录注册框手机号和验证码校验(前端部分)
  • JavaScript 完成注册页面表单校验的实例
  • JavaScript注册时密码强度校验代码
  • js简单实现用户注册信息的校验代码
  • JS校验与最终登陆界面功能完整示例
  • 详解AngularJs HTTP响应拦截器实现登陆、权限校验
  • Angular.js与node.js项目里用cookie校验账户登录详解
  • 拦截JSP页面,校验是否已登录详解及实现代码
  • ASP.NET MVC结合JavaScript登录、校验和加密
  • javascript使用正则表达式实现注册登入校验

标签:衡水 大理 江苏 广元 衢州 萍乡 枣庄 蚌埠

巨人网络通讯声明:本文标题《jsp登陆校验演示 servlet、login、success》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266