【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: