package forms; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * A login servlet * * @version 1.0 2001/08/15 * @author Mike Gehard */ public class Login extends HttpServlet{ /** Used to hole the username/password combinations */ protected Map users = new HashMap(); /** Constant for the username field*/ public static String USERNAME = "username"; /** Constant for the passowrd field*/ public static String PASSWORD = "password"; /** * Initializaion method. Sets up username/password combinations */ public void init(){ // user names and passwords users.put("Mike Gehard", "se452"); users.put("Scott McNealy", "java"); users.put("Bill Gates", "windows"); users.put("Larry Ellison", "oracle"); } /** * Handle POST request. * 1) Gets information from form * 2) Validates login data * 3) Displays results of validation * * @param request the HTTPServletRequest associated with this servlet * @param response the HTTPServletResponse associated with this servlet * @throws ServletException * @throws IOException * */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // get the form data String userName = request.getParameter(USERNAME); String password = request.getParameter(PASSWORD); //prep the data if (userName != null){ userName = userName.trim(); } if (password != null){ password = password.trim(); } // look up login/password combiniation if (userName != null && userName.length() > 0){ if (password != null && password.length() > 0){ String pw = (String) users.get(userName); if (pw != null){ //you found the login if (pw.equals(password)){ // check the password // get the first name of the user String firstName = userName; // look for a space int i = userName.indexOf(' '); if (i > 0){ firstName = userName.substring(0, i); } sendPage(response, "Login successful", "Hello " + firstName + "!"); } else { // you didn't match the password sendPage(response, "Login fail", "Sorry, incorrect password"); } } else { sendPage(response, "Login fail", "Sorry, not a user"); } } else { sendPage(response, "Login fail", "Sorry, no password"); } } else { sendPage(response, "Login fail", "Sorry, no username"); } } /** * Handle GET request. * Delagates to doPost(). * * @param request the HTTPServletRequest associated with this servlet * @param response the HTTPServletResponse associated with this servlet * @throws ServletException * @throws IOException * */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // forward to doPost() doPost(request, response); } /** * Handle generation of the response page sent to the user based on passed * parameters. * * @param response the HTTPServletResponse associated with this servlet * @param title the title for the generated page * @param message the message to be displayed on the page * @throws IOException * */ protected void sendPage(HttpServletResponse response, String title, String message) throws IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); if (title != null) { out.println("