一个简单的Java MVC模式示例
2018-07-20 来源:open-open
package com.neuq.control; //视图层 import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.neuq.dao.UserDao; import com.neuq.service.UserService; import com.neuq.vo.UserVO; public class UserManage extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } //增加用户 public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { //获取客户端传进来的数据 String userName =(String)request.getParameter("userName"); String password = (String)request.getParameter("password"); String sex = (String)request.getParameter("sex"); String hobby = (String)request.getParameter("hobby"); String email = (String)request.getParameter("email"); String telephone = (String)request.getParameter("telephone"); String province = (String)request.getParameter("province"); UserVO user = new UserVO(); //建立一个实例,利用控制层 user.setUserName(userName); user.setPassword(password); user.setSex(sex); user.setHobby(hobby); user.setEmail(email); user.setTelephone(telephone); user.setProvince(province); UserService userService = new UserService(); int resultCount=userService.addUser(user); if(resultCount<=0){ request.setAttribute("info","添加失败!" ); }else{ request.setAttribute("info", "添加成功!"); } request.getRequestDispatcher("../info.jsp").forward(request, response);//请求转发 } //删除用户 public void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException { String userName = request.getParameter("userName"); UserService userService = new UserService(); boolean flag =userService.deleteUser(userName); if (flag) { request.setAttribute("info", "删除数据成功!"); } else { request.setAttribute("info", "删除数据失败!"); } try { request.getRequestDispatcher("../info.jsp").forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //查询用户 public void selectUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName =(String)request.getParameter("userName"); UserDao userDao = new UserDao(); List list = (List)userDao.getUsers(userName); request.setAttribute("result", list); request.getRequestDispatcher("../ShowMessage.jsp").forward(request, response); } //修改用户 public void updateUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException { String userName=(String)request.getParameter("userName"); String password = (String)request.getParameter("password"); String sex = (String)request.getParameter("sex"); String hobby = (String)request.getParameter("hobby"); String email = (String)request.getParameter("email"); String telephone = (String)request.getParameter("telephone"); String province = (String)request.getParameter("province"); UserVO user = new UserVO(); //建立一个实例,利用控制层 user.setUserName(userName); user.setPassword(password); user.setSex(sex); user.setHobby(hobby); user.setEmail(email); user.setTelephone(telephone); user.setProvince(province); UserService userService = new UserService(); int resultCount=userService.updateUser(user); if(resultCount<=0){ request.setAttribute("info", "修改数据失败!"); }else{ request.setAttribute("info", "修改数据成功!"); } try { request.getRequestDispatcher("../info.jsp").forward(request, response); } catch (ServletException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method=(String)request.getParameter("method"); try { if("add".equals(method)){ addUser(request,response); }else if("select".equals(method)){ selectUser(request,response); }else if("update".equals(method)){ updateUser(request,response); }else if("delete".equals(method)){ deleteUser(request,response); } } catch (SQLException e) { e.printStackTrace(); } } }
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:PHP读取远程文件
最新资讯
热门推荐