JavaWeb⽤户信息管理系统-添加⽤户的实现1 添加⽤户持久层
1.1 创建添加⽤户的持久层接⼝
public interface UserManagerDao {
void insertUser(Users users);
}
1.2 持久层⽤户添加的接⼝实现类
/**
* ⽤户管理持久层
*/
public class UserManagerDaoImpl implements UserManagerDao {
@Override
public void insertUser(Users users) {
Connection conn = null;
try{
conn = Connection();
conn.setAutoCommit(false);
PreparedStatement ps = conn.prepareStatement("insert into users values(default,?,?,?,?,?)");
ps.setString(Username());
ps.setString(Userpwd());
ps.setString(Usersex());
ps.setString(Phonenumber());
ps.setString(Qqnumber());
connmit();
}catch(Exception e){
e.printStackTrace();
}finally{
JdbcUtils.closeConnection(conn);
}
}
2 添加⽤户业务层
2.1 创建添加⽤户的业务层接⼝
public interface UserManagerService {
void addUser(Users users);
}
2.2 创建添加⽤户的业务层实现类
/**
* ⽤户管理业务层
*/
public class UserManagerServiceImpl implements UserManagerService {
/**
* 添加⽤户
* @param users
*/
@Override
public void addUser(Users users) {
UserManagerDao userManagerDao = new UserManagerDaoImpl();
userManagerDao.insertUser(users);
}
}
3 添加⽤户的 Servlet
/**
* 处理⽤管理请求
*/
@WebServlet("/userManager.do")
public class UserManagerServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        String flag = Parameter("flag");
if("addUser".equals(flag)){
this.addUser(req,resp);
}
}
//处理添加⽤户请求
private void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        Users users = ateUsers(req);
try{
UserManagerService userManagerService = new UserManagerServiceImpl();
userManagerService.addUser(users);
resp.sendRedirect("ok.jsp");
}catch(Exception e){
e.printStackTrace();
htmlradio的text出不来resp.sendRedirect("error.jsp");
}
}
//获取⽤户提交数据
private Users createUsers(HttpServletRequest req){
String username = Parameter("username");
String userpwd = Parameter("userpwd");
String usersex = Parameter("usersex");
String phonenumber = Parameter("phonenumber");
String qqnumber = Parameter("qqnumber");
Users users = new Users();
users.setQqnumber(qqnumber);
users.setPhonenumber(phonenumber);
users.setUsername(username);
users.setUserpwd(userpwd);
users.setUsersex(usersex);
return users;
}
}
4 前台添加页⾯
将⼀个模块放⼊独⽴的⽂件,要不全是jsp⽂件过于臃肿。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>⽆标题⽂档</title>
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<link href="../css/select.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="#">⽤户管理</a></li>
<li><a href="#">添加⽤户</a></li>
</ul>
</div>
<div class="formbody">
<div class="formtitle"><span>基本信息</span></div>
<form action="../userManager.do" method="post">
<input type="hidden"  name="flag" value="addUser"/>
<ul class="forminfo">
<li>
<label>⽤户名</label>
<input name="username" type="text" class="dfinput" /></li>
<li>
<li>
<label>⽤户密码</label>
<input name="userpwd" type="text" class="dfinput" /><i></i></li>
<li>
<label>性别</label><cite>
<input name="usersex" type="radio" value="1" checked="checked" />男                <input name="usersex" type="radio" value="0" />⼥
</li>
<li>
<label>联系⽅式</label>
<input name="phonenumber" type="text" class="dfinput" />
</li>
<li>
<label>QQ号</label>
<input name="qqnumber" type="text" class="dfinput" />
</li>
<li>
<label> </label>
<input  type="submit"  class="btn" value="确认保存" />
</li>
</ul>
</form>
</div>
</body>
</html>
4.1 反馈成功的jsp页⾯
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function submitForm(){
window.close();
}
</script>
</head>
<body>
<div class="place">
<span>位置:</span>
<ul class="placeul">
<li><a href="#">操作提⽰</a></li>
</ul>
</div>
操作成功!
</body>
</html>
5 测试结果

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。