jar包:
登录实例:
前端:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <form action="/servlet" method="post"> <input type="text" name="username" /> <input type="password" name="password"/> <input type="submit" value="登录" /> </form> </body> </html>
后端:
package cn.rongxinrou.web.servlet; import org.apache.commons.beanutils2.BeanUtils; 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 java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet("/servlet") public class DemoServlet extends HttpServlet{ @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Map<String, String[]> map = req.getParameterMap(); User user = new User(); // 利用BeanUtils 封装对象 try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserDao userDao = new UserDao(); User login = userDao.login(user); if (login == null){ req.getRequestDispatcher("/failServerlet").forward(req,resp); }else { req.setAttribute("user",user); req.getRequestDispatcher("/successServerlet").forward(req,resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
package cn.rongxinrou.web.servlet; 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 java.io.IOException; @WebServlet("/successServerlet") public class ServerletSuccess extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); User user = (User) req.getAttribute("user"); resp.getWriter().write("登录成功:" + user.getUsername()); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
package cn.rongxinrou.web.servlet; 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 java.io.IOException; @WebServlet("/failServerlet") public class failServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write("登录失败"); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
具体功能:
package cn.rongxinrou.web.servlet; import cn.danielw.spring.jdbc.JdbcTemplate; import org.springframework.jdbc.core.BeanPropertyRowMapper; public class UserDao { final JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource()); public User login(User loginUser){ String sql = "select * from users where username = ? and password = ?"; try{ User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), loginUser.getUsername(), loginUser.getPassword()); return user; }catch (Exception e){ System.out.println(e); return null; } } }
package cn.rongxinrou.web.servlet; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; public class JDBCUtils { // 定义连接池成员变量 private static DataSource ds; static { Properties pro = new Properties(); try { // 加载配置文件 pro.load(JDBCUtils.class.getResourceAsStream("/cn/druid.properties")); // 创建 Druid连接池对象 ds = DruidDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } } // 返回连接池对象 public static DataSource getDataSource(){ return ds; } // 返回connection对象 public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
package cn.rongxinrou.web.servlet; public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", password=" + password + '}'; } }
配置文件
driverClassname=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1/test username=root password=root initialSize=5 maxActive=10 maxWait=3000