บทความนี้ส่วนใหญ่วิเคราะห์โมดูลผู้ใช้ของ Book Mall สำหรับทุกคนเนื้อหาเฉพาะมีดังนี้
1. การสร้างคลาสที่เกี่ยวข้องของโมดูลผู้ใช้
โดเมน: ผู้ใช้
dao: userdao
บริการ: userdao
web.servlet: Userservlet
2. การลงทะเบียนผู้ใช้
2.1 กระบวนการลงทะเบียน
/jsps/user/regist.jsp -> userservlet#regist () -> msg.jsp
2.2 หน้าการลงทะเบียน
<%@ page language = "java" import = "java.util.*" pageencoding = "utf-8"%> <%@ taglib คำนำหน้า = "c" uri = "http://java.sun.com/jsp/jstl/core"%> transitional // en "> <html> <head> <title> ลงทะเบียน </title> <meta http-equiv =" pragma "content =" no-cache "> <meta http-equiv =" cache-control "content =" no-cache "> <meta http-equiv =" expires " content = "keyword1, keyword2, keyword3"> <meta http-equiv = "คำอธิบาย" content = "นี่คือหน้าของฉัน"> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"> <! <body> <h1> ลงทะเบียน </h1> <%-1. แสดงข้อผิดพลาด-> ข้อผิดพลาดของฟิลด์ 2. แสดงข้อผิดพลาดข้อยกเว้น 3. echo-%> <p style = "สี: สีแดง; font-weight: 900"> $ {msg} </p> <form action = " ชื่อผู้ใช้: <อินพุต type = "text" name = "username" value = "$ {form.username}"/> <span style = "สี: สีแดง; font-weight: 900"> $ {errors.username} </span> <br/> รหัสผ่าน: <อินพุตประเภท = "รหัสผ่าน" ชื่อ "รหัสผ่าน" font-weight: 900 "> $ {errors.password} </span> <br/> อีเมล: <อินพุต type =" text "name =" email "value =" $ {form.email} "/> <span style =" สีแดง; font-weight: 900 "> $ {errors.email} </span> <br/> <อินพุต type =" subment "value =" register "/> </form> </body> </html>2.3 UserserVlet
ผู้ใช้
/*** วัตถุโดเมนของผู้ใช้*/ผู้ใช้คลาสสาธารณะ {/** ตารางฐานข้อมูลที่สอดคล้องกัน*/สตริงส่วนตัว uid; // คีย์หลักสตริงส่วนตัวชื่อผู้ใช้ชื่อผู้ใช้; // ชื่อผู้ใช้รหัสผ่านสตริงส่วนตัว;ฐาน
Baseservlet ระดับสาธารณะขยาย httpservlet { / * * มันจะตัดสินใจว่าวิธีการของคลาสนี้เรียกว่าตามวิธีการในคำขอ * / การป้องกันการบริการ (httpservletrequest req, httpservletResponse res) การโยน servletexception, ioexception res.setContentType ("text/html; charset = utf-8"); ลอง {// ตัวอย่าง: http: // localhost: 8080/demo1/xxx? m = เพิ่มวิธีการสตริง = req.getParameter ("วิธี"); // เป็นวิธี nameclass c = this.getClass (); วิธี m = c.getMethod (วิธีการ httpservletrequest.class, httpservletresponse.class); String result = (String) M.Invoke (this, req, res); if (result! = null &&! result.isEmpty ()) {req.getRequestDispatcher (ผลลัพธ์). forward (req, res); }} catch (exception e) {โยน servletexception ใหม่ (e); -userservlet
/*** เลเยอร์นิพจน์ผู้ใช้*/ผู้ใช้ระดับสาธารณะผู้ใช้งานขยาย BasesERVLET {ผู้ใช้ส่วนตัวผู้ใช้บริการ userservice = ใหม่ UserserVice (); / ** * ฟังก์ชั่นออก * @param คำขอ * @param Response * @return * @throws servletexception * @throws ioexception */ สตริงสาธารณะเลิก (httpservletrequest Request, httpservletResponse) โยน servletexception, iOexception {request.getSession () return "r: /index.jsp"; } การเข้าสู่ระบบสตริงสาธารณะ (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception { / * * 1. ห่อหุ้มข้อมูลฟอร์มลงในรูปแบบ * 2 ป้อนการตรวจสอบ (ไม่มีการเขียน) * 3 บริการโทรเพื่อการเปิดใช้งาน *> บันทึกข้อมูลข้อผิดพลาด Commonutils.tobean (request.getParameterMap (), user.class); ลอง {user user = userservice.login (แบบฟอร์ม); request.getSession (). setAttribute ("session_user", ผู้ใช้); /** เพิ่มตะกร้าสินค้าให้กับผู้ใช้นั่นคือบันทึกวัตถุรถเข็นลงในเซสชัน*/ request.getSession () SetAttribute ("รถเข็น", รถเข็นใหม่ ()); return "r: /index.jsp"; } catch (userException e) {request.setAttribute ("msg", e.getMessage ()); request.setAttribute ("แบบฟอร์ม", แบบฟอร์ม); return "f: /jsps/user/login.jsp"; }} /** * ฟังก์ชั่นการเปิดใช้งาน * @param Request * @param Response * @return * @throws servletexception * @throws ioexception * /สตริงสาธารณะที่ใช้งานอยู่ (httpservletRequest Request, httpservletResponse) การส่งข้อมูลการทำงานของ ServleTexception โดเมนและส่งต่อไปยัง msg.jsp * 3. บันทึกข้อมูลความสำเร็จไปยังโดเมนคำขอและส่งต่อไปยัง msg.jsp */ string code = request.getParameter ("รหัส"); ลอง {userservice.active (รหัส); request.setAttribute ("msg", "ขอแสดงความยินดีคุณได้เปิดใช้งานสำเร็จแล้ว! โปรดเข้าสู่ระบบทันที!"); } catch (userException e) {request.setAttribute ("msg", e.getMessage ()); } return "f: /jsps/msg.jsp"; } /** * ฟังก์ชั่นการลงทะเบียน * @param Request * @param Response * @return * @throws servletexception * @throws ioexception * /public String regist (httpservletrequest Regification * การตอบสนอง * 2 ข้อมูลฟิลด์แบบฟอร์มเพื่อขอส่งต่อไปยัง register.jsp * 4. โทรวิธีการบริการเพื่อให้การลงทะเบียนเสร็จสิ้น *> บันทึกข้อมูลข้อผิดพลาดแบบฟอร์มเพื่อขอฟิลด์ส่งต่อไปยัง register.jsp * 5. ส่งอีเมล * 6. บันทึกข้อมูลที่ประสบความสำเร็จไปยัง msg.jsp * // // // สมบูรณ์แบบ setuid (conmonutils.uuid ()); Form.setCode (Commonutils.uuid ()) + Commonutils.uuid ()); /** ป้อนการตรวจสอบ* 1. สร้างแผนที่เพื่อห่อหุ้มข้อมูลข้อผิดพลาดโดยที่คีย์คือชื่อฟิลด์ฟิลด์และค่าคือข้อความแสดงข้อผิดพลาด*/ แผนที่ <สตริงสตริง> ข้อผิดพลาด = ใหม่ hashmap <สตริงสตริง> (); /** 2. รับชื่อผู้ใช้รหัสผ่านและอีเมลในแบบฟอร์มสำหรับการตรวจสอบ*/ string username = form.getUserName (); ถ้า (ชื่อผู้ใช้ == null || username.trim (). isempty ()) {errors.put ("ชื่อผู้ใช้", "ชื่อผู้ใช้ไม่สามารถว่างเปล่า!"); } else ถ้า (username.length () <3 || username.length ()> 10) {errors.put ("ชื่อผู้ใช้", "ชื่อผู้ใช้ต้องอยู่ระหว่าง 3 และ 10!"); } สตริงรหัสผ่าน = form.getPassword (); ถ้า (รหัสผ่าน == null || password.trim (). isempty ()) {errors.put ("รหัสผ่าน", "รหัสผ่านไม่สามารถว่างเปล่า!"); } อื่นถ้า (password.length () <3 || password.length ()> 10) {errors.put ("รหัสผ่าน", "รหัสผ่านต้องอยู่ระหว่าง 3 ~ 10!"); } string email = form.getEmail (); if (email == null || email.trim (). isempty ()) {errors.put ("อีเมล", "อีเมลไม่สามารถว่างเปล่า!"); } else if (! email.matches ("// w+@//w+//.//w+")) {errors.put ("อีเมล", "ข้อผิดพลาดรูปแบบอีเมล!"); }/** 3. ตรวจสอบว่ามีข้อความแสดงข้อผิดพลาด*/ if (errors.size ()> 0) {// 1. บันทึกข้อความแสดงข้อผิดพลาด // 2. บันทึกข้อมูลฟอร์ม // 3. ส่งต่อไปยัง regist.jsp request.setAttribute ("ข้อผิดพลาด", ข้อผิดพลาด); request.setAttribute ("แบบฟอร์ม", แบบฟอร์ม); return "f: /jsps/user/regist.jsp"; } / * * เรียกวิธีการลงทะเบียนของบริการ () * / ลอง {userservice.regist (แบบฟอร์ม); } catch (userException e) { / * * 1. บันทึกข้อมูลข้อยกเว้น * 2. บันทึกแบบฟอร์ม * 3. ส่งต่อไปยัง regist.jsp * / request.setAttribute ("msg", e.getMessage ()); request.setAttribute ("แบบฟอร์ม", แบบฟอร์ม); return "f: /jsps/user/regist.jsp"; } /** ส่งอีเมล* เตรียมไฟล์การกำหนดค่า! */ // รับคุณสมบัติเนื้อหาไฟล์การกำหนดค่าอุปกรณ์ประกอบการ = คุณสมบัติใหม่ (); props.load (this.getClass (). getClassLoader () .getResourceasstream ("email_template.properties")); สตริงโฮสต์ = props.getProperty ("โฮสต์"); // รับเซิร์ฟเวอร์โฮสต์โฮสต์ uname = props.getProperty ("uname"); // รับชื่อผู้ใช้สตริง pwd = props.getProperty ("pwd"); props.getProperty ("หัวเรื่อง"); // รับเนื้อหาสตริงหัวเรื่อง = props.getProperty ("เนื้อหา"); // รับเนื้อหาเนื้อหาจดหมาย = messageFormat.format (เนื้อหา, form.getCode ()); // แทนที่ {0} เซสชันเซสชัน = mailutils.createSession mailutils.send (เซสชัน, จดหมาย); // ส่งอีเมล! } catch (messagingException e) {} / * * 1. บันทึกข้อมูลความสำเร็จ * 2. ส่งต่อไปยัง msg.jsp * / request.setAttribute ("msg", "ขอแสดงความยินดี, การลงทะเบียนสำเร็จ! โปรดเปิดใช้งานในที่อยู่อีเมลทันที"); return "f: /jsps/msg.jsp"; -2.4 Userservice
/*** เลเยอร์ธุรกิจผู้ใช้*/ผู้ใช้ระดับสาธารณะ userservice {UserDao ส่วนตัว userDao = ใหม่ userDao (); / *** ฟังก์ชั่นการลงทะเบียน* @param แบบฟอร์ม*/ การลงทะเบียนโมฆะสาธารณะ (แบบฟอร์มผู้ใช้) โยน userException {// ยืนยันผู้ใช้ชื่อผู้ใช้ผู้ใช้ = userdao.findbyUserName (form.getUserName ()); ถ้า (ผู้ใช้! = null) โยน userException ใหม่ ("ชื่อผู้ใช้ได้รับการลงทะเบียนแล้ว!"); // ยืนยันผู้ใช้อีเมล = userDao.FindByEmail (form.getEmail ()); ถ้า (ผู้ใช้! = null) โยน userException ใหม่ ("อีเมลได้รับการลงทะเบียนแล้ว!"); // แทรกผู้ใช้ลงในฐานข้อมูล userdao.add (แบบฟอร์ม); } / ** * ฟังก์ชั่นการเปิดใช้งาน * @throws userException * / โมฆะสาธารณะที่ใช้งานอยู่ (รหัสสตริง) พ่น userException { / * * 1. ใช้รหัสเพื่อสอบถามฐานข้อมูลและรับผู้ใช้ * / ผู้ใช้ผู้ใช้ = userDao.FindByCode (รหัส); /** 2. หากไม่มีผู้ใช้หมายความว่ารหัสการเปิดใช้งานไม่ถูกต้อง*/ ถ้า (user == null) โยน userException ใหม่ ("รหัสการเปิดใช้งานไม่ถูกต้อง!"); / * * 3. ตรวจสอบว่าสถานะของผู้ใช้ไม่ได้ใช้งานหรือไม่ หากเปิดใช้งานหมายความว่าเป็นการเปิดใช้งานรองและมีการโยนข้อยกเว้น*/ ถ้า (user.isstate ()) โยน userexception ใหม่ ("คุณได้รับการเปิดใช้งานแล้วอย่าเปิดใช้งานอีกครั้งเว้นแต่คุณต้องการตาย!"); /** 4. แก้ไขสถานะของผู้ใช้*/ userdao.updatestate (user.getuid (), จริง); } /** * ฟังก์ชั่นการเข้าสู่ระบบ * @param แบบฟอร์ม * @return * @throws userexception * /การเข้าสู่ระบบผู้ใช้สาธารณะ (แบบฟอร์มผู้ใช้) โยน userException { / * * 1 ใช้ชื่อผู้ใช้เพื่อสอบถามเพื่อรับผู้ใช้ * 2 ถ้าผู้ใช้เป็นโมฆะ หากแตกต่างกันข้อยกเว้นจะถูกโยน (รหัสผ่านไม่ถูกต้อง) * 4 ตรวจสอบสถานะของผู้ใช้ หากเป็นเท็จข้อยกเว้นจะถูกโยน (ยังไม่เปิดใช้งาน) * 5. ส่งคืนผู้ใช้ */ ผู้ใช้ผู้ใช้ = userDao.FindByUserName (form.getUserName ()); ถ้า (user == null) โยน userException ใหม่ ("ชื่อผู้ใช้ไม่มีอยู่!"); if (! user.getPassword (). เท่ากับ (form.getPassword ())) โยน userException ใหม่ ("ข้อผิดพลาดรหัสผ่าน!"); if (! user.isstate ()) โยน userexception ใหม่ ("ยังไม่เปิดใช้งาน!"); ผู้ใช้ส่งคืน; -2.5 userdao
/*** เลเยอร์การคงอยู่ของผู้ใช้*/คลาสสาธารณะ UserDao {Private QueryRunner QR = ใหม่ txQueryRunner (); / ** * Query โดยชื่อผู้ใช้ * @param ชื่อผู้ใช้ * @return */ ผู้ใช้สาธารณะ findByUserName (ชื่อผู้ใช้สตริง) {ลอง {string sql = "เลือก * จาก tb_user ที่ชื่อผู้ใช้ =?"; return qr.Query (SQL, New BeanHandler <user> (user.class), ชื่อผู้ใช้); } catch (sqlexception e) {โยน runtimeException ใหม่ (e); }} / ** * แบบสอบถามทางอีเมล * @param อีเมล * @return * / ผู้ใช้สาธารณะ findByeMail (อีเมลสตริง) {ลอง {string sql = "เลือก * จาก tb_user ที่อีเมล =?"; return qr.Query (SQL, New BeanHandler <user> (user.class), อีเมล); } catch (sqlexception e) {โยน runtimeException ใหม่ (e); }} / ** * แทรกผู้ใช้ * @param ผู้ใช้ * / โมฆะสาธารณะเพิ่ม (ผู้ใช้ผู้ใช้) {ลอง {string sql = "แทรกลงในค่า tb_user (?,?,?,?,?,?)"; Object [] params = {user.getUid (), user.getUserName (), user.getPassword (), user.getEmail (), user.getCode (), user.isstate ()}; qr.update (sql, params); } catch (sqlexception e) {โยน runtimeException ใหม่ (e); }} / ** * Query โดยรหัสเปิดใช้งาน * @param code * @return * / ผู้ใช้สาธารณะ findByCode (รหัสสตริง) {ลอง {string sql = "เลือก * จาก tb_user โดยที่รหัส =?"; return qr.Query (SQL, ใหม่ beanHandler <user> (user.class), รหัส); } catch (sqlexception e) {โยน runtimeException ใหม่ (e); }} / ** * แก้ไขสถานะที่ระบุของผู้ใช้ที่ระบุ * @param uid * @param state * / โมฆะสาธารณะ updateState (String uid, สถานะบูลีน) {ลอง {string sql = "อัปเดต tb_user set state =? โดยที่ uid =?"; qr.update (sql, state, uid); } catch (sqlexception e) {โยน runtimeException ใหม่ (e); -3. การเปิดใช้งานผู้ใช้
กระบวนการ: อีเมลของผู้ใช้ -> UserserVlet#active () -> msg.jsp
4.
เข้าสู่ระบบผู้ใช้
กระบวนการ: /jsps/user/login.jsp -> userservlet#login () -> index.jsp
5.
ทางออกของผู้ใช้
กระบวนการ: top.jsp -> userservlet#quit () -> login.jsp
ออก (): ทำลายเซสชัน!
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น