zoukankan      html  css  js  c++  java
  • 2020年11月26日Java学习日记

    searchServlet.java

    复制代码
    package 人口普查系统;
    
    import java.io.IOException;
    import java.sql.SQLException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class searchServlet
     */
    @WebServlet("/searchServlet")
    public class searchServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public searchServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //response.getWriter().append("Served at: ").append(request.getContextPath());
            request.setCharacterEncoding("utf-8");
             response.setCharacterEncoding("utf-8");
             String cxfs=request.getParameter("cxfs");
             System.out.print(cxfs);
    
             String str=request.getParameter("value");
             Dao dao=new Dao();
             List<Bean> list = null;
            
                 try {
                     if("1".equals(cxfs))
                     {
                    list=dao.searchByName(str);
                    } 
                     if("2".equals(cxfs))
                     {
                    list=dao.searchBySex(str);
                    } 
                     if("3".equals(cxfs))
                     {
                    list=dao.searchByEducation(str);
                    } 
                     if("4".equals(cxfs))
                     {
                    list=dao.searchByNation(str);
                    } 
                    
                     
                } catch (SQLException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
           request.setAttribute("list", list);
        request.getRequestDispatcher("list.jsp").forward(request,response);
             System.out.print(list.size());
        }    
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    复制代码

    servlet.java

    复制代码
    package 人口普查系统;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    
    /**
     * Servlet implementation class servlet
     */
    @WebServlet("/servlet")
    public class servlet extends HttpServlet {
        Dao dao=new Dao();
        private static final long serialVersionUID = 1L;
        /**
         * @see HttpServlet#HttpServlet()
         */
        public servlet() {
            super();
            // TODO Auto-generated constructor stub
        } 
    
          
        private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(request.getParameter("id"));
            String hubie = request.getParameter("hubie");
            String livetype = request.getParameter("livetype");
            int area= Integer.parseInt(request.getParameter("area"));
            int roomnum = Integer.parseInt(request.getParameter("roomnum"));
            String name = request.getParameter("name");
            String idcard = request.getParameter("idcard");
            String sex = request.getParameter("sex");
            String nation = request.getParameter("nation");
            String education= request.getParameter("education");
            Bean bean=new Bean(id,hubie,livetype,area,roomnum,name,idcard,sex,nation,education);
            dao.update(bean);
            request.setAttribute("message", "修改成功");
            request.getRequestDispatcher("servlet?method=list").forward(request, response);
        }
    
        private void list(HttpServletRequest request, HttpServletResponse response) throws Exception {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            List<Bean> list = dao.list();
            request.setAttribute("list", list);
            request.getRequestDispatcher("list.jsp").forward(request,response);
        }
    
        private void delete(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("UTF-8");
            int id=Integer.parseInt(request.getParameter("id"));
            dao.delete(id); //进行数据库的删除操作
            request.setAttribute("message", "删除成功");
            request.getRequestDispatcher("servlet?method=list").forward(request, response);
        }
    
       
        
        private void insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(request.getParameter("id"));
            String hubie = request.getParameter("hubie");
            String livetype = request.getParameter("livetype");
            int area= Integer.parseInt(request.getParameter("area"));
            int roomnum = Integer.parseInt(request.getParameter("roomnum"));
            String name = request.getParameter("name");
            String idcard =  request.getParameter("idcard");
            String sex = request.getParameter("sex");
            String nation = request.getParameter("nation");
            String education= request.getParameter("education");
            Bean bean=new Bean(id,hubie,livetype,area,roomnum, name, idcard, sex,nation, education);
     
            if(dao.insert(bean)) {
                request.setAttribute("message", "添加成功");
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        }
        
        
        
       
        private void search(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("UTF-8");
            String cxfs = request.getParameter("cxfs");    
            System.out.print("cxfs");
            request.setAttribute("search", "查询成功");
            request.getRequestDispatcher("list.jsp").forward(request, response);
        }
        
    
        
        
        
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            request.setCharacterEncoding("utf-8");
            String method=request.getParameter("method");
            if("insert".equals(method)) {
                insert(request,response);           
            }
            else if("delete".equals(method)) {
                try {
                    delete(request,response);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            else if("update".equals(method)) {
                update(request,response);
            }
            else if("list".equals(method)) {
                try {
                    list(request,response);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }}
           else if("search".equals(method)) {
                    try {
                        search(request,response);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
            
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
           
        }
    
    }
  • 相关阅读:
    郭大小
    最近翻译的三篇新闻
    又是一年教师节
    PowerDesigner 12.5 反向工程sql server
    Sql Server使用技巧
    一个纠结的silverlight问题
    PowerDesigner 15 使用技巧
    windows2008 + iis7 下载特殊后缀名文件设置方法
    无法读取配置节system.serviceModel因为它缺少节声明的解决方法
    PowerDesigner 15对ACCESS进行反向工程
  • 原文地址:https://www.cnblogs.com/9635741zly/p/14176482.html
Copyright © 2011-2022 走看看