【JAVAWEB學習筆記】網上商城實戰:環境搭建和完成使用者模組,javaweb學習筆記

來源:互聯網
上載者:User

【JAVAWEB學習筆記】網上商城實戰:環境搭建和完成使用者模組,javaweb學習筆記
網上商城實戰

今日任務

  • 完成使用者模組的功能
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:

 

聯繫我們

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