Demo entry 5865373

Java

   

Submitted by anonymous on Aug 19, 2016 at 09:36
Language: Java. Code size: 1.9 kB.

package com.web.web.controller;

import com.web.domain.User;
import com.web.service.IUserService;
import com.web.service.UserServiceImpl;
import com.web.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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 java.io.IOException;

/**
 * Created by Lenovo on 2016/7/12 0012.
 */
@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {

    private static final Logger logger = LoggerFactory.getLogger(Log.class);

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

        //获取用户填写的登录用户名
        String username = request.getParameter("username");
        //获取用户填写的登录密码
        String password = request.getParameter("password");

        IUserService service = new UserServiceImpl();
        //用户登录

        User user = service.loginUser(username, password);
        System.out.println("用户登录");
        if(user == null){
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("<script>alert('系统中没有该用户');window.location='/index.jsp' ;window.close();</script>");
            response.getWriter().flush();
            return;
        }
        //登录成功后,就将用户存储到session中

        request.getSession().setAttribute("user", user);

        logger.info("{} 登录",username);


        request.getRequestDispatcher("/search.jsp").forward(request, response);
    }

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

    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).