Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 CRUD with servlets project

2 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.

3 initial session info shows not logged in but lookup finds entry

4 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+"'");

5 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(" Students "); out.println(" "); while(rs.next()) { out.println(" " + rs.getString("name") + " " + rs.getString("info") ); ct++; }// out.println(" "); out.println(" "); } if(ct==1){session.setAttribute("login", "Logged In"); out.println("correct login");}

6 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");

7 Having entered correct log in

8 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+“’”;

9 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);

10 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( );

11 Servlets can redirect A servlet can redirect to another servlet using send redirect. response.sendRedirect(res.encodeRedirectURL( request.getContextPath() + “OtherServletMapping"));


Download ppt "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."

Similar presentations


Ads by Google