1 / 11

CRUD with servlets project

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.

kiara
Télécharger la présentation

CRUD with servlets project

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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("<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");}

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

More Related