小编给大家分享一下JavaWeb Servlet怎么实现网页登录功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
具体内容如下:
1、配置文件,导入jar包
2、创建数据库环境
use student;create table user( id int primary key auto_increment, username varchar(32), password varchar(32));insert into user values(null,'zhangsan','123');insert into user values(null,'lisi','234');
3、在web目录下创建HTML页面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录页面</title></head><body> <form action="/day14_request/loginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="登录"> </form></body></html>
4、在src下编写druid.properties文件
driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/studentusername=rootpassword=rootmaxActive=10maxWait=1000
5、创建包cn.itcast.domain,创建类User
package cn.itcast.domain;public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } 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{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; }}
6、创建包cn.itcast.util,编写工具类JDBCUtils
package cn.itcast.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 JDBCUtils { private static DataSource dataSource; static { try { //1、加载配置文件 Properties properties = new Properties(); InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); properties.load(resourceAsStream); //2、初始化连接池对象 dataSource = DruidDataSourceFactory.createDataSource(properties); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource() { return dataSource; } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); }}
7、创建包cn.itcast.dao,创建类UserDao,提供login方法
package cn.itcast.dao;import cn.itcast.domain.User;import cn.itcast.util.JDBCUtils;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;public class UserDao { //声明JDBCTemplate对象共用 private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource()); public User login(User loginUser) { try { //1、编写sql String sql = "select * from user where username = ? and password = ?"; //2、调用query方法 User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), loginUser.getUsername(),loginUser.getPassword()); return user; } catch (DataAccessException e) { e.printStackTrace();//记录日志 return null; } }}
8、编写cn.itcast.web.servlet.LoginServlet类
package cn.itcast.web.servlet;import cn.itcast.dao.UserDao;import cn.itcast.domain.User;import org.apache.commons.beanutils.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("/loginServlet")public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1、设置编码 request.setCharacterEncoding("utf-8"); //2、获取所有请求参数(这里使用BeanUtils来简化操作) Map<String, String[]> parameterMap = request.getParameterMap(); //3.1 创建User对象 User loginUser = new User(); //3.2 使用BeanUtils对象 try { BeanUtils.populate(loginUser,parameterMap); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //4、调用UserDao的login方法 UserDao userDao = new UserDao(); User user = userDao.login(loginUser); //5、判断user if (user == null) { //登录失败 request.getRequestDispatcher("/failServlet").forward(request,response); } else { //登录成功 //存储数据 request.setAttribute("user",user); //转发 request.getRequestDispatcher("/successServlet").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); }}
运行结果:
目录结构
运行界面
输入用户名密码均正确
跳转到成功登录界面
输入用户名或密码错误时
跳转登录失败界面
看完了这篇文章,相信你对“JavaWeb Servlet怎么实现网页登录功能”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网行业资讯频道,感谢各位的阅读!