jsp登陆校验演示 servlet、login、success
程序员文章站
2023-11-24 20:44:16
jsp的登录校验demo
part_1:login.jsp:登录页面:
<%@ page language="java" import="java...
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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: python实现多线程的两种方式