package com.wxpn.tutorial.servlet;

import java.io.IOException;

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

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

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

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = request.getParameter("name");
		String password = request.getParameter("pass");
		int classid = Integer.parseInt(request.getParameter("classid"));

		MasterMgr masterMgr = new MasterMgr();
		
		if	(masterMgr.isExist(username)) {
			response.sendRedirect("error2.jsp");
		}
		else {
			Master master=new Master();
			master.setCategoryid(classid);
			master.setUsername(username);
			master.setUserpwd(password);
			masterMgr.add(master);
			
			response.sendRedirect("admin/savemaster.jsp");
		}
	}
}