/** * User Presentation layer/public class Userservlet extends Baseservlet {private UserService userservice = new UserService ();
/** * Exit Function * @param request * @param response * @return * @throws servletexception * @throws IOException * *
Public String Quit (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {
Request.getsession (). invalidate ();
return "r:/index.jsp"; Public String Login (httpservletrequest request, httpservletresponse response) throws Servletexception, IOException {/* * 1. Encapsulate form data into form * 2. Input checksum (NO) * 3. Invoke service completes activation * > Save error message, form to request, forward to login.jsp * 4. Save with
User information into session, then redirect to index.jsp/User form = Commonutils.tobean (Request.getparametermap (), user.class);
try {User user = Userservice.login (form);
Request.getsession (). setattribute ("Session_user", user);
* * To add a shopping cart to the user, that is, save a Cart object to the session/Request.getsession (). setattribute ("cart", new cart ()); REturn "r:/index.jsp";
catch (Userexception e) {request.setattribute ("msg", E.getmessage ());
Request.setattribute ("form", form);
return "f:/jsps/user/login.jsp"; }/** * Activation function * @param request * @param response * @return * @throws servletexception * @throws ioexceptio n */Public String Active (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexcep tion {* * 1. Get parameter Activation code * 2. Invoke the service method to complete activation * > Save exception information to request field and forward to msg.jsp * 3. Save Success information to request field, forward to Msg.js
p */String code = request.getparameter ("code");
try {userservice.active (code); Request.setattribute ("msg", "Congratulations, your activation is successful!") Please login now!
");
catch (Userexception e) {request.setattribute ("msg", E.getmessage ());
Return to "f:/jsps/msg.jsp";
/** * Registration Function * @param request * @param response * @return * @throws servletexception * @throws IOException */Public String regist (httpservletrequest request, HttpServletResponse responsE) throws Servletexception, IOException {/* 1. Encapsulate form data to form Object * 2. Complement: UID, code * 3. Input CHECKSUM * > Save error message , form to request field, forwarded to REGIST.JSP * 4. Call service method completes registration * > Save error message, form to request field, forward to regist.jsp * 5. Send mail * 6.
Save success message forward to msg.jsp//Encapsulate form data User form = Commonutils.tobean (Request.getparametermap (), user.class);
Complement full Form.setuid (Commonutils.uuid ());
Form.setcode (Commonutils.uuid () + commonutils.uuid ()); * * Input CHECKSUM * 1.
Create a MAP that encapsulates the error message, where key is the form field name, and the value is the error message/map<string,string> errors = new hashmap<string,string> (); * * 2.
Get the username, password, email to verify the form */String username = Form.getusername (); if (username = null | | Username.trim (). IsEmpty ()) {Errors.put ("username", "user name cannot be empty!")
"); else if (Username.length () < 3 | | username.length () > Ten) {errors.put ("username", "user name length must be between 3~10!")
");
String password = Form.getpassword (); if (password = null | | Password.trim (). IsEmpty ()) {Errors.puT ("Password", "Password cannot be empty!")
"); else if (Password.length () < 3 | | password.length () > Ten) {errors.put ("password", "password length must be between 3~10!")
");
String email = form.getemail (); if (email = null | | Email.trim () isempty ()) {errors.put ("email", "email cannot be empty!")
"); else if (!email.matches ("\\w+@\\w+\\.\\w+")) {errors.put ("email", "email format is wrong!")
"); } * * 3. Determine if there is an error message */if (errors.size () > 0) {//1. Save error message//2. Save form data//3. Forward to regist.jsp Request.setattribu
Te ("Errors", errors);
Request.setattribute ("form", form);
return "f:/jsps/user/regist.jsp";
* * * the Regist () method for invoking the service is * * */try {userservice.regist (form); catch (Userexception e) {/* 1. Save exception Information * 2. Save form * 3. Forward to regist.jsp * * Request.setattribute ("M
SG ", E.getmessage ());
Request.setattribute ("form", form);
return "f:/jsps/user/regist.jsp";
* * * Send mail * prepare configuration file!
//Get configuration file content Properties props = new properties (); Props.load (this.GetClass (). getClassLoader (). getResourceAsStream ("email_template.properties")); String host = Props.getproperty ("host");/Get server host string uname = Props.getproperty ("uname");/get user name string pwd = Props . GetProperty ("pwd");/get password string from = Props.getproperty (' from ');//Get sender string to = Form.getemail ();//Get Recipient Strin G subject = Props.getproperty ("subject"); Get the subject String content = Props.getproperty ("content")//Get message content = Messa Geformat.format (content, Form.getcode ()),//replace {0} session session = Mailutils.createsession (host, uname, PWD);
Get session mail = new mail (from, to, subject, content);//Create a Mail object try {mailutils.send (session, mail);/e-mail! catch (Messagingexception e) {} * * 1. Save Success Information * 2. Forward to msg.jsp/Request.setattribute ("MSG", "Congratulations, registration success!")
Please immediately to the mailbox activation ");
return "f:/jsps/msg.jsp";
}
}