1.新建Maven Module项目account-web,父项目选中account,生成后,account项目的Pom文件内容为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.iteye.xujava</groupId>
<artifactId>account</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<name>Account</name>
<modules>
<module>account-email</module>
<module>account-persist</module>
<module>account-captcha</module>
<module>account-service</module>
<module>account-web</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>2.5.6</springframework.version>
<junit.version>4.9</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
account-web项目内容的Pom文件内容为:
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iteye.xujava</groupId>
<artifactId>account</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>account-web</artifactId>
<name>Account Web</name>
<packaging>war</packaging>
<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>
</project>
2.在src/main/java的com.iteye.xujava.account.web的包下新建以下4个文件
package com.iteye.xujava.account.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iteye.xujava.account.service.AccountService;
import com.iteye.xujava.account.service.AccountServiceException;
public class ActivateServlet extends HttpServlet {
private static final long serialVersionUID = 3668445055149826106L;
private ApplicationContext context;
@Override
public void init() throws ServletException {
super.init();
context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String key = req.getParameter("key");
if (key == null || key.length() == 0) {
resp.sendError(400, "No activation key provided.");
return;
}
AccountService service = (AccountService) context.getBean("accountService");
try {
service.activate(key);
resp.getWriter().write("Account is activated, now you can login.");
} catch (AccountServiceException e) {
resp.sendError(400, "Unable to activate account");
return;
}
}
}
package com.iteye.xujava.account.web;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iteye.xujava.account.service.AccountService;
import com.iteye.xujava.account.service.AccountServiceException;
public class CaptchaImageServlet extends HttpServlet {
private ApplicationContext context;
private static final long serialVersionUID = 5274323889605521606L;
@Override
public void init() throws ServletException {
super.init();
context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String key = request.getParameter("key");
if (key == null || key.length() == 0) {
response.sendError(400, "No Captcha Key Found");
} else {
AccountService service = (AccountService) context.getBean("accountService");
try {
response.setContentType("image/jpeg");
OutputStream out = response.getOutputStream();
out.write(service.generateCaptchaImage(key));
out.close();
} catch (AccountServiceException e) {
response.sendError(400, e.getMessage());
}
}
}
}
package com.iteye.xujava.account.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iteye.xujava.account.service.AccountService;
import com.iteye.xujava.account.service.AccountServiceException;
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());
}
}
}
package com.iteye.xujava.account.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.iteye.xujava.account.service.AccountService;
import com.iteye.xujava.account.service.AccountServiceException;
import com.iteye.xujava.account.service.SignUpRequest;
public class SignUpServlet extends HttpServlet {
private static final long serialVersionUID = 4784742296013868199L;
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 email = req.getParameter("email");
String name = req.getParameter("name");
String password = req.getParameter("password");
String confirmPassword = req.getParameter("confirm_password");
String captchaKey = req.getParameter("captcha_key");
String captchaValue = req.getParameter("captcha_value");
if (id == null || id.length() == 0 || email == null || email.length() == 0 || name == null || name.length() == 0 || password == null
|| password.length() == 0 || confirmPassword == null || confirmPassword.length() == 0 || captchaKey == null
|| captchaKey.length() == 0 || captchaValue == null || captchaValue.length() == 0) {
resp.sendError(400, "Parameter Incomplete.");
return;
}
AccountService service = (AccountService) context.getBean("accountService");
SignUpRequest request = new SignUpRequest();
request.setId(id);
request.setEmail(email);
request.setName(name);
request.setPassword(password);
request.setConfirmPassword(confirmPassword);
request.setCaptchaKey(captchaKey);
request.setCaptchaValue(captchaValue);
request.setActivateServiceUrl(getServletContext().getRealPath("/") + "activate");
try {
service.signUp(request);
resp.getWriter().print("Account is created, please check your mail box for activation link.");
} catch (AccountServiceException e) {
resp.sendError(400, e.getMessage());
return;
}
}
}
3. 新建src/main/webapp源码目录,在目录下新建login.jsp和signup.jsp,新建WEB-INF,在其下新建web.xml.
xml内容为:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <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.iteye.xujava.account.web.CaptchaImageServlet</servlet-class> </servlet> <servlet> <servlet-name>SignUpServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.SignUpServlet</servlet-class> </servlet> <servlet> <servlet-name>ActivateServlet</servlet-name> <servlet-class>com.iteye.xujava.account.web.ActivateServlet</servlet-class> </servlet> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.iteye.xujava.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> <welcome-file-list> <welcome-file>signup.jsp</welcome-file> </welcome-file-list> </web-app>
login.jsp内容为:
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<html>
<head>
<style type="text/css">
.text-field {position: absolute; left: 40%; background-color:rgb(255,230,220);}
label {display: inline-table; width: 90px; margin: 0px 0px 10px 20px; }
input {display: inline-table; width: 150px; margin: 0px 20px 10px 0px;}
h2 {margin: 20px 20px 20px 40px;}
button {margin: 20px 20px 10px 110px}
</style>
</head>
<body>
<div class="text-field">
<h2>账户登录</h2>
<form name="login" action="login" method="post">
<label>账户ID:</label><input type="text" name="id"></input><br/>
<label>密码:</label><input type="password" name="password"></input><br/>
<button>确认并提交</button>
</form>
</div>
</body>
</html>
signup.jsp内容如下:
<%@ page contentType="text/html; charset=gb2312" language="java" %>
<%@ page import="com.iteye.xujava.account.service.*,
org.springframework.context.ApplicationContext,
org.springframework.web.context.support.WebApplicationContextUtils"%>
<html>
<head>
<style type="text/css">
.text-field {position: absolute; left: 40%; background-color:rgb(255,230,220);}
label {display: inline-table; width: 90px; margin: 0px 0px 10px 20px; }
input {display: inline-table; width: 150px; margin: 0px 20px 10px 0px;}
img {width:150px; margin: 0px 20px 10px 110px;}
h2 {margin: 20px 20px 20px 40px;}
button {margin: 20px 20px 10px 110px}
</style>
</head>
<body>
<%
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext( getServletContext() );
AccountService accountervice = (AccountService) context.getBean( "accountService" );
String captchaKey = accountervice.generateCaptchaKey();
%>
<div class="text-field">
<h2>注册新账户</h2>
<form name="signup" action="signup" method="post">
<label>账户ID:</label><input type="text" name="id"></input><br/>
<label>Email:</label><input type="text" name="email"></input><br/>
<label>显示名称:</label><input type="text" name="name"></input><br/>
<label>密码:</label><input type="password" name="password"></input><br/>
<label>确认密码:</label><input type="password" name="confirm_password"></input><br/>
<label>验证码:</label><input type="text" name="captcha_value"></input><br/>
<input type="hidden" name="captcha_key" value="<%=captchaKey%>"/>
<img src="<%=request.getContextPath()%>/captcha_image?key=<%=captchaKey%>"/>
</br>
<button>确认并提交</button>
</form>
</div>
</body>
</html>
4.在src/main/resources目录下新建service.properties文件,内容如下,此处要写正确邮箱,否则测试无法通过:
email.protocol=smtp email.host=smtp.163.com email.port=25 email.username=test@163.com email.password=password email.auth=true email.systemEmail=test@163.com persist.file=E:/mavenspace/account/target/test-classes/persist-data.xml
5.测试:
在account-web项目的POM内容中添加plugin:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <pluginManagement> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>7.1.0.RC1</version> <configuration> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/account</contextPath> </webAppConfig> </configuration> </plugin> </plugins> </pluginManagement> </build>
在.m2/中的setting.xml文件中的
<pluginGroups>中增加以下内容:
<pluginGroup>org.mortbay.jetty</pluginGroup>
就可以直接使用mvn jetty:run部署并启动Jetty。
或输入:
mvn jetty:run -Djetty.port=9999改变端口
6.输入:http://localhost:8080/account/signup.jsp进行注册,注册完后会发送邮件到你输入的邮箱,发送到邮箱的链接不正确,正确格式类似于:http://localhost:8080/account/activate?key=hhhjdfbm。
输入链接激活后,就可以输入http://localhost:8080/account/login.jsp登录了。
7.自动部署cargo-maven2-plugin
7.1本地部署
standalone模式:Cargo会从Web容器的安装目录复制一份配置到用户指定的目录,每次重新构建的时候,这个目录会被清空
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<home>D:\dev\tomcat\6035frame</home>
</container>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat6x</home>
</configuration>
</configuration>
</plugin>
这样生成的访问路径为:http://localhost:8080/account-web-1.0.0/,
要设置finalName,即:
<build>
<finalName>account</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>7.1.0.RC1</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppConfig>
<contextPath>/account</contextPath>
</webAppConfig>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.0</version>
<configuration>
<container>
<containerId>tomcat6x</containerId>
<home>D:\dev\tomcat\6035frame</home>
</container>
<configuration>
<type>standalone</type>
<home>${project.build.directory}/tomcat6x</home>
</configuration>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
访问路径为:http://localhost:8080/account/,
配置完成后,运行mvn clean install,再运行mvn cargo:start启动服务器.cargo会把Tomcat中的文件复制到account-web/target/tomcat6x下,再将项目部署到tomcat6x下的webapps中。
运行mvn cargo:stop停止服务器。
eisting模式:用户要指定现有Web容器配置目录,Cargo会直接使用这些配置并将应用部署到其对应的位置
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>D:\dev\tomcat\6035frame</home> </container> <configuration> <type>existing</type> <home>D:\dev\tomcat\6035frame</home> </configuration> </configuration> </plugin>
配置完成后,运行mvn clean install,再运行mvn cargo:start启动服务器.cargo会把项目部署到指定的Tomcat下的webapps中。
运行mvn cargo:stop停止服务器。
7.2远程部署(未测试)
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.remote.username>username</cargo.remote.username> <cargo.remote.password>password</cargo.remote.password> <cargo.tomcat.manager.url>http://localhost:8080/manager</cargo.tomcat.manager.url> </properties> </configuration> </configuration> </plugin>
运行命令:mvn cargo:redeploy部署
本文介绍如何使用Maven搭建一个包含注册、登录、验证码及邮件激活功能的账户服务项目,并详细说明了项目结构、配置文件内容及测试部署过程。
3741

被折叠的 条评论
为什么被折叠?



