码迷,mamicode.com
首页 > Web开发 > 详细

第六周jsp

时间:2021-04-14 12:19:31      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:map   uid   username   验证   red   encoding   gets   EDA   contex   

1.数据库连接

 1 package util;
 2 
 3 import com.alibaba.druid.pool.DruidDataSourceFactory;
 4 
 5 import javax.sql.DataSource;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.sql.Connection;
 9 import java.sql.SQLException;
10 import java.util.Properties;
11 
12 public class JDBCUtil {
13     private static DataSource ds;
14     static {
15         Properties pro=new Properties();
16         InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
17         try {
18             pro.load(is);
19            ds = DruidDataSourceFactory.createDataSource(pro);
20         } catch (IOException e) {
21             e.printStackTrace();
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25 
26     }
27     public static DataSource getDataSource(){return ds;}
28     public static Connection getConnection() throws SQLException {
29         Connection connection=ds.getConnection();
30         return connection;
31     }
32 }

 

 

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);
    }
}

 

 

 

4.若不存在,跳到登录页面

 

第六周jsp

标签:map   uid   username   验证   red   encoding   gets   EDA   contex   

原文地址:https://www.cnblogs.com/zrz1/p/14655579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!