数据库中user的信息:

前台开发——处理用户收货信息的修改-冯金伟博客园

处理用户收货信息的修改的效果图:

前台开发——处理用户收货信息的修改-冯金伟博客园

数据库中经修改的收货信息:

前台开发——处理用户收货信息的修改-冯金伟博客园

修改user_center.jsp,里面应该使用两个表单:

 1 <div class="register-top-grid">
 2                         <h3>个人中心</h3>
 3                         
 4                         <form action="${pageContext.request.contextPath }/user_changeaddress" method="post"> 
 5                         <!-- 收货信息start -->
 6                         <h4>收货信息</h4>
 7                         <div class="input">
 8                             <span>收货人<label></label></span>
 9                             <input type="text" name="name" value="${user.name }" placeholder="请输入收货"> 
10                         </div>
11                         <div class="input">
12                             <span>收货电话</span>
13                             <input type="text" name="phone" value="${user.phone }" placeholder="请输入收货电话"> 
14                         </div>
15                         <div class="input">
16                             <span>收货地址</span>
17                             <input type="text" name="address" value="${user.address }" placeholder="请输入收货地址"> 
18                         </div>
19                         <div class="register-but text-center">
20                            <input type="submit" value="提交">
21                         </div>
22                         
23                         <!-- 收货信息end -->    
24                         
25                         
26                         
27                     </form>
28                         
29                         
30                         <hr>
31                         <form action="">
32                         <!-- 安全信息start -->
33                         <h4>安全信息</h4>
34                         <div class="input">
35                             <span>原密码</span>
36                             <input type="password" name="user.password" placeholder="请输入原密码"> 
37                         </div>
38                         <div class="input">
39                             <span>新密码</span>
40                             <input type="password" name="user.passwordNew" placeholder="请输入新密码"> 
41                         </div>
42                         <div class="clearfix"> </div>
43                         <div class="register-but text-center">
44                            <input type="submit" value="提交">
45                         </div>
46                         <!-- 安全信息end -->
47                         </form>    
48                     </div>

在UserDao.java中创建一个函数updateUserAddress

public void updateUserAddress(User user) throws SQLException {
    QueryRunner r = new QueryRunner(DBUtil.getDataSource());
    String sql="update user set name=?,phone=?,address=?where id=?";
    r.update(sql,user.getName(),user.getPhone(),user.getAddress(),user.getId());
}
        

UserService.java中进行对该函数的异常处理:

public void updateUserAddress(User user) {
        
        try {
            uDao.updateUserAddress(user);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }

创建一个servlet类,命名为: UserChangeAddressServlet.java,获取3个数据,它们分别是 name、phone、address,并当修改收货信息成功时,有提示修改成功的信息:

package com.guiyan.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.guiyan.model.User;
import com.guiyan.service.UserService;

/**
 * Servlet implementation class UserChangeAddressServlet
 */
@WebServlet("/user_changeaddress")
public class UserChangeAddressServlet extends HttpServlet {
    private UserService uService=new UserService();
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User loginUser=(User) request.getSession().getAttribute("user");

        
        try {
            BeanUtils.copyProperties(loginUser, request.getParameterMap());
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        uService.updateUserAddress(loginUser);//收件地址的更新
        request.setAttribute("msg", "收件信息更新成功!!!");
        request.getRequestDispatcher("/user_center.jsp").forward(request, response);
        
        
        
    }

}