使用jsp实现登录功能
页面说明:1、index.jsp登录表单页面 2、go.jsp表单验证页面 3、success.jsp登录成功界面
1、index.jsp登录表单页面
1)登录表单
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <form action="go.jsp" name="Myfeg" onsubmit="return checkReg()" method="get"> 2 <table cellpadding="10"> 3 <tr> 4 <td> 用户名:</td> 5 <td><input type="text" name="userName" placeholder="用户名为4-16个字符" value="<%=userName%>"/></td> 6 </tr> 7 <tr> 8 <td> 密 码:</td> 9 <td> <input type="password" name="userPwd" placeholder="密码由数字字符6~18位"></td> 10 </tr> 11 <tr> 12 <td> 13 14 </td> 15 16 <td> 17 <input class="dl" type="submit" name="dl" value="登录" style="margin-left: 10px;border:none"> 18 <input class="cz" type="button" name="cz" value="注册" style="margin-left: 40px;border:none"> 19 </td> 20 </tr> 21 </table> 22 </form>
2)获取cookie
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%-- 获取网页的cookie,判断是否有需要的登录名信息,有则显示在用户名框内--%> 2 <% 3 String userName = ""; 4 Cookie[] cookie = request.getCookies(); 5 if(cookie == null || cookie.length == 0){ 6 }else { 7 for (Cookie cookie1 : cookie) { 8 if (cookie1.getName().equals("userName")){ 9 userName = cookie1.getValue(); 10 //out.print(userName); 11 } 12 //out.print(cookie1.getName()+"--"); 13 } 14 } 15 %>
3)获取地址栏参数值以及记录访问次数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%-- 获取地址栏的属性值以及application记录页面访问次数--%> 2 <% 3 String mess = request.getParameter("info"); 4 if (mess != null ){ 5 out.print(mess); 6 } 7 Object count = application.getAttribute("count"); 8 if(count == null){ 9 //application中未存放count 10 application.setAttribute("count", 1); 11 }else{ 12 //application中已经存放count 13 Integer i=(Integer)count; 14 application.setAttribute("count", i+1); 15 } 16 Integer icount=(Integer)application.getAttribute("count"); 17 out.println("页面被访问了"+icount+"次"); 18 %>
4)修改地址,删除参数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%-- 登录失败后,返回主页面,修改地址栏参数,刷新之后不会显示登录失败提示--%> 2 <script> 3 var url = document.URL; 4 var num = url.indexOf('?'); 5 if (num){ 6 URL = url.substring(0,num); //截取网址信息 7 history.pushState(null,null,URL); //将网址设置 8 } 9 </script>
2、go.jsp表单验证页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <% 2 String userName = request.getParameter("userName"); 3 String userPwd = request.getParameter("userPwd"); 4 if (userName.equals("系统管理员") && userPwd.equals("123")){ 5 //登录成功,session保存客户信息,并创建cookie发送客户,用于记录登录名,并显示在登录名框内 6 session.setAttribute("userName",userName); 7 Cookie cookie = new Cookie("userName",userName); 8 cookie.setPath("/"); 9 response.addCookie(cookie); 10 request.getRequestDispatcher("success.jsp").forward(request,response); 11 }else{ 12 //登录失败,使用重定向回到登录界面,为地址栏添加一个属性,提示登录失败 13 String info = "登录失败!"; 14 info = URLEncoder.encode(info,"utf-8"); 15 response.sendRedirect("index.jsp?info="+info); 16 //request.getRequestDispatcher("index.jsp").forward(request,response); 17 } 18 %>
3、success.jsp登录成功界面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <% 2 String userName = (String) session.getAttribute("userName"); 3 if(userName == null || userName == ""){ 4 response.sendRedirect("index.jsp"); 5 }else {%> 6 <h1>登录成功!欢迎您,<%out.print(session.getAttribute("userName"));%></h1> 7 <%}%> 8 <button type="button" onclick="javascript:location.href='index.jsp'">注销</button>