現在的架構比較流行,為了更好的理解架構,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實現的使用者登入功能。