Javaweb综合练习一(简单的增删改查)

文章目录

    • 添加联系人
      • 相关代码
    • 删除单个的联系人
      • 代码实现
    • 删除选中
      • 代码的实现
    • 修改联系人
      • 相关代码
    • 分页查询


添加联系人

下面的图是添加联系人的流程思路:
首先是要设置编码,因为有中文的输入
然后是获取所有的数据
然后是封装对象(对象我并没有写出来,对象就是自己创建的user实体类)
然后是调用service的add方法完成保存到数据库里面去
再跳转(因为添加后就会回到主的查询所有的用户的界面)

相关代码

首先是

package web.Servlet;

import domain.user;
import org.apache.commons.beanutils.BeanUtils;
import service.impl.userServiceimpl;

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 java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/adduserServlet")
public class adduserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap();
        user user = new user();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userServiceimpl serviceimpl = new userServiceimpl();
        serviceimpl.add(user);
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

  public void add(user user) {
        //1.定义sql
        String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
        //2.执行sql
        jdbcTemplate.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
    }

删除单个的联系人

下面的图是删除单个联系人的流程思路:
首先获取参数(获取的是需要删除的联系人的Id)
然后是调用service的删除的方法(这里需要注意的是,当删除的时候应该给用户一个提示是否删除联系人?这样才能给用户一个更好的体验,避免误删的发生)
最后再跳转到主界面

代码实现

package web.Servlet;

import service.impl.userServiceimpl;

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 java.io.IOException;

@WebServlet("/userdeleteServlet")
public class userdeleteServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        userServiceimpl userServiceimpl = new userServiceimpl();
        userServiceimpl.delete(id);
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

删除选中

下面的图是删除选中的联系人的流程思路:
首先获取得到一个id的数组,因为删除的选中的联系人,所以得到的是一个数组
这里需要注意的是也要给用户一个提示是否删除让用户有更好的体验
然后调用service的删除的方法
然后再跳转到主页面

代码的实现

package web.Servlet;

import service.impl.userServiceimpl;

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 java.io.IOException;

@WebServlet("/delselectuser")
public class delselectuser extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("uid");
        userServiceimpl userServiceimpl = new userServiceimpl();
        userServiceimpl.deleteselectusers(ids);
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

修改联系人

下图是修改联系人的信息的流程思路:
首先得设置编码,因为有中文得输入
这里需要注意得是需要弄一个影藏域得id
还要弄一个回显得信息,让用户有更好得体验
然后是获取单表得数据用map
封装user得对象
调用service得修改得方法
再跳转到主界面

相关代码

package web.Servlet;

import domain.user;
import org.apache.commons.beanutils.BeanUtils;
import service.impl.userServiceimpl;

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 java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/updatauserServlet")
public class updatauserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        user user = new user();
        Map<String, String[]> map = request.getParameterMap();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        userServiceimpl userServiceimpl = new userServiceimpl();
        userServiceimpl.updatauser(user);
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

分页查询

做分页查询得时候比较得复杂,也是跟以上比起来最难的地方思路图上已经很清晰了


你可能感兴趣的:(java,apache,服务器)