package com.wxpn.tutorial.servlet;

import java.io.IOException;
import java.util.Collection;

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

import com.wxpn.tutorial.control.news.MasterMgr;

/**
 * Servlet implementation class for Servlet: LoginServlet
 */
public class LoginServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
	
		// 获取提交的用户名：
		String strUserName = request.getParameter("username");
		// 获取提交的密码：
		String strPassword = request.getParameter("password");
		MasterMgr masterMgr = new MasterMgr();
		if (masterMgr.checkLogin(strUserName, strPassword)) {
			// 登录成功时,把strUserName设成session变量UserName
			request.getSession(true).setAttribute("username", strUserName);
			request.getSession(true).setAttribute("password", strPassword);
			masterMgr.addloginnum(strUserName);

			response.sendRedirect("admin/main.jsp");
		} else {
			response.sendRedirect("admin/loginerror.jsp");
		}

	}
}