Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BKBIET Phase I - Day 2 and 3

BKBIET Phase I - Day 2 and 3

Presentation given to engineering student @ BKBIET, focussing on java servlets and database programming

Mihir Malaviya

June 04, 2013
Tweet

More Decks by Mihir Malaviya

Other Decks in Education

Transcript

  1. Today.... ✤ Technical ✤ Servlet Platform ✤ tomcat ✤ Database

    ✤ postgreSQL,mysql,h2 ✤ Atlassian - Jira
  2. Servlets ✤ Servlets - Java CGI? ✤ run inside a

    servlet container ✤ encapsulate HTTP in java objects ✤ HTTPServletRequest ✤ HTTPServletResponse ✤ provide methods to process HTTP methods ✤ doGet ✤ doPost public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Hello World!</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Hello World!</h1>"); out.println("</body>"); out.println("</html>"); } }
  3. Exercise! ✤ Create a Hello World Servlet! ✤ create a

    project in eclipse ✤ create src file ✤ create web.xml file ✤ add source/details ✤ deploy! ✤ Development Stations!!
  4. Databases ✤ Relational Databases ✤ ACID ✤ Transactions ✤ JDBC

    ✤ Database Connections ✤ Queries ✤ ResultSets!
  5. Exercise ✤ Create a db bkbiet ✤ Create a table

    bkbiet ✤ id ✤ name ✤ age ✤ email ✤ Use java to connect to db ✤ insert your own data in table CREATE TABLE `bkbiet`.`bkbiet` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(45) NOT NULL , `stream` VARCHAR(45) NOT NULL , `email` VARCHAR(45) NOT NULL , PRIMARY KEY (`id`) , UNIQUE INDEX `id_UNIQUE` (`id` ASC) ); String insertTableSQL = "INSERT INTO DBUSER" ! ! + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES" ! ! + "(?,?,?,?)"; PreparedStatement preparedStatement = dbConnection.prepareStatement(insertTableSQL); preparedStatement.setInt(1, 11); preparedStatement.setString(2, "mkyong"); preparedStatement.setString(3, "system"); preparedStatement.setTimestamp(4, getCurrentTimeStamp()); // execute insert SQL stetement preparedStatement .executeUpdate();
  6. Templates ✤ Difficult to write long html pages ✤ requires

    templates ✤ Enter JSP ✤ Servlets feed data(model) to jsp ✤ converts to html pages ✤ JSP get converted to servlets at runtime
  7. MVC Recall ✤ Model-View-Controller ✤ separate the data from the

    view ✤ Servlet: Controller ✤ DAO: Data Access Object ✤ View: JSP
  8. Exercise ✤ Create a web site for users ✤ enter

    their details into the db ✤ create a page to view all details ✤ search for a user <HTML> <HEAD> <TITLE>A Sample Form Using GET</TITLE> </HEAD> <BODY> <FORM ACTION="http://localhost:8080/bkbiet/ BKBIETServlet" METHOD=”POST”> <CENTER> Name: <INPUT TYPE="TEXT" NAME="name" VALUE="Joe"><BR> Last name: <INPUT TYPE="TEXT" NAME="age" VALUE="26"><BR> <INPUT TYPE="email" NAME="email" VALUE="[email protected]"><P> <INPUT TYPE="SUBMIT"> <!-- Press this button to submit form --> </CENTER> </FORM> </BODY> </HTML>