Crud with servlets project
This presentation is the property of its rightful owner.
Sponsored Links
1 / 11

CRUD with servlets project PowerPoint PPT Presentation


  • 48 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Crud with servlets project

CRUD with servlets project


Login

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

initial session info shows not logged in but lookup finds entry


Reminder querying a table

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

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

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


Having entered correct log in

Having entered correct log in


Update delete insert read

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

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

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

Servlets can redirect

A servlet can redirect to another servlet using send redirect.

response.sendRedirect(res.encodeRedirectURL( request.getContextPath() + “OtherServletMapping"));


  • Login