Jsp achieve using the login function

Page Description: 1, index.jsp login form page 2, go.jsp form validation page 3, success.jsp successful login interface

1, index.jsp login form page

1) login form

 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> 密&nbsp;&nbsp;&nbsp;码:</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>
Interface login form

2) obtain cookie

1 <% - Cookie acquired page, determines whether it is necessary to login information, there is displayed in the user name box -%>
 2        <%
 . 3          String the userName = "" ;
 . 4          cookies [] = Cookie request.getCookies ();
 . 5          IF (Cookie == null || cookie.length == 0 ) {
 . 6          } the else {
 . 7            for (cookiel cookies: Cookie) {
 . 8              IF (cookie1.getName () the equals ( "the userName." {))
 . 9                  the userName = cookie1.getValue ();
 10                  // Out.print (the userName); 
. 11              }
 12 is             //out.print(cookie1.getName()+"--");
13           }
14         }
15       %>
Get cookie

3) to obtain the address bar parameter value and record number of visits

1 <% - getting a property value and a column address record page visits application -%>
 2      <%
 . 3          String = Mess request.getParameter ( "info" );
 . 4          IF (Mess =! Null ) {
 . 5              Out.print (Mess);
 . 6          }
 . 7        Object application.getAttribute COUNT = ( "COUNT" );
 . 8        IF (COUNT == null ) {
 . 9          // file application are not stored COUNT 
10          application.setAttribute ( "COUNT",. 1 );
 . 11        } the else {
 12          // the Application has been stored 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     %>
Get the address of the parameter values ​​and the record number of visits

4) modify the address, delete parameter

1 <% - login failed, then return to the main page, to modify the address field parameters, failed login prompt will not be refreshed -%>
 2        <Script>
 . 3            var URL = document.URL;
 . 4            var url.indexOf NUM = ( '?' );
 . 5            IF (NUM) {
 . 6                the URL url.substring = (0, NUM);   // taken URL information 
. 7                history.pushState ( null , null , the URL);   // URL setting 
8            } 
 9        </ script>
Modify the address, delete parameter

2, go.jsp form validation page

. 1 <%
 2          String request.getParameter the userName = ( "the userName" );
 . 3          String = UserPwd request.getParameter ( "UserPwd" );
 . 4          IF (userName.equals ( "system administrator") && userPwd.equals ( "123" )) {
 5              // login is successful, session save customer information, and send customers to create a cookie for recording the login name, login name and displayed in the box 
6              session.setAttribute ( "userName" , userName);
 7              cookie cookie = new new cookie ( "the userName" , the userName);
 . 8              cookie.setPath ( "/" );
 . 9              response.addCookie (cookie);
10             request.getRequestDispatcher ( "success.jsp" ) .forward (Request, the Response);
 11          } the else {
 12              // login fails, use a redirect back to the login screen, add a property to the address bar, login failure 
13              String info = "Login failed!" ;
 14              info = URLEncoder.encode (info, "UTF-8" );
 15              response.sendRedirect ( "index.jsp info =?" + info);
 16              // request.getRequestDispatcher ( "index.jsp ") .forward (Request, Response); 
. 17          }
 18 is      %>
form validation

3, success.jsp successful login interface

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>
Login success page

 

Guess you like

Origin www.cnblogs.com/Dean-0/p/11595839.html