標籤:
java的web應用打包方式一般為war它和jar的區別就是包含了更多的資源和檔案,如JSP檔案,靜態資源檔案,servlet等等。war包的核心就WEB-INF檔案夾下必須有一個web.xml 的設定檔,子目錄classes包含所有該項目的類,子目錄lib包含所有的依賴,這兩個目錄都會在啟動並執行時候添加到classpath下。
maven對於web項目有統一的格式。項目代碼和資源還是放在src/main/java和src/main/resources下,web資來源目錄在src/main/webapp/。webapp下就包含WEB-INF,css, js, jsp,等等檔案夾。
這裡account-service模組是綜合之前三個模組,提供總的服務,直接看代碼:
SignUpRequest對應表單的資訊:
public class SignUpRequest {private String id; private String email; private String name; private String password; private String confirmPassword; private String captchaKey; private String captchaValue; private String activateServiceUrl;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getConfirmPassword() {return confirmPassword;}public void setConfirmPassword(String confirmPassword) {this.confirmPassword = confirmPassword;}public String getCaptchaKey() {return captchaKey;}public void setCaptchaKey(String captchaKey) {this.captchaKey = captchaKey;}public String getCaptchaValue() {return captchaValue;}public void setCaptchaValue(String captchaValue) {this.captchaValue = captchaValue;}public String getActivateServiceUrl() {return activateServiceUrl;}public void setActivateServiceUrl(String activateServiceUrl) {this.activateServiceUrl = activateServiceUrl;}}
介面實現:
public interface AccountService {String generateCaptchaKey() throws AccountServiceException;byte[] generateCaptchaImage(String captchaKey)throws AccountServiceException;void signUp(SignUpRequest signUpRequest) throws AccountServiceException;void activate(String activationNumber) throws AccountServiceException;void login(String id, String password) throws AccountServiceException;}public class AccountServiceImpl implements AccountService {private AccountPersistService accountPersist;private AccountEmailService accountEmail;private AccountCaptchaService accountCaptcha;private Map<String, String> activationMap = new HashMap<String, String>();public AccountPersistService getAccountPersist() {return accountPersist;}public void setAccountPersist(AccountPersistService accountPersist) {this.accountPersist = accountPersist;}public AccountEmailService getAccountEmail() {return accountEmail;}public void setAccountEmail(AccountEmailService accountEmail) {this.accountEmail = accountEmail;}public AccountCaptchaService getAccountCaptcha() {return accountCaptcha;}public void setAccountCaptcha(AccountCaptchaService accountCaptcha) {this.accountCaptcha = accountCaptcha;}public String generateCaptchaKey() throws AccountServiceException {try {return accountCaptcha.generateCaptchaKey();} catch (Exception e) {throw new AccountServiceException("Unable to generate captcha key",e);}}public byte[] generateCaptchaImage(String captchaKey)throws AccountServiceException {try {return accountCaptcha.generateCaptchaImage(captchaKey);} catch (Exception e) {throw new AccountServiceException("Unable to generate captcha image",e);}}public void signUp(SignUpRequest signUpRequest)throws AccountServiceException {try {if (!signUpRequest.getPassword().equals(signUpRequest.getConfirmPassword())) {throw new AccountServiceException("password donnot match");}if (!accountCaptcha.validateCaptcha(signUpRequest.getCaptchaKey(), signUpRequest.getCaptchaValue())) {throw new AccountCaptchaException("captcha not match");}Account account = new Account();account.setId(signUpRequest.getId());account.setEmail(signUpRequest.getEmail());account.setName(signUpRequest.getName());account.setPassword(signUpRequest.getPassword());account.setActivated(false);accountPersist.createAccount(account);String activationId = RandomGenerator.getRandomString();activationMap.put(activationId, account.getId());String link = signUpRequest.getActivateServiceUrl().endsWith("/") ? signUpRequest.getActivateServiceUrl()+activationId : signUpRequest.getActivateServiceUrl()+"?key="+activationId;accountEmail.sendMail(account.getEmail(), "please activate Your email", link);} catch (AccountCaptchaException e) {throw new AccountServiceException("unable to validate captcha", e);} catch (AccountEmailException e) {throw new AccountServiceException("unable to send email", e);} catch (AccountPersistException e) {throw new AccountServiceException("unable to create account", e);}}public void activate(String activationId)throws AccountServiceException {String accountId = activationMap.get(activationId);if (accountId == null) {throw new AccountServiceException("invalid account activated id");}try {Account account = accountPersist.readAccount(accountId);account.setActivated(true);accountPersist.updateAccount(account);} catch (Exception e) {throw new AccountServiceException("Unable to activate");}}public void login(String id, String password)throws AccountServiceException {try {Account account = accountPersist.readAccount(id);if (account == null) {throw new AccountServiceException("account donnot exits");}if (!account.isActivated()) {throw new AccountServiceException("account not activate");}if (!account.getPassword().equals(password)) {throw new AccountServiceException("password is error");}} catch (AccountPersistException e) {throw new AccountServiceException("Unable to logging", e);}}}
注意service的設定檔中pom必須將email、captcha、persist這三個模組包含進去,依賴關係:
<dependency> <groupId>${project.groupId}</groupId> <artifactId>account-email</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>account-persist</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> <artifactId>account-captcha</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>com.icegreen</groupId> <artifactId>greenmail</artifactId> <version>${greenmail.version}</version> <scope>test</scope> </dependency>
account-web模組:
pom需要依賴servlet,service模組,其他的配置和一般的maven項目一樣。
<dependencies> <dependency> <groupId>${project.groupId}</groupId> <artifactId>account-service</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.4</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> </dependencies>
web.xml:定義了一些servlet,具體的servlet實現代碼在src/main/java 下。
<web-app><display-name>Sample Maven Project: Account Service</display-name><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:/account-persist.xmlclasspath:/account-captcha.xmlclasspath:/account-email.xmlclasspath:/account-service.xml</param-value></context-param><servlet><servlet-name>CaptchaImageServlet</servlet-name><servlet-class>com.hust.silence.account.web.CaptchaImageServlet</servlet-class></servlet><servlet><servlet-name>SignUpServlet</servlet-name><servlet-class>com.hust.silence.account.web.SignUpServlet</servlet-class></servlet><servlet><servlet-name>ActivateServlet</servlet-name><servlet-class>com.hust.silence.account.web.ActivateServlet</servlet-class></servlet><servlet><servlet-name>LoginServlet</servlet-name><servlet-class>com.hust.silence.account.web.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>CaptchaImageServlet</servlet-name><url-pattern>/captcha_image</url-pattern></servlet-mapping><servlet-mapping><servlet-name>SignUpServlet</servlet-name><url-pattern>/signup</url-pattern></servlet-mapping><servlet-mapping><servlet-name>ActivateServlet</servlet-name><url-pattern>/activate</url-pattern></servlet-mapping><servlet-mapping><servlet-name>LoginServlet</servlet-name><url-pattern>/login</url-pattern></servlet-mapping></web-app>
這個給出其中一個servlet的代碼:對使用者login的處理。
public class LoginServlet extends HttpServlet{ private static final long serialVersionUID = 929160785365121624L; private ApplicationContext context; @Override public void init() throws ServletException { super.init(); context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() ); } @Override protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException { String id = req.getParameter( "id" ); String password = req.getParameter( "password" ); if ( id == null || id.length() == 0 || password == null || password.length() == 0 ) { resp.sendError( 400, "incomplete parameter" ); return; } AccountService service = (AccountService) context.getBean( "accountService" ); try { service.login( id, password ); resp.getWriter().print( "Login Successful!" ); } catch ( AccountServiceException e ) { resp.sendError( 400, e.getMessage() ); } }}
關於jsp介面只有兩個:login.jsp和signup.jsp這個對於接觸過web 的而言很簡單了。
基本上這些內容大概就可以說明maven的好處和使用方法了,經過書上的講解和自己的實際操練,對maven的使用會更加熟悉,當然自己用到的只是maven的皮毛,它的功能可 不僅僅只有這些,這些只是它的核心功能。如果能在實際項目中運用,並去學習的話會對maven有更深的瞭解,鑒於時間關係,我只能先瞭解個大概,之後若需要的時候能夠快速的使用起來,也希望能在實驗室項目中好好的使用這個工具。(有機會和時間的話,嘗試重構實驗室之前的web項目代碼)。
maven小項目註冊服務(三)--web模組