javaweb超市管理系统--登录设置

思路

  1. servlet层:获得用户输入的用户名和密码,使用service层的返回值进行验证并设置验证后的跳转。
  2. service层:获得输入的值,类似于一个中间过程。
  3. dao层通过输入的值在数据库中进行查询,并获得此用户的其他所有信息。

dao层

//先创建一个接口
public user getLoginUser(Connection connection,String usercode,String password) throws SQLException;
//接口的实现类
public user getLoginUser(Connection connection, String usercode,String password) throws SQLException {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;
        user u = null;
        if (connection!=null){
            String sql ="select * from smbms_user where userCode = ? && userPassword = ?";
            Object[] params = {usercode,password};
            resultSet = BaseDao.Excute(connection, sql, params, resultSet, preparedStatement);
//            resultset是结果集
            if (resultSet.next()){
                u=new user();
                u.setId(resultSet.getInt("id"));
                u.setAddress(resultSet.getString("address"));
                u.setBirthday(resultSet.getDate("birthday"));
                u.setCreatedBy(resultSet.getInt("createdBy"));
                u.setCreationDate(resultSet.getDate("creationDate"));
                u.setGender(resultSet.getInt("gender"));
                u.setModifyBy(resultSet.getInt("modifyBy"));
                u.setModifyDate(resultSet.getDate("modifyDate"));
                u.setPhone(resultSet.getString("phone"));
                u.setUserCode(resultSet.getString("userCode"));
                u.setUserName(resultSet.getString("userName"));
                u.setUserPassword(resultSet.getString("userPassword"));
                u.setUserRole(resultSet.getInt("userRole"));

            }
            BaseDao.Close(resultSet,preparedStatement,null);
        }
        return u;

service层

//接口
public user login(String userCode,String Password);
//实现类
public class UserServiceImpl implements UserService {
    private UserDao userDao;
//使用构造方法,确保一定会被初始化
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }
//登录方法
    public user login(String userCode, String Password) {
        Connection connection =null;
        user user = null;
        try {
            connection = BaseDao.GetConnection();
            user=userDao.getLoginUser(connection,userCode,Password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.Close(null,null,connection);
        }
        return user;
    }

servlet层

  1. 这层中将user信息以USER_SESSION名称传入了SESSION内,判断输入的是否正确的判断就是查看session中是否有对应的值。
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userCode = req.getParameter("userCode");
        String userPassword = req.getParameter("userPassword");
        UserService userService = new UserServiceImpl();
        user loginuser = userService.login(userCode, userPassword);
        if (loginuser!=null){
            //将user信息以USER_SESSION名称传入了SESSION内
            req.getSession().setAttribute(Constants.USER_SESSION,loginuser);
            resp.sendRedirect("jsp/frame.jsp");//重定向
        }else {
            req.setAttribute("error","用户名或者密码不正确");//error对应的是login.jsp中的error
            req.getRequestDispatcher("login.jsp").forward(req,resp);//转发回来
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}

2.配置web.xml

    <servlet>
        <servlet-name>Loginservlet</servlet-name>
        <servlet-class>com.wu.servlet.user.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Loginservlet</servlet-name>
        <url-pattern>/login.do</url-pattern>
    </servlet-mapping>

util层

存放了整个项目所需要的常量

public class Constants {
    public static final String USER_SESSION = "userSession";
}

其他

  1. 登录页面的代码
    ${pageContext.request.contextPath }/login.do
  • 使用的el表达式,获取的是项目的http路径。login.do是跳转的中间变量,要在web.xml中配置。
<div class="info">${error}</div>
  • 当输入的密码或者用户名错误时的情况。error是自己定义的名称。
<section class="loginCont">
	        <form class="loginForm" action="${pageContext.request.contextPath }/login.do"  name="actionForm" id="actionForm"  method="post" >
				<div class="info">${error}</div>
				<div class="inputbox">
                    <label for="userCode">用户名:</label>
					<input type="text" class="input-text" id="userCode" name="userCode" placeholder="请输入用户名" required/>
				</div>	
				<div class="inputbox">
                    <label for="userPassword">密码:</label>
                    <input type="password" id="userPassword" name="userPassword" placeholder="请输入密码" required/>
                </div>	
				<div class="subBtn">
					
                    <input type="submit" value="登录"/>
                    <input type="reset" value="重置"/>
                </div>	
			</form>
        </section>
  1. 关于转发和重定向

转发是服务器内部的跳转,是在request中设置。

request.getRequestDispatcher("/地址").forward(request, response);

重定向是在浏览器端进行的页面跳转,由response调用。

resp.sendRedirect("重定向的网址")

重定向实际上是发送了两次请求,所以安全性较差。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值