170 likes | 419 Views
CRUD with servlets project. login. Servlets support various authentication mechanisms You can always code your own login as we did with php Build a table of user names and logins Provide a form to get a name and login. Query the login table. Result should be a single record.
E N D
login • Servlets support various authentication mechanisms • You can always code your own login as we did with php • Build a table of user names and logins • Provide a form to get a name and login. • Query the login table. Result should be a single record. • Set session data or a cookie. I only show session here.
initial session info shows not logged in but lookup finds entry
Reminder… querying a table • I just queried the table with form data. • If name/pw pairs are primary keys in the login table and the query resultset returns not null then the name/pw are legal. • Note extra single quotes below • rs = stmt.executeQuery("SELECT * from members where Name='"+name+"' and Password='"+pw+"'");
lookup a name…set login to success or failure HttpSession session = req.getSession(); session.setAttribute("login","NOT LOGGED IN"); try { int ct=0;//if you need a cout of records Class.forName("org.gjt.mm.mysql.Driver"); con = DriverManager.getConnection( "jdbc:mysql://localhost/users", "root", "");//or user= "root", pw=""Class.forName("org.gjt.mm.mysql.Driver"); String sql="SELECT * from members where Name='"+name+"' and Password='"+pw+"'"; stmt = con.createStatement(); // Execute an SQL query, get a ResultSet if(stmt.execute(sql)){ rs = stmt.getResultSet(); // Display the result set as a list out.println("<HTML><HEAD><TITLE>Students</TITLE></HEAD>"); out.println("<BODY>"); out.println("<UL>"); while(rs.next()) { out.println("<LI>" + rs.getString("name") + " " + rs.getString("info") ); ct++; }// out.println("</UL>"); out.println("</BODY></HTML>"); } if(ct==1){session.setAttribute("login", "Logged In"); out.println("correct login");}
A snippet of a servlet that checks for login status stored in session HttpSession session = req.getSession(); String status = (String)session.getAttribute("login"); if(status==null||status.equals("NOT LOGGED IN")) out.println("login status: NOT LOGGED IN"); else out.println("login status: LOGGED IN");
Update, delete, insert, read • Lots of examples of read, a few of insert. • For delete use a regular query where form parameters may need to be concatenated in with single quotes around them as in: • String name=req.getParameter(“name”); • String sql=“Delete * from members where Name=‘”+name+“’”;
update • Strings will need to be in single quotes. • Statement stmt=con.createStatement(); • String sql= “Update students set age=”+age+” where Name=‘”+name+”’”; • Stmt.executeUpdate(sql);
For insert you should use a prepared statement String name = request.getParameter("name"); String sage = request.getParameter("age"); String sgrade = request.getParameter("grade"); int age=Integer.parseInt(sage); double grade=Double.parseDouble(sgrade); out.println("adding"+name+","+grade+","+age); try { PreparedStatement pstmt=conn.prepareStatement("insert into table1 (name,grade,age) values (?,?,?)"); pstmt.clearParameters(); pstmt.setString(1,name); pstmt.setDouble(2,grade); pstmt.setInt(3,age); pstmt.executeUpdate( );
Servlets can redirect A servlet can redirect to another servlet using send redirect. response.sendRedirect(res.encodeRedirectURL( request.getContextPath() + “OtherServletMapping"));