手把手教你做JavaWeb项目:登录模块

现如今,无论是客户端还是移动端,无论是游戏登陆还是社交平台登陆,无处不在的“登陆”。那么你知道怎么制作吗?今天就为你娓娓道来:

在各大信息管理系统中,登录功能是必不可少的,他的作用就是验证用户的身份,判断用户是否是本站的会员,只有会员才能访问当前系统

实现:

1.用户填写账号和密码,提交到后台
2.后台获取到账号和密码后,将其发送到数据库中进行查询
3.查询结果如果为null,说明用户填写的账号或者密码有误,应该回到登录页面并提示用户重新输入
4.查询结果如果不为null,说明用户填写的账号和密码正确,将对应的账户信息共享到session中(在后面的请求,我们还需要继续使用当前登录的用户信息),然后跳转到网站的主页面

按照上面的步骤,我们使用下面的代码完成了相应功能
1.登录页面

<form class="form-horizontal" action="/login" method="post">

    <div class="form-group">

        <label for="inputEmail3" class="col-sm-3 control-label">用户名</label>

        <div class="col-sm-9">

            <input type="text" name="name" class="form-control" id="inputEmail3">

        </div>

    </div>

    <div class="form-group">

        <label for="inputPassword3" class="col-sm-3 control-label">密   码</label>

        <div class="col-sm-9">

            <input type="password" name="password" class="form-control" id="inputPassword3">

        </div>

    </div>

    <div class="form-group">

        <label for="inputPassword3" class="col-sm-3 control-label"></label>

        <div class="col-sm-9">

            <button type="submit" class="btn btn-default">登录</button>

        </div>

    </div>

页面中存在很多布局相关的代码,大家可以直接略过,重点看表单元素相关的标签即可
LoginServlet

@WebServlet("/login")

public class LoginServlet extends HttpServlet {

    private IEmployeeService service = new EmployeeServiceImpl();

 

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");

        String password = req.getParameter("password");

        Employee currentUser = service.login(name, password);

        if(currentUser==null){

            //登录失败

            req.setAttribute("errorMsg","亲,账户或者密码错误");

            req.getRequestDispatcher("/login.jsp").forward(req,resp);

            return;

        }else{

            //登录成功

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值