admin管理员组文章数量:1442251
JavaWeb后端入门案例一—简易登录案例
1. 需求分析
登录界面为输入用户名、密码、登录按钮
登陆失败回到登录页面并显示“用户名或密码错误”
登陆成功重定向到Success页面,并显示登录成功的人数
2. 步骤
2.1 创建数据库
代码语言:javascript代码运行次数:0运行复制create database web02_login;
use web02_login;
create table user(
uid int primary key auto_increment,
username varchar(20),
password varchar(20),
nickname varchar(20)
);
insert into user values (null,'zs','123','张三');
insert into user values (null,'ls','123','李四');
insert into user values (null,'ww','123','王五');
2.2 项目环境搭建
2.3 创建登录界面 login.jsp
代码语言:javascript代码运行次数:0运行复制<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>登录页面</h1>
<%
/**
判断request域中是否有错误信息:第一次进入时,没有错误信息
如果有错误信息:显示错误信息
*/
String msg = "";
if(request.getAttribute("msg")!=null){
//有错误信息:显示错误信息
msg = (String)request.getAttribute("msg");
}
%>
<h3> <font color="red"><%=msg %></font> </h3>
<form action="/web02_login/LoginServlet" method="post">
<table border="1" width="600">
<tr>
<td>用户名</td>
<td> <input type="text" name="username"> </td>
</tr>
<tr>
<td>密码</td>
<td> <input type="password" name="password"> </td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="登录"> </td>
</tr>
</table>
</form>
</body>
</html>
2.4 创建User类用于封装数据
代码语言:javascript代码运行次数:0运行复制package domain;
/**
* 用于封装数据的JavaBean
* */
public class User {
private Integer uid;
private String username;
private String password;
private String nickname;
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
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;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
}
2.5 创建UserModel类用于连接数据库并对比登录信息
代码语言:javascript代码运行次数:0运行复制package model;
import java.sql.SQLException;
import org.apachemons.dbutils.QueryRunner;
import org.apachemons.dbutils.handlers.BeanHandler;
import domain.User;
import utils.JDBCUtils;
public class UserModel {
public User login(User user) throws SQLException{
//连接数据库:通过传入的用户名和密码去数据库进行查询
QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
User existUser = queryRunner.query("select * from user where username = ? and password = ?",
new BeanHandler<User>(User.class), user.getUsername(), user.getPassword());
return existUser;
}
}
2.6 用于记录人数的InitServlet控制层代码
代码语言:javascript代码运行次数:0运行复制package controller;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitServlet extends HttpServlet {
public void init() throws ServletException {
//初始一个值为0
int count = 0;
//将这个值存入到ServletContext域中
this.getServletContext().setAttribute("count", count);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
并使此Servlet在服务器运行开始启动
代码语言:javascript代码运行次数:0运行复制<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>controller.InitServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
2.7 登录的LoginServlet控制层代码
代码语言:javascript代码运行次数:0运行复制package controller;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.User;
import model.UserModel;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.接受数据
//处理中文乱码
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
//2.封装数据
User user = new User();
user.setUsername(username);
user.setPassword(password);
//3.处理数据
UserModel userModel = new UserModel();
User existUser = null;
try {
existUser = userModel.login(user);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//4.页面跳转
if (existUser == null) {
//登陆失败
//想resquest域中保存一个错误信息
request.setAttribute("msg", "用户名或密码错误!");
//使用请求转发进行页面跳转
request.getRequestDispatcher("/login.jsp").forward(request, response);
}else {
//登陆成功
//将ServletContext中的值取出
int count = (int) this.getServletContext().getAttribute("count");
//+1
count++;
//存回到ServletContext中
this.getServletContext().setAttribute("count", count);
//重定向到成功页面
response.sendRedirect("/web02_login/success.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.8 登陆成功的界面
代码语言:javascript代码运行次数:0运行复制<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>恭喜!登陆成功</h1>
<%
Integer count = 0;
//如果ServletContext中有值,获取并显示
if(this.getServletContext().getAttribute("count")!=null){
count = (Integer)this.getServletContext().getAttribute("count");
}
%>
<h2>登陆成功的总人数:<%=count %></h2>
</body>
</html>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2021-04-19 ,如有侵权请联系 cloudcommunity@tencent 删除数据库import登录后端入门本文标签: JavaWeb后端入门案例一简易登录案例
版权声明:本文标题:JavaWeb后端入门案例一—简易登录案例 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1748000695a2789298.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论