package servlet;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.CachedRowSet;
import db.DBHelper;
import pojo.UserInfo;
/**
* Servlet implementation class LoginServlet
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static Map loginmap=new HashMap();
static{
loginmap.put("1", "/department.jsp");
loginmap.put("2", "/office.jsp");
loginmap.put("3", "/secretary.jsp");
loginmap.put("4", "/deputy_secretary.jsp");
loginmap.put("5", "/admin.jsp");
}
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
HttpSession httpSession=request.getSession();
if(httpSession.getAttribute("user")!=null){
UserInfo us=(UserInfo)(httpSession.getAttribute("user"));
response.sendRedirect(getServletContext().getContextPath()+loginmap.get(us.getUsertype()));
return;
}
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
String usertype=request.getParameter("usertype");
if((null==username)||(null==password)||(null==usertype)){
response.sendRedirect(getServletContext().getContextPath()+"/index.jsp");
}else{
String sqlstr="select * from users left join usertypes on users.usertypeid=usertypes.id where users.username='"+username+"' and users.password='"+password+"' and usertypes.usertype='"+usertype+"'";
ResultSet queryret=DBHelper.executeQuery(sqlstr);
if(null!=queryret){
try {
queryret.last();
if(queryret.getRow()>0){
UserInfo userinfo=new UserInfo();
userinfo.setId(queryret.getString("id"));
userinfo.setUsername(username);
userinfo.setUsertype(usertype);
httpSession.setAttribute("user",userinfo);
response.sendRedirect(getServletContext().getContextPath()+loginmap.get(usertype));
}else{
response.sendRedirect(getServletContext().getContextPath()+"/index.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}