jsp+servlet 實現登入功能__js

來源:互聯網
上載者:User

現在的架構比較流行,為了更好的理解架構,javaweb的基本開發形式還需要瞭解。

下面是運用jsp+servlet技術是實現登入。(沒有運用資料庫,資料操作比較簡單,這個例子主要描述開發流程)

project:jspServletLoginTest

項目的目錄結構如下:

 

下面貼上代碼:

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!-- 該Login頁面是一個簡單的登入介面 --> <html >    <head>      <title>Login</title>      <meta http-equiv= "content-type" content = "text/html; charset=UTF-8">      <meta http-equiv= "Content-Language" content = "ch-cn">    </head>    <body >   <!-- Form 用來提取使用者填入並提交的資訊-->   <form method = "post" name = "frmLogin" action="loginServlet">     <h1 align = "center"> USER LOGIN</h1> <br >     <div align = "center" > User name:      <input type = "text" name = "txtUserName" value = "hym"         size = "20" maxlength = "20"  > <br> Password:      <input type = "password" name = "txtPassword" value = "Your password"         size = "20" maxlength = "20" > <br>        <input type = "submit" name = "Submit"  value= "submit" onClick = "validateLogin();" >        <input type = "reset" name = "Reset" value = "reset" > <br>     </div>   </form>   <!-- javaScript 函數 validateLogin(),null validate-->      <script language = "javaScript">       function validateLogin( )       {        var sUserName = document. frmLogin. txtUserName. value ;        var sPassword = document. frmLogin. txtPassword. value ;        if( sUserName= = "" )        {         alert( "input user name。" ) ;         return false;        }        if( sPassword= = "" )        {         alert( "input password。" ) ;         return false;        }       }      </script>    </body> </html> 


login_success.jsp

<%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > <html>   <head>    <title> My JSP 'login_failure.jsp' starting page </title>    <meta http-equiv= "content-type" content = "text/html; charset=UTF-8">    <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">   </head>   <body>    <%     String userName = ( String ) session. getAttribute ( "UserName" ) ;    %>    <div align = center>     <%=userName%>     welcome,logined!  </div>   </body> </html> 

login_failure.jsp

<%@page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>    <title> My JSP 'login_failure.jsp' starting page </title>    <meta http-equiv= "content-type" content = "text/html; charset=UTF-8">    <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">   </head>   <body>    <%    String userName = ( String ) session. getAttribute ( "UserName" ) ;    %>    <div align = center>     <%=userName%>   failure access!  </div>   </body> </html> 

 

servlet類,表單的資料需要提交到這個類進行處理(頁面跳轉的控制類),若要深入理解Form的提交和處理原理,則需要深入研究web的HTTP協議

loginServlet.java

package test.login;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class loginServlet extends HttpServlet {private static final long serialVersionUID = 1L;           public loginServlet() {        super();    }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   response. setContentType ( "text/html" ) ;    // get username   String sUserName = request.getParameter("txtUserName") ;    // get password   String sPasswd = request.getParameter("txtPassword") ;     //test   System.out.println("username:"+sUserName+"\r\n"+"password:"+sPasswd);   request . getSession( ).setAttribute ( "UserName" , sUserName ) ;    request.getSession().setAttribute("password", sPasswd);   //just compare with hym&123,not do database operation   if(sUserName.equals("hym")&&sPasswd.equals("123"))   {   response. sendRedirect ("login_success.jsp") ;    }   else {   response. sendRedirect ("login_failure.jsp") ;    }}}


 

 

到目前為止,項目的主要的功能檔案已經完成。但是還要在web.xml中註冊項目啟動並執行歡迎檔案以及servlet的註冊。(關於web.xml檔案的工作原理有機會在補上了)

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>jspServletLoginTest</display-name> <servlet>  <description>  </description>  <display-name>loginServlet</display-name>  <servlet-name>loginServlet</servlet-name>  <servlet-class>  test.login.loginServlet</servlet-class> </servlet> <servlet-mapping>  <servlet-name>loginServlet</servlet-name>  <url-pattern>/loginServlet</url-pattern> </servlet-mapping> <!-- 通過如下的設定web project在tomcat伺服器上啟動並執行時候,可以首先啟動和運行webcontent目錄下面 相應的頁面,會根據welcome-file的list去檢索 --> <welcome-file-list>  <welcome-file>index.html</welcome-file>  <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>


如此就實現了jsp+servlet實現的使用者登入功能。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.