您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
小编给大家分享一下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.Driver url=jdbc:mysql://localhost:3306/student username=root password=root maxActive=10 maxWait=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; /* JDBC工具类,使用Durid连接池 */ 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; } /** * 获取连接Connection对象 */ 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; /** * 操作数据库中User表的类 */ public class UserDao { //声明JDBCTemplate对象共用 private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource()); /** * 登录方法 * @param loginUser 只有用户和密码 * @return user 包含用户全部信息,没有查询到,返回null */ 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、获取请求参数 String username = request.getParameter("username"); String password = request.getParameter("password"); //3、封装对象 User loginUser = new User(); loginUser.setUsername(username); loginUser.setPassword(password);*/ //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); } }
1、目录结构
2、运行界面
3、输入用户名密码均正确
4、跳转到成功登录界面
5、输入用户名或密码错误时
6、跳转登录失败界面
看完了这篇文章,相信你对“JavaWeb Servlet怎么实现网页登录功能”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。