MAVEN Small Project Registration Service (iii)--web module

Source: Internet
Author: User

  Java's web application is generally packaged in the war it differs from the jar in that it contains more resources and files, such as JSP files, static resource files, Servlets, and so on. The core of the war package is the Web-inf folder must have a Web. XML configuration file, the subdirectory classes contains all of the project's classes, and the subdirectory Lib contains all the dependencies, both of which are added to classpath at run time.

MAVEN has a unified format for Web projects. The project code and resources are still placed under Src/main/java and Src/main/resources, and the Web resource directory is in src/main/webapp/. WebApp contains Web-inf,css, JS, JSP, and so on folder.

Here the Account-service module is integrated before three modules, which provide the total service, directly look at the code:

Signuprequest information for the corresponding form:

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;}}

interface implementation:

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> ();p ublic 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 (Accountema Ilexception e) {throw new Accountserviceexception ("Unable to send email", e);} catch (Accountpersistexception e) {throw NE W 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.readac Count (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);}}

note in the service configuration file, the pom must email, captcha, Persist these three modules are included in the dependency relationship:

<dependency> <groupId>${project.groupId}</groupId> <ar Tifactid>account-email</artifactid> <version>${project.version}</version> </dependency&    Gt <dependency> <groupId>${project.groupId}</groupId> <artifactid>account-persist</artif actid> <version>${project.version}</version> </dependency> <dependency> <gro Upid>${project.groupid}</groupid> <artifactId>account-captcha</artifactId> <version>$ {project.version}</version> </dependency> <dependency> <groupid>com.icegreen</groupi d> <artifactId>greenmail</artifactId> <version>${greenmail.version}</version> &L T;scope>test</scope> </dependency> 

Account-web module:

The POM relies on the Servlet,service module, and the other configuration is the same as the general Maven project.

<dependencies> <dependency> <groupid>${project.groupid}& Lt;/groupid> <artifactId>account-service</artifactId> <version>${project.version}</versi on> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactid& Gt;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</artifacti D> </dependency> </dependencies> 

web.xml: Defines some servlets, specific servlet implementation code in Src/main Under the/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>

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 (40            0, "incomplete parameter");        Return        } accountservice service = (accountservice) context.getbean ("Accountservice");            try {service.login (ID, password);        Resp.getwriter (). Print ("Login successful!"); }        catch (Accountserviceexception e) {resp.senderror (+, E.getmessage ()); }    }}

  There are only two of JSP interfaces: Login.jsp and signup.jsp This is easy to do with Web Access.

Basically these things can explain the benefits and use of Maven, after the book's explanation and their actual practice, the use of MAVEN will be more familiar with, of course, the only use of Maven's fur, its function is not only these, these are just its core function. If you can use in the actual project, and to learn the words will have a deeper understanding of MAVEN, in view of the time, I can only understand a general, and then if necessary to quickly use, but also hope to be able to use this tool in the lab project. (If you have the opportunity and time, try refactoring the Web project code before the lab).

MAVEN Small Project Registration Service (iii)--web module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.