会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132435个问题
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 17101楼
WEB前端全系列/第十三阶段:微信小程序-安心食疗(旧)/安心食疗-项目基础配置 17103楼
JAVA 全系列/第十三阶段:分布式文件存储与数据缓存/Redis 17104楼
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 17106楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 17108楼
Python 全系列/第五阶段:数据库编程/MySQL数据库的使用 17110楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 17111楼
JAVA 全系列/第九阶段:权限控制与安全认证/Spring Security(旧) 17112楼
人工智能/第六阶段:机器学习-线性分类/SMO优化算法 17114楼

package com.bjsxt.web.servlet;

import com.bjsxt.commons.Constants;
import com.bjsxt.exception.UserNotFoundException;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserLoginServive;
import com.bjsxt.service.impl.UserLoginServiceImpl;

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 javax.servlet.http.HttpSession;
import java.io.IOException;

/*
    处理用户请求登录
*/
@WebServlet("/login.do") 
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("username"); 
        String pwd =req.getParameter("userpwd");
        try{
            UserLoginServive userLoginServive = new UserLoginServiceImpl(); 
            Users users =userLoginServive.userLogin(name,pwd); 
            /*
            建立客户端与服务端的会话状态
            */
            HttpSession session =req.getSession();
            //会话状态建立
            session.setAttribute(Constants.USER_SESSION_KEY,users);//这里的 键 调用了Constants类中的常量, 值 是上面的users
            //跳转到首页,使用重定向的方式实现跳转
            resp.sendRedirect("main.jsp");
        }catch (UserNotFoundException u){
            req.setAttribute("msg",u.getMessage());
            req.getRequestDispatcher("login.jsp").forward(req,resp); //重新跳转到登录页面
        }catch (Exception e){
            resp.sendRedirect("error.jsp"); //如发生其他异常的时候,跳转到一个错误提示页面
        }
    }
}

老师,在写servlet层的时候,上面这段代码中的

            UserLoginServive userLoginServive = new UserLoginServiceImpl(); 
            Users users =userLoginServive.userLogin(name,pwd);

这两行代码的作用是什么?

用来判断用户信息是否存在的嘛?


JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 17115楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637