import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class Login extends HttpServlet { Connection connection; public void init() throws ServletException { //initialization try { Class.forName("com.mysql.jdbc.Driver").newInstance(); connection = DriverManager.getConnection("jdbc:mysql://localhost/srollins?user=srollins&password=srollins"); } catch(Exception e) { throw new ServletException("Problem connecting to database"); } } public void destroy() { //cleanup } public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // set content-type header before accessing the Writer response.setContentType("text/html"); response.setBufferSize(8192); PrintWriter out = response.getWriter(); int idnum = 0000; String password = null; String storedpass = null, firstname = null, lastname = null; ResultSet rs = null; Statement stmt = null; HttpSession session = request.getSession(); String login = request.getParameter("login"); password = request.getParameter("passwd"); try { idnum = Integer.parseInt(login); stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT * FROM student where STUID=" + idnum); if (rs != null && rs.next()) { storedpass = rs.getString("password"); firstname = rs.getString("firstname"); lastname = rs.getString("lastname"); } } catch(Exception e) { e.printStackTrace(out); } if(storedpass.equals(password)) { session.setAttribute("connection", connection); session.setAttribute("studentid", new Integer(idnum)); session.setAttribute("firstname", firstname); session.setAttribute("lastname", lastname); session.setAttribute("password", password); out.println("" + "Show Student"); out.println(""); out.println("

" + firstname + " " + lastname + "

"); try { rs = stmt.executeQuery("select course.* from course left join reg_entry on course.COURSEID=reg_entry.COURSEID where reg_entry.STUID=" + idnum); if(rs != null && rs.next()) { out.println("

"); out.println(""); out.println("

"); } } catch(Exception e) { e.printStackTrace(out); } } else { out.println("Bad password"); } out.println(""); out.println(""); out.close(); } public String getServletInfo() { return "Login servlet"; } }