You are on page 1of 3

import import import import import import import import import import import import import import

java.io.IOException; java.io.PrintWriter; java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.SQLException; java.sql.Statement; javax.servlet.ServletConfig; javax.servlet.ServletException; javax.servlet.UnavailableException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.servlet.http.HttpSession;

public class Login extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); try { // load the driver Class.forName("oracle.jdbc.driver.OracleDriver").newInst ance(); } catch (ClassNotFoundException e) { throw new UnavailableException("Login init() ClassNotFou ndException: " + e.getMessage()); } catch (IllegalAccessException e) { throw new UnavailableException("Login init() IllegalAcce ssException: " + e.getMessage()); } catch (InstantiationException e) { throw new UnavailableException("Login init() Instantiati onException: " + e.getMessage()); } } public void doGet(HttpServletRequest request, HttpServletResponse respon se)throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); System.out.println("<html>"); System.out.println("<head>"); System.out.println("<title>Login</title>"); System.out.println("</head>"); System.out.println("<body>"); HttpSession session = request.getSession(); Connection connection = (Connection) session.getAttribute("conne ction"); if (connection == null)

{ String userName = request.getParameter("username"); String password = request.getParameter("password"); if (userName == null password == null) { // prompt the user for her username and password System.out.println("<form method=\"get\" action= \"Login\">"); System.out.println("Please specify the following to log in:<p>"); System.out.println("Username: <input type=\"text \" "+ "name=\"username\" size=\"30\"><p>"); System.out.println("Password: <input type=\"pass word\" "+ "name=\"password\" size=\"30\"><p>"); System.out.println("<input type=\"submit\" value =\"Login\">"); System.out.println("</form>"); } else { // create the connection try { connection = DriverManager.getConnection ("jdbc:oracle:thin:@dssw2k01:1521:orcl", userName,password); } catch (SQLException e) { System.out.println("Login doGet() " + e. getMessage()); } if (connection != null) { // store the connection session.setAttribute("connection", conne ction); response.sendRedirect("Login"); return; } } } else { String logout = request.getParameter("logout"); if (logout == null) { // test the connection Statement statement = null; ResultSet resultSet = null; String userName = null; try { statement = connection.createStatement() ; resultSet = statement.executeQuery("sele ct initcap(user) from sys.dual"); if (resultSet.next()) userName = resultSet.getString(1); } catch (SQLException e)

{ System.out.println("Login doGet() SQLExc eption: " + e.getMessage()+ "<p>"); } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) { } if (statement != null) try { statement.close(); } catch (SQLException ignore) { } } System.out.println("Hello " + userName + "!<p>"); System.out.println("Your session ID is " + session.getId () + "<p>"); System.out.println("It was created on "+ new java.util.D ate(session.getCreationTime())+ "<p>"); System.out.println("It was last accessed on "+ new java. util.Date(session.getLastAccessedTime())+ "<p>"); System.out.println("<form method=\"get\" action=\"Login\ ">"); System.out.println("<input type=\"submit\" name=\"logout \" "+ "value=\"Logout\">"); System.out.println("</form>"); } else { // close the connection and remove it from the session try { connection.close(); } catch (SQLException ignore) { } session.removeAttribute("connection"); System.out.println("You have been logged out."); } } System.out.println("</body>"); System.out.println("</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse respo nse)throws IOException, ServletException { doGet(request, response); } }