package com.demo.struts.actions;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.demo.struts.forms.RegisterForm;
import com.demo.struts.util.Constants;
public class RegisterAction extends DispatchAction {
Logger log = Logger.getLogger(this.getClass());
public ActionForward init(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionForward forward = mapping.findForward(Constants.FAILURE_KEY);
return (forward);
}
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
RegisterForm registerForm = (RegisterForm) form;
try {
// if exist
boolean isExist = isExist(request, registerForm);
if (isExist) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"register.message.failed"));
} else {
insert(request, registerForm);
log.info("User " + registerForm.getUsername() + " register.");
}
} catch (Exception e) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"register.message.failed"));
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
request.setAttribute("registerFormBean", registerForm);
forward = mapping.findForward(Constants.FAILURE_KEY);
} else {
forward = mapping.findForward(Constants.SUCCESS_KEY);
}
// Finish with
return (forward);
}
private boolean isExist(HttpServletRequest request,
RegisterForm registerForm) {
DataSource ds = null;
Connection cn = null;
boolean b = false;
try {
ds = getDataSource(request);
cn = ds.getConnection();
Statement st = cn.createStatement();
ResultSet rs = st
.executeQuery("select * from user where username='"
+ registerForm.getUsername() + "'");
if (rs.next()) {
b = true;
}
rs.close();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
private void insert(HttpServletRequest request, RegisterForm registerForm) {
DataSource ds = null;
Connection cn = null;
try {
ds = getDataSource(request);
cn = ds.getConnection();
Statement st = cn.createStatement();
st.executeUpdate("insert into user(username,password,email) values('"
+ registerForm.getUsername() + "', '"
+ registerForm.getPassword1() + "', '"
+ registerForm.getEmail() + "')");
cn.commit();
st.close();
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
C0deRush
- 粉丝: 0
- 资源: 17
最新资源
- 宿舍维修管理系统的设计与实现-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 失踪人员信息发布与管理系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 玩具租赁系统boot--论文pf-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 图书大厦图书管理系统的设计与实现--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- Docker容器引擎的技术详解及应用场景区分
- 学生宿舍管理系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 网吧管理系统--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 学生信息管理系统_06bg9--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 一款基于BS的美食网站的设计与实现--论文-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 武汉君耐营销策划有限公司员工信息管理系统pf-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 疫苗接种管理系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 纹理生成图片系统--论文pf-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 微乐校园pf-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- citespace6.3.1
- 疫情打卡健康评测系统-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
- 物流信息管理系统_ho5g5-springboot毕业项目,适合计算机毕-设、实训项目、大作业学习.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈