|
| 1 | +package com.juvenxu.mvnbook.account.www; |
| 2 | + |
| 3 | +import com.juvenxu.mvnbook.account.service.AccountService; |
| 4 | +import com.juvenxu.mvnbook.account.service.AccountServiceException; |
| 5 | +import com.juvenxu.mvnbook.account.service.SignUpRequest; |
| 6 | +import org.springframework.context.ApplicationContext; |
| 7 | +import org.springframework.web.context.support.WebApplicationContextUtils; |
| 8 | + |
| 9 | +import javax.servlet.ServletException; |
| 10 | +import javax.servlet.http.HttpServlet; |
| 11 | +import javax.servlet.http.HttpServletRequest; |
| 12 | +import javax.servlet.http.HttpServletResponse; |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +/** |
| 16 | + * Created by zhangbin on 16/2/14. |
| 17 | + */ |
| 18 | +public class SignUpServlet extends HttpServlet { |
| 19 | + private static final long serialVersionUID = 4784742296013868199L; |
| 20 | + private ApplicationContext context; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void init() throws ServletException { |
| 24 | + super.init(); |
| 25 | + context = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 30 | + String id = req.getParameter("id"); |
| 31 | + String email = req.getParameter("email"); |
| 32 | + String name = req.getParameter("name"); |
| 33 | + String password = req.getParameter("password"); |
| 34 | + String confirmPassword = req.getParameter("confirm_password"); |
| 35 | + String captchaKey = req.getParameter("captcha_key"); |
| 36 | + String captchaValue = req.getParameter("captcha_value"); |
| 37 | + |
| 38 | + if (id == null || id.length() == 0 || email == null || email.length() == 0 || name == null || name.length() == 0 |
| 39 | + || password == null || password.length() == 0 || confirmPassword == null || confirmPassword.length() == 0 |
| 40 | + || captchaKey == null || captchaKey.length() == 0 || captchaValue == null || captchaValue.length() == 0) { |
| 41 | + resp.sendError(400, "Parameter Incomplete"); |
| 42 | + return; |
| 43 | + } |
| 44 | + AccountService service = (AccountService) context.getBean("accountService"); |
| 45 | + SignUpRequest request = new SignUpRequest(); |
| 46 | + request.setId(id); |
| 47 | + request.setEmail(email); |
| 48 | + request.setName(name); |
| 49 | + request.setCaptchaKey(captchaKey); |
| 50 | + request.setPassword(password); |
| 51 | + request.setConfirmPassword(confirmPassword); |
| 52 | + request.setCaptchaValue(captchaValue); |
| 53 | + |
| 54 | + request.setActivateServiceUrl(getServletContext().getRealPath("/") + "activate"); |
| 55 | + try { |
| 56 | + service.signUp(request); |
| 57 | + resp.getWriter().print("Account is created, please check your mail box for activation link."); |
| 58 | + } catch (AccountServiceException e) { |
| 59 | + resp.sendError(400, e.getMessage()); |
| 60 | + return; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments