package webIM; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; /** * Title: Validate * Description: This servlet is the first one invoked in the application, i.e., * when the user submits a username and password. A session is * created and initialized, and control is passed to a JSP page * that will build the buddy list. * Copyright: Copyright (c) 2001 * Company: UCCS Computer Science Department * @author Merlin Vincent * @version 1.0 */ public class Validate extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; /**Initialize global variables*/ public void init(ServletConfig config) throws ServletException { super.init(config); } /**Process the HTTP Post request*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); String userIn = null; String passwdIn = null; HttpSession session = null; ServletContext context = null; try { // Retrieve the POSTed username and password userIn = request.getParameter("UserName"); passwdIn = request.getParameter("Password"); // Retrieving the password will verify the user has a properties file. String passwdLocal = getCustomerInfo( userIn, "Password" ); if ( !passwdIn.equals( passwdLocal )) { out.println(""); out.println("

Incorrect password...

"); out.println(""); } else { // Create and initialize a session for this user. String nickname = getCustomerInfo( userIn, "NickName" ); session = request.getSession(true); session.setAttribute("username", userIn); session.setAttribute("nickname", nickname); context = getServletContext(); context.setAttribute(nickname+"selectedBuddy", "none"); /** * Each user has a login status file in their public_html/web directory, * i.e., http://cs.uccs.edu/~/webIM/_online.jpg * or _offline.jpg. In real life these would be manipulated * by the program to reflect status... I haven't found a way to do * that from Java yet, and it's a minor matter, so I fake it with * shell scripts. Useless code follows... */ String path = "/users/server/students/~"+ userIn + "/public_html/webIM/"; File loginStatusFile = new File(nickname +"_offline.jpg"); // Forward control to the JSP that builds the buddy list. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/createBuddyList.jsp" ); dispatcher.forward( request, response ); } } catch (MissingResourceException mre) { out.println(""); out.println("

Unknown user "+ userIn +"

"); out.println(""); } catch (IOException ioe) { out.println(""); out.println("

Unable to create login status file...

"); out.println(""); } catch (Exception e) { throw new ServletException("doPost: "+ e.toString()); } } /** * Method: getCustomerInfo * * This method retrieves a value from the customer's properties file. */ public static String getCustomerInfo( String custName, String key ) throws Exception { String ret = null; ResourceBundle m_custInfo = null; try { if(( key != null ) && !key.equals( "" )) { if (m_custInfo == null) { m_custInfo = ResourceBundle.getBundle( custName ); } ret = m_custInfo.getString( key ); } } catch (Exception e) { throw new Exception("getCustomerInfo: "+ e.toString()); } return ret; } /**Clean up resources*/ public void destroy() { } }