MVC开发用户模块三

前两篇主要是开发了注册功能,接下来我们进行登录功能的开发:
一、对 index.jsp 界面做修改,设计的内容后面会有讲到

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


    首页




    欢迎您:${user.nickname } 注销

注册
登录

二、显示登录界面的 LoginUIServlet

package cn.itcast.web.UI;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//提供登陆界面
public class LoginUIServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

三、登录界面 login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    登录界面



用户:
密码:

四、处理登录请求的 LoginServlet

package cn.itcast.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.domain.User;
import cn.itcast.service.impl.BusinessServiceImpl;

public class LoginServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        BusinessServiceImpl service = new BusinessServiceImpl();
        User user = service.login(username, password);
        if (user != null) {
            //在session里存入一个登录标记
            request.getSession().setAttribute("user", user);
            //用户登录成功,跳转到首页
            response.sendRedirect(request.getContextPath() + "/index.jsp");
            return;
        }
        request.setAttribute("message", "用户名或密码错误");
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

五、处理用户注销请求的 LoginOutServlet

package cn.itcast.web.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginOutServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.removeAttribute("user");
        }
        //注销成功,跳到全局消息显示页面,显示注销成功信息
        request.setAttribute("message", "注销成功!!!浏览器将在3秒后跳转...");
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}

接上篇:http://www.jianshu.com/p/2121014cbaef
源代码:https://github.com/yvettee36/MVC_User

你可能感兴趣的:(MVC开发用户模块三)