Java Web實踐專題——基本MVC執行個體

來源:互聯網
上載者:User
  

         本文介紹了一個MVC執行個體,涉及檔案如下:l         login.jsp——視圖部分的輸入檔案l         success.jsp——視圖部分的輸出檔案l         failure.jsp——視圖部分的輸出檔案l         LoginBean.java——模型部分l         LoginServlet.java——控制器部分l         web.xml——web應用的設定檔下面分別介紹:1、login.jsp該功能的輸入檔案,使用者首先訪問這個檔案。主要用於輸入使用者名稱和口令。代碼如下:<%@ page contentType="text/html;charset=gb2312"%><script language="JavaScript">   function isValidate(form)   {          // 得到使用者輸入的資訊          username = form.username.value;          userpass = form.userpass.value;           // 判斷使用者名稱長度          if(!minLength(username,6))          {                 alert("使用者名稱長度小於6位!");                 form.username.focus();                 return false;          }          if(!maxLength(username,8))          {                 alert("使用者名稱長度大於8位!");                 form.username.focus();                 return false;          }           // 判斷口令長度       if(!minLength(userpass,6))          {                 alert("口令長度小於6位!");                 form.userpass.focus();                 return false;          }          if(!maxLength(userpass,8))          {                 alert("口令長度大於8位!");                 form.userpass.focus();                 return false;          }            return true;   }   // 驗證是否滿足最小長度   function minLength(str,length)   {          if(str.length>=length)                 return true;          else                 return false;   }   // 判斷是否滿足最大長度   function maxLength(str,length)   {          if(str.length<=length)                 return true;          else                 return false;   }</script><html>   <head>      <title>使用者登陸</title>   </head>   <body>      <h2>使用者登入</h2>      <form name="form1" action="login" method="post"              onsubmit="return isValidate(form1)">             使用者名稱:<input type="text" name="username"> <br>             口令:<input type="password" name="userpass"><br>             <input type="reset" value="重設">             <input type="submit" value="提交"><br>      </form>   </body></html>代碼中提供了用戶端驗證功能(使用者名稱和口令的長度為6-8位)。驗證通過之後會把請求提交給控制器Servlet。2、success.jsp登入成功之後會跳轉到這個介面,介面的代碼如下:<%@ page contentType="text/html;charset=gb2312"%><html>   <head>      <title>登入成功</title>   </head>   <body>      <h2>${sessionScope.username}您好,歡迎登入網上書店!</h2>   </body></html>代碼中使用運算式語言把登入後的使用者資訊顯示在街面上。3、failure.jsp登入失敗後會跳轉到這個介面,介面的代碼如下:<%@ page contentType="text/html;charset=gb2312"%><html>   <head>      <title>登入失敗</title>   </head>   <body>      <h2>使用者名稱或者口令不正確,請<a href="login.jsp">重新登入!</a></h2>   </body></html>代碼中提供了一個超連結,能夠連結到登入介面。4、LoginBean.java    完成登入功能,這裡假設使用者名稱和口令相等表示登入成功。package beans; public class LoginBean {        public boolean validate(String username,String userpass){              return username.equals(userpass);       } }5、LoginServlet.java該檔案完成控制,主要功能可以描述如下:l         從login.jsp擷取使用者輸入的使用者名稱和口令;l         建立LoginBean的對象,調用LoginBean的方法validate;l         根據方法返回的結果,選擇success.jsp或者failure.jsp對使用者響應。完整的代碼如下:package servlets; import java.io.IOException;import java.io.PrintWriter; import javax.servlet.*;import javax.servlet.http.*; import beans.*; public class LoginServlet extends HttpServlet {        public void doGet(HttpServletRequest request, HttpServletResponse response)                     throws ServletException, IOException {                doPost(request,response);         }        public void doPost(HttpServletRequest request, HttpServletResponse response)                     throws ServletException, IOException {              // 擷取使用者輸入的使用者ID和口令             String username = request.getParameter("username");             String userpass = request.getParameter("userpass");                          // 建立模型對象             LoginBean loginBean = new LoginBean();                         // 調用業務方法進行驗證             boolean b = loginBean.validate(username,userpass);              // 要轉向的檔案             String forward;              // 如果登陸成功,把使用者名稱寫入session中,並且轉向success.jsp,             // 否則轉向failure.jsp             if(b){                                // 擷取session                HttpSession session = (HttpSession)request.getSession(true);                        // 把使用者名稱儲存到session中               session.setAttribute("username",username);                        // 目標轉向檔案是success.jsp                forward = "success.jsp";              }else{                 // 目標轉向檔案是failure.jsp                forward = "failure.jsp";              }                                // 擷取Dispatcher對象             RequestDispatcher dispatcher = request.getRequestDispatcher(forward);             // 完成跳轉             dispatcher.forward(request,response);        } }代碼中把登入使用者的使用者資訊儲存在了session中,在實際應用中同樣也是這樣處理的。6、web.xml主要代碼是Servlet的配置,代碼如下:<?xml version="1.0" encoding="UTF-8"?><web-app 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"> <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>LoginServlet</servlet-name>    <servlet-class>servlets.LoginServlet</servlet-class> </servlet>  <servlet-mapping>    <servlet-name>LoginServlet</servlet-name>    <url-pattern>login</url-pattern> </servlet-mapping> </web-app>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.