Tuesday, July 17, 2018

The MAGIC of Collection in Web-Applications

Note: Before using these codes please follow the given guidelines

1. create a database 'cars' in MySQL server

>> create database cars; 

2. make this database as a default database

>> use cars;

3. create a table 'car' inside this database

>> create table car(car_id int,car_name varchar(100),car_model varchar(100), car_maker varchar(100),primary key(car_id));

note: here car_id is a primey key (always unique, and not null)

4. insert some records inside this table

>> insert into car values('1','alto','2016','maruti');
>> insert into car values('2','swift','2017','maruti');
>> insert into car values('3','ritz','2018','maruti');
>> insert into car values('4','amaze','2017','honda');

5. create a DYNAMIC-WEB-PROJECT (web-application) in Eclipse IDE and do the following stpes
5.1 create three jsp files inside WEB-CONTENT folder  names of the jsp files must be index.jsp, show_cars.jsp, and show_cars2.jsp

5.2 create a package inside Java Resources folder, and create a public class inside this beans package, this class must follow the java beans guidelines 

The Bean class is used to store and share the data mainly in web-applications 

Java bean guidelines means [class must be public and must be inside a package, its data members must be private, it must have public setter and getter methods, and it must have  a public no argument constructor]

5.3. copy and paste mysql-connector.jar files inside the lib folder (lib folder is available inside WEB-INF folder)

Some information about the code:

1. The index.jsp will show a hyperlink
2. when a client click on that the show_cars.jsp will be executed
3.  show_cars.jsp will fetch the data of car table and will store the data inside the object of bean class (that is Car)
4. after that the objects of Car class will be stored inside a collection and the collection will be further stored inside the object of session
5. the controls moves to show_cars2.jsp
6. show_cars2.jsp  will fetch the collection from the session object and later fetch the object of bean from the collection (using for each loop) 
7. then show_cars2.jsp fetches the data from the bean object using getter methods and show the data at cleint side

<< code of index.jsp starts >>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="show_cars.jsp">show the car details</a>
</body>
</html>

<< code of index.jsp ends >>


<< code of Car.java starts >>

package beans; // <<== look here, package name is beans  

public class Car 
{
// data members
private int id;
private String name, maker, model;

// getters and setters to access the data members
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getMaker() {
return maker;
}

public void setMaker(String maker) {
this.maker = maker;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}
}

<< code of Car.java ends >>


<< code of show_cars.jsp starts >>

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
  
<%--import JDBC API --%>
<%@ page import="java.sql.*" %>   
    
<%-- import bean class --%>    
<%@ page import="beans.*" %>    
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
// load driver class
Class.forName("com.mysql.jdbc.Driver");

// get a connection
Connection co = DriverManager.getConnection
("jdbc:mysql://localhost:3306/cars","root","root");

// get a statement
Statement st = co.createStatement();

// create a sql query
String sql = "select * from car";

// execute sql query to fetch all the columns of table 'car'
ResultSet rs = st.executeQuery(sql);

// create an ArrayList (it is a collection) to store 
// objects of car
ArrayList<Car> listOfCars = new ArrayList<Car>();

// fetch the records from the ResultSet
while(rs.next()) // fetch a record from ResultSet
{
// fetch data of all the columns of this record
int id = rs.getInt("car_id");
String name = rs.getString("car_name");
String model = rs.getString("car_model");
String maker = rs.getString("car_maker");

// create object of bean class
Car car = new Car();

// store the data of all the columns inside the car using setter
// methods
car.setId(id);
car.setName(name);
car.setMaker(maker);
car.setModel(model);

// store the object of car inside the collection using add() method
listOfCars.add(car);
}

// store the collection inside the session, so that it can be used anywhere
session.setAttribute("CARINFO",listOfCars);
%>
<%-- 
=>> pass the same client request to show_cars2.jsp 
=>> in this case the response will be given by the show_cars.jsp, but
the url of show_cars.jsp will be displayed to the client
--%>
<jsp:forward page="show_cars2.jsp"/>
</body>
</html>

<< code of show_cars.jsp ends >>



<< code of show_cars2.jsp starts >>

<%--  import bean class --%>
<%@page import="beans.Car"%>

<%--  import ArrayList --%>
<%@page import="java.util.ArrayList"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>

<%--show a heading in blue color --%>
<h1 style="color : blue"> << Data of Car table >></h1>

<%-- show a table --%>
<table border="1">

<%-- show table headings --%>
<th>Id</th> <th>Name</th> <th>Model</th> <th>Maker</th>
<%
// fetch the object of collection from the session's object using
// setAttribute() method
// here, downcasting is mandatory
ArrayList<Car> list = (ArrayList<Car>) session.getAttribute("CARINFO");

// fetch the objects of car from the collection using for-each loop
for(Car car : list) 
{
// fetch the data from the car using getter methods
int id = car.getId();
String name = car.getName();
String maker = car.getMaker();
String model = car.getModel();

// the first scriptlet tag is closed
%> 

<%-- show a table row --%>
<tr>

<%-- 
=>> show a table data (means column) 
=>> here we using expression tag to show the data of variables inside
the table data that is denoted by <td></td>
--%>
<td><%=id%></td>
<td><%=name%></td>
<td><%=maker%></td>
<td><%=model%></td>
</tr>

<% // second scriptlet tag starts

// end of the for-each loop
}

// second scriptlet tag closed here
%>
</table>
</center>
</body>

</html>

<< code of show_cars2.jsp ends >>

No comments:

Post a Comment