下边只在注册中实现
1、首先是在Action中写出生成验证码所需的方法(下边是在MemberAction.java中写)
- package www.csdn.dbshop.action;
- import org.apache.struts2.ServletActionContext;
- import www.csdn.dbshop.domain.Admin;
- import www.csdn.dbshop.domain.Member;
- import www.csdn.dbshop.service.BaseService;
- import www.csdn.dbshop.service.MemberService;
- import www.csdn.dbshop.service.impl.BaseServiceImpl;
- import www.csdn.dbshop.service.impl.MemberServiceImpl;
- import www.csdn.dbshop.util.Pagination;
- import www.csdn.dbshop.util.StringUtil;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- public class MemberAction extends BaseAction{
- private BaseService baseService = new BaseServiceImpl();
- private MemberService memberService = new MemberServiceImpl();
- private String cuemail;
- private boolean cueflag;
- private Member member;
- private String acodet;
- private boolean cacodet;
- private String sRand = "";
- private String acode;
- public void setAcode(String acode) {
- this.acode = acode;
- }
- public boolean isCacodet() {
- return cacodet;
- }
- public void setAcodet(String acodet) {
- this.acodet = acodet;
- }
- public void setMember(Member member) {
- this.member = member;
- }
- public Member getMember() {
- return member;
- }
- public boolean isCueflag() {
- return cueflag;
- }
- public void setCacodet(boolean cacodet) {
- this.cacodet = cacodet;
- }
- public void setCuemail(String cuemail) {
- this.cuemail = cuemail;
- }
- public String checkuemail() {
- cueflag = baseService.checkProperty(Member.class, "email", cuemail,
- null);
- return "cuemail";
- }
- public void setCueflag(boolean cueflag) {
- this.cueflag = cueflag;
- }
- public String register() {
- if (((String) ServletActionContext.getRequest().getSession()
- .getAttribute("checkcode")).equalsIgnoreCase(acode)) {
- Member user = new Member();
- user.setEmail(member.getEmail());
- user.setPass(member.getPass());
- boolean sflag = baseService.saveObject(user);
- if (sflag) {
- return "register";
- } else {
- return "registererror";
- }
- } else {
- ServletActionContext.getRequest().setAttribute("register",
- "acerror");
- return "registererror";
- }
- }
- public String login() {
- if (((String) ServletActionContext.getRequest().getSession()
- .getAttribute("checkcode")).equalsIgnoreCase(acode)) {
- Member user = memberService.login(member.getEmail(),
- member.getPass());
- if (user == null) {
- ServletActionContext.getRequest()
- .setAttribute("login", "error");
- return "loginerror";
- } else {
- ServletActionContext.getRequest().getSession()
- .setAttribute("user", user);
- return "login";
- }
- } else {
- ServletActionContext.getRequest().getSession()
- .setAttribute("login", "acerror");
- return "loginerror";
- }
- }
- public String checkAcodet() {
- if (((String) ServletActionContext.getRequest().getSession()
- .getAttribute("checkcode")).equalsIgnoreCase(acodet)) {
- cacodet = true;
- } else {
- cacodet = false;
- }
- return "checkacode";
- }
- /**
- * ****************** * Action生成验证码* ******************
- */
- private Font imgFont = new Font("Times New Roman", Font.BOLD, 17);
- Color getRandColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255) {
- fc = 255;
- }
- if (bc > 255) {
- bc = 255;
- }
- int r = fc + random.nextInt(bc - fc);
- int g = fc + random.nextInt(bc - fc);
- int b = fc + random.nextInt(bc - fc);
- return new Color(r, g, b);
- }
- public void checkCodeImage() {
- HttpServletRequest request = ServletActionContext.getRequest();
- HttpServletResponse response = ServletActionContext.getResponse();
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- response.setContentType("image/jpeg");
- int width = 75, height = 20;
- BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = image.getGraphics();
- Random random = new Random();
- g.setColor(getRandColor(200, 250));
- g.fillRect(1, 1, width - 1, height - 1);
- g.setColor(new Color(102, 102, 102));
- g.drawRect(0, 0, width - 1, height - 1);
- g.setFont(imgFont);
- g.setColor(getRandColor(160, 200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int xl = random.nextInt(6) + 1;
- int yl = random.nextInt(12) + 1;
- g.drawLine(x, y, x + xl, y + yl);
- }
- for (int i = 0; i < 70; i++) {
- int x = random.nextInt(width - 1);
- int y = random.nextInt(height - 1);
- int xl = random.nextInt(12) + 1;
- int yl = random.nextInt(6) + 1;
- g.drawLine(x, y, x - xl, y - yl);
- }
- for (int i = 0; i < 4; i++) {
- int itmp = 0;
- if (random.nextInt(2) == 1) {
- itmp = random.nextInt(26) + 65;
- char ctmp = (char) itmp;
- sRand += String.valueOf(ctmp);
- g.setColor(new Color(20 + random.nextInt(110), 20 + random
- .nextInt(110), 20 + random.nextInt(110)));
- g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
- } else if (random.nextInt(3) == 1) {
- itmp = random.nextInt(26) + 97;
- char ctmp = (char) itmp;
- sRand += String.valueOf(ctmp);
- g.setColor(new Color(20 + random.nextInt(110), 20 + random
- .nextInt(110), 20 + random.nextInt(110)));
- g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
- } else {
- itmp = random.nextInt(9);
- sRand += String.valueOf(itmp);
- g.setColor(new Color(20 + random.nextInt(110), 20 + random
- .nextInt(110), 20 + random.nextInt(110)));
- g.drawString(String.valueOf(itmp), 15 * i + 10, 16);
- }
- }
- HttpSession session = request.getSession(true);
- session.setAttribute("checkcode", sRand);
- g.dispose();
- try {
- ImageIO.write(image, "JPEG", response.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
package www.csdn.dbshop.action;
import org.apache.struts2.ServletActionContext;
import www.csdn.dbshop.domain.Admin;
import www.csdn.dbshop.domain.Member;
import www.csdn.dbshop.service.BaseService;
import www.csdn.dbshop.service.MemberService;
import www.csdn.dbshop.service.impl.BaseServiceImpl;
import www.csdn.dbshop.service.impl.MemberServiceImpl;
import www.csdn.dbshop.util.Pagination;
import www.csdn.dbshop.util.StringUtil;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MemberAction extends BaseAction{
private BaseService baseService = new BaseServiceImpl();
private MemberService memberService = new MemberServiceImpl();
private String cuemail;
private boolean cueflag;
private Member member;
private String acodet;
private boolean cacodet;
private String sRand = "";
private String acode;
public void setAcode(String acode) {
this.acode = acode;
}
public boolean isCacodet() {
return cacodet;
}
public void setAcodet(String acodet) {
this.acodet = acodet;
}
public void setMember(Member member) {
this.member = member;
}
public Member getMember() {
return member;
}
public boolean isCueflag() {
return cueflag;
}
public void setCacodet(boolean cacodet) {
this.cacodet = cacodet;
}
public void setCuemail(String cuemail) {
this.cuemail = cuemail;
}
public String checkuemail() {
cueflag = baseService.checkProperty(Member.class, "email", cuemail,
null);
return "cuemail";
}
public void setCueflag(boolean cueflag) {
this.cueflag = cueflag;
}
public String register() {
if (((String) ServletActionContext.getRequest().getSession()
.getAttribute("checkcode")).equalsIgnoreCase(acode)) {
Member user = new Member();
user.setEmail(member.getEmail());
user.setPass(member.getPass());
boolean sflag = baseService.saveObject(user);
if (sflag) {
return "register";
} else {
return "registererror";
}
} else {
ServletActionContext.getRequest().setAttribute("register",
"acerror");
return "registererror";
}
}
public String login() {
if (((String) ServletActionContext.getRequest().getSession()
.getAttribute("checkcode")).equalsIgnoreCase(acode)) {
Member user = memberService.login(member.getEmail(),
member.getPass());
if (user == null) {
ServletActionContext.getRequest()
.setAttribute("login", "error");
return "loginerror";
} else {
ServletActionContext.getRequest().getSession()
.setAttribute("user", user);
return "login";
}
} else {
ServletActionContext.getRequest().getSession()
.setAttribute("login", "acerror");
return "loginerror";
}
}
public String checkAcodet() {
if (((String) ServletActionContext.getRequest().getSession()
.getAttribute("checkcode")).equalsIgnoreCase(acodet)) {
cacodet = true;
} else {
cacodet = false;
}
return "checkacode";
}
/**
* ****************** * Action生成验证码* ******************
*/
private Font imgFont = new Font("Times New Roman", Font.BOLD, 17);
Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void checkCodeImage() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
int width = 75, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200, 250));
g.fillRect(1, 1, width - 1, height - 1);
g.setColor(new Color(102, 102, 102));
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(imgFont);
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x, y, x + xl, y + yl);
}
for (int i = 0; i < 70; i++) {
int x = random.nextInt(width - 1);
int y = random.nextInt(height - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x, y, x - xl, y - yl);
}
for (int i = 0; i < 4; i++) {
int itmp = 0;
if (random.nextInt(2) == 1) {
itmp = random.nextInt(26) + 65;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
} else if (random.nextInt(3) == 1) {
itmp = random.nextInt(26) + 97;
char ctmp = (char) itmp;
sRand += String.valueOf(ctmp);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(ctmp), 15 * i + 10, 16);
} else {
itmp = random.nextInt(9);
sRand += String.valueOf(itmp);
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(String.valueOf(itmp), 15 * i + 10, 16);
}
}
HttpSession session = request.getSession(true);
session.setAttribute("checkcode", sRand);
g.dispose();
try {
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.在struts.xml中
- <result name="checkacode" type="json">
- <param name="root">cacodet</param>
- </result>
- <result name="register">/login.jsp</result>
- <result name="registererror">/personreg.jsp</result>
- <result name="login">/index.jsp</result>
- <result name="loginerror">/login.jsp</result>
<result name="checkacode" type="json">
<param name="root">cacodet</param>
</result>
<result name="register">/login.jsp</result>
<result name="registererror">/personreg.jsp</result>
<result name="login">/index.jsp</result>
<result name="loginerror">/login.jsp</result>
3.在register.jsp中
- <script type="text/javascript">
- function reImg(){
- var img = document.getElementById("picture");
- img.src=img.src + "?";
- }
- </script>
- <TR>
- <TD class=mustcontent><SPAN>*</SPAN>验证码:</TD>
- <td><input type="text" id="acodet" name="acode" style="width:110px" value="${randomCode}"><span id="acode"><a href="javascript:void(0);" onclick="reImg();" class="changeacode"><img id="picture" src="${pageContext.request.contextPath }/csdn/MemberAction_checkCodeImage.action?nowtime=" style="position:relative;top:5px;border:0;"></a> </span><span
- style="font-size:12px;color:rgb(150,150,150);margin-left:5px">看不清?<a class="changeacode" href="javascript:void(0);" onclick="reImg();">换一张</a></span>
- </td>
- </TR>
<script type="text/javascript">
function reImg(){
var img = document.getElementById("picture");
img.src=img.src + "?";
}
</script>
<TR>
<TD class=mustcontent><SPAN>*</SPAN>验证码:</TD>
<td><input type="text" id="acodet" name="acode" style="width:110px" value="${randomCode}"><span id="acode"><a href="javascript:void(0);" οnclick="reImg();" class="changeacode"><img id="picture" src="${pageContext.request.contextPath }/csdn/MemberAction_checkCodeImage.action?nowtime=" style="position:relative;top:5px;border:0;"></a> </span><span
style="font-size:12px;color:rgb(150,150,150);margin-left:5px">看不清?<a class="changeacode" href="javascript:void(0);" οnclick="reImg();">换一张</a></span>
</td>
</TR>
4.效果如下:
希望能给大家带来帮助