【JAVAWEB學習筆記】網上商城實戰1:環境搭建和完成用戶模塊


今日任務

  • 完成用戶模塊的功能

1.1      網上商城的實戰:

1.1.1    演示網上商城的功能:

 

 

1.1.2    制作目的:

 靈活運用所學知識完成商城實戰。

1.1.3    數據庫分析和設計:

 

 

1.1.4    代碼實現:

1.1.4.1  通用的Servlet的編寫:

傳統的方式:

傳統的方式:
* 一個請求對應一個Servlet.
* 能不能一個模塊對應一個Servlet.

一個模塊對應一個Servlet:
<a href=”/UserServlet?method=add”>添加</a>
<a href=”/UserServlet?method=update”>修改</a>
<a href=”/UserServlet?method=delete”>刪除</a>

public class UserServlet extends HttpServlet{

   public void service(HttpServletRequest req,HttpServletResponse resp){
           String method = req.getParameter(“method”);
           if(“add”.equals(method)){
               add(req,resp);
           }else if(“update”.equals(method)){
               update(req,resp);
           }
   }

   public void add(HttpServletRequest req,HttpServletResponse resp){
   
   }

   public void update(HttpServletRequest req,HttpServletResponse resp){
   
   }

}

改進以后:
public class BaseServlet extends HttpServlet{
   public void service(HttpServletRequest req,HttpServletResponse resp){
           String methodName = req.getParameter(“method”);
           // 反射:
           Class clazz = this.getClass(); // 指代的是子類的對象.
           Method method = clazz.getMethod(methodName ,HttpServletRequest.class,HttpServletResponse,class);
           method.invoke(this,req,resp);
   }

}


public class UserServlet extends BaseServlet{

   public void add(HttpServletRequest req,HttpServletResponse resp){
   
   }

   public void update(HttpServletRequest req,HttpServletResponse resp){
   
   }

}

public class A{
   public A(){
      System.out.println(this.getClass());
   }
}

public class B extends A{
    Public B(){

    }
}

public class Test{
   Public static void main(String[] args){
       B b = new B();
   }
}

 

1.1.4.2  BaseServlet的代碼實現:

【創建包結構】

 

【代碼實現】

public class BaseServlet extends HttpServlet{

 

    @Override

    // http://loacalhost:8080/store/UserServlet?method=add

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 處理Post請求的亂碼

        req.setCharacterEncoding("UTF-8");

        // 接收參數:

        String methodName = req.getParameter("method");

        // 反射獲得類的字節碼.

        Class clazz = this.getClass();

        // 獲得正在執行的類的指定名稱的方法

        try {

            Method method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);

            // 讓這個方法執行:

            String path = (String) method.invoke(this, req,resp);

            if(path != null){

                req.getRequestDispatcher(path).forward(req, resp);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

 

}

 

1.1.4.3  環境的搭建:

【創建一個WEB工程】

【創建包結構】

【引入相應的jar包】

    * mysql驅動          1

    * dbutils            1

    * c3p0連接池         1

    * beanutils          2

    * JSTL               2

    * 文件上傳            2

    * 發送郵件            1

【創建數據庫和表】

CREATE TABLE `user` (

  `uid` varchar(32) NOT NULL,

  `username` varchar(20) DEFAULT NULL,

  `password` varchar(20) DEFAULT NULL,

  `name` varchar(20) DEFAULT NULL,

  `email` varchar(30) DEFAULT NULL,

  `telephone` varchar(20) DEFAULT NULL,

  `birthday` varchar(20) DEFAULT NULL,

  `sex` varchar(10) DEFAULT NULL,

  `state` int(11) DEFAULT NULL,

  `code` varchar(64) DEFAULT NULL,

  PRIMARY KEY (`uid`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 

1.2      前台用戶模塊:

1.2.1    注冊

1.2.1.1  異步校驗用戶名是否存在

1.JS的事件觸發一個函數.

2.使用AJAX異步向服務器發送請求.

3.得到返回的數據進行判斷.

4.將信息寫入到文本框后面span元素中.

1.2.1.2  完成用戶注冊

1.在注冊頁面中輸入信息.

2.點擊注冊,提交到Servlet.

3.接收數據,封裝數據.

4.在Servlet調用業務層.

5.頁面跳轉.

1.2.1.3  發送激活郵件

1.使用JavaMail的技術-了解.

* 了解郵件相關內容:

    * 術語:

        * 電子郵箱:一個郵箱服務器上的一個賬號,通過賬號獲得到服務器上一塊空間.

        * 郵箱服務器:一台電腦安裝一個郵箱的服務器.

        * 郵件收發的協議:協議規范雙方的數據的格式.

    * 協議:

        * 接收:POP/POP3  IMAP

        * 發送:SMTP

    * 郵件收發過程:

    * 會配置郵箱的客戶端:

【配置郵箱服務器:】

 

 

安裝成功!

第一步:

 點擊工具→服務器設置

第二步:

 

 

第三步:

 點擊賬號→新建賬號

【配置郵箱的客戶端】

* Foxmail:免費的

    * 輸入用戶名

    * 輸入密碼:

    * 修改服務器的地址localhost.

* outlook:微軟的收費的.

1.2.2    激活

在郵箱的界面點擊激活鏈接:

提交到Servlet傳遞一個激活碼:

根據激活碼進行查詢用戶:

* 如果查詢到該用戶:修改用戶的狀態.

* 如果沒有查詢到該用戶:激活失敗.

1.2.3    登錄

在登錄頁面上輸入用戶名和密碼

點擊提交:提交到Servlet.

在Servlet中接收參數

調用業務層

頁面跳轉

1.2.3.1  記住用戶名

使用Cookie記住用戶名:

* 自己完成

1.2.3.2  自動登錄

使用Cookie記住用戶名和密碼:

使用過濾器:

* 自己完成

1.2.4    退出

* 在首頁上點擊退出的鏈接:

* 提交到Servlet:

    * 銷毀session:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM