jsp第六次
数据库
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 Test2{ 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; } }
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); } }