• 第六周jsp作业


    1.数据库链接

    package util;
    
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    
    import javax.sql.DataSource;
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.Properties;
    
    public class JDBCUtil {
        private static DataSource ds;
        static {
            Properties pro=new Properties();
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
            try {
                pro.load(is);
               ds = DruidDataSourceFactory.createDataSource(pro);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
        public static DataSource getDataSource(){return ds;}
        public static Connection getConnection() throws SQLException {
            Connection connection=ds.getConnection();
            return connection;
        }
    }

    2.数据库登录操作

    public Doctor Login(Doctor loginDoctor) {
            try {
                String sql="select * from doctors where username = ? and password = ?";
                Doctor doctor = temp.queryForObject(sql, new BeanPropertyRowMapper<Doctor>(Doctor.class), loginDoctor.getUsername(), loginDoctor.getPassword());
                return doctor;
            } catch (DataAccessException e) {
               return null;
            }
        }

    3.servlet(密码或用户名错误返回页面并提示)

    package web.Servlet;
    
    import domain.Doctor;
    import org.apache.commons.beanutils.BeanUtils;
    import service.DoctorService;
    import service.impl.DoctorServiceImpl;
    
    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 javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.lang.reflect.InvocationTargetException;
    import java.util.Map;
    
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            String checkCode = request.getParameter("checkCode");
            HttpSession session = request.getSession();
            String checkcode = (String) session.getAttribute("CHECKCODE_SERVER");
            if (!checkcode.equalsIgnoreCase(checkCode)){
                request.setAttribute("error_msg","验证码错误");
                request.getRequestDispatcher("/index.jsp").forward(request,response);
                return;
            }
            Map<String, String[]> map = request.getParameterMap();
            Doctor loginDoctor =new Doctor();
            try {
                BeanUtils.populate(loginDoctor,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            DoctorService service=new DoctorServiceImpl();
            Doctor doctor=service.Login(loginDoctor);
            if (doctor!=null){
                session.setAttribute("user",doctor);
                response.sendRedirect(request.getContextPath()+"/Home.jsp");
            }else {
                request.setAttribute("error_msg","用户名或密码错误");
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    }

     

  • 相关阅读:
    用c写一个小的聊天室程序
    socket相关的开机初始化分析
    HTML——CSS3学习
    iOS--OCR图片识别
    iOS学习——Socket
    iOS学习——数据加密
    iOS学习——并发编程GCD
    iOS学习——weak的应用场景
    iOS学习——RUNLOOP、NSTimer
    iOS学习——锁
  • 原文地址:https://www.cnblogs.com/GEM520/p/14645590.html
Copyright © 2020-2023  润新知