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.xml
classpath:/account-captcha.xml
classpath:/account-email.xml
classpath:/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模块的更多相关文章

  1. maven小项目注册服务(二)--captcha模块

    验证码生成模块,配置信息基本和前面的模块一样.account-captcha需要提供的服务是生成随机的验证码主键,然后用户可以使用这个主键要求服务生成一个验证码图片,这个图片对应的值应该是随机的,最后 ...

  2. maven小项目注册服务(一)--email和persist模块

    跟着书里的讲解,跟着做了一遍该项目: 首先明白注册账户的需求: 账号的lD和Email地址都可以用来唯一地标识某个用户,而显示名称则用来显示在页面下,方便浏览.注册的时候用户还需要输入两次密码,以确保 ...

  3. Maven实现项目构建直接部署Web项目到Tomcat

    Maven实现项目构建直接部署Web项目到Tomcat配置如下: 1.Tomcat的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <ro ...

  4. 一、基础项目构建,引入web模块,完成一个简单的RESTful API

    一.Spring Boot的主要优点: 为所有Spring开发者更快的入门 开箱即用,提供各种默认配置来简化项目配置 内嵌式容器简化Web项目 没有冗余代码生成和XML配置的要求 二.使用maven构 ...

  5. idea结合maven小项目

    整体构造 (修改 POM 文件 )parent <?xml version="1.0" encoding="UTF-8"?> <project ...

  6. 基础项目构建,引入web模块,完成一个简单的RESTful API 转载来自翟永超

    简介 在您第一次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得一堆反复粘贴的配置有一些厌烦?那么您就不妨来试试使用Spring Boot ...

  7. Yii2项目高级模版 三个模块在同一个目录下的重定向配置

    最近做项目用到的,非常好用. 修改 advanced/backend/config/main.PHP 文件如下: return [ 'homeUrl' => '/admin', 'compone ...

  8. Eclipse使用Maven jetty/tomcat:run命令启动web项目

    Eclipse安装好m2e插件,使用Maven构建项目后,启动web项目就行就非常简单了,如下所示. 操作步骤: 1.右键你的项目 -> Run As -> Run Configurati ...

  9. 基于vue 、vue-router 、firebase的todolist小项目

    第一次写博客,都不知道改怎么写的好. 本着一颗学习的心,也希望一段时间后再回来在看看自己写的代码,会不会让自己有种不忍直视的念头 *-* 还是先上图吧~ 这是首页,主要是展示所有的列表页面,可以通过输 ...

随机推荐

  1. 基础才是重中之重~理解linq中的groupby

    linq将大部分SQL语句进行了封装,这使得它们更加面向对象了,对于开发者来说,这是一件好事,下面我从基础层面来说一下GroupBy在LINQ中的使用. 对GroupBy的多字段分组,可以看我的这篇文 ...

  2. java 泛型通配符 extends, super

    引自:http://sharewind.iteye.com/blog/1622164 关键字说明 ? 通配符类型 <? extends T> 表示类型的上界,表示参数化类型的可能是T 或是 ...

  3. 【python】网络爬虫抓取图片

    利用python抓取网络图片的步骤: 1.根据给定的网址获取网页源代码 2.利用正则表达式把源代码中的图片地址过滤出来 3.根据过滤出来的图片地址下载网络图片 今天我们用http://www.umei ...

  4. Encog

    http://www.heatonresearch.com/encog/ https://www.mql5.com/zh/articles/252

  5. Windows 10 响应式设计和设备友好的开发

    使用Effective pixels有效像素设计UI 什么是缩放像素和Effective有效像素: 当你的应用程序运行在Windows的设备,系统用一个算法控制的规范,字体,和其他UI元素显示在屏幕上 ...

  6. WPF简单布局 浅尝辄止

            WPF的窗口只能包含一个元素,为了在WPF窗口中放置多个元素并创建更实用的用户界面,需要在窗口上放置一个容器,然后在容器中放置其它元素. 注意:造成这一限制的原因是window类继承自 ...

  7. sqlserver convert 日期时间 转换格式化

    Select CONVERT(varchar(100), GETDATE(), 120): 2006-05-16 10:57:49   Select CONVERT(varchar(100), GET ...

  8. 【BZOJ】【1565】【NOI2009】PVZ 植物大战僵尸

    网络流/最大权闭合子图+拓扑排序 感动死了>_<,一年多以前刚知道网络流的时候听说了这道名字很带感的题目,现在终于有实力切掉它了. 这题是最大权闭合子图模型的经典应用<_<,首 ...

  9. 【BZOJ】【2594】【WC2006】水管局长数据加强版

    LCT 动态维护MST嘛……但是有删边= =好像没法搞的样子 离线记录所有修改&询问,倒序处理,就可以变删边为加边了- 论如何用LCT维护最小生成树:先搞出一棵最小生成树,然后每次加边(u,v ...

  10. 【POJ】【1741】/【BZOJ】【1468】Tree

    点分治 怎么又一道叫Tree的题目……真是醉了. 本题为漆子超论文<分治算法在树的路径问题中的应用>例一 题解 : http://blog.csdn.net/sdj222555/artic ...