Current Topic: 4.4.4.1.Data Service Framework basic
You have a privilege to create a quiz (QnA) related to this subject and obtain creativity score...
4.4.4.1.Data Service Framework There are several frameworks, including Hybernate, Spring, and DataService, that recognize and solve common data processing issues and allow us to minimize our coding efforts and focus on specific business cases.
The Hybernate and Spring frameworks provide object-relational mappings and offer creating SQL statements on-the-fly. Developers don’t need to be familiar with the database structures; they just create initial configuration files instead.
The DataService framework is the smallest one. The framework requires developers to know data structure and use this knowledge to create good SQL statements. The framework allows developers keep SQL statements in separate files that can be directly used for testing SQL with the TOAD or SQLPlus tools. The DataService takes care of data source initializations, data connections, and processing result sets.
The DataService class is the façade of the com.util.jar library. This is a single jar file with a very small footprint.
The library includes the ItsBaseServlet class that reads initialization parameters for a web application from the web.xml file, and makes data sources available. The initialization procedure is called automatically when application is started.
There are several pieces of data that we pass to the framework via the web.xml file
1. Provide the name of the project, which is actually the name of the application, so it goes as appName.
The application will also need to work with the file system.
Let the application know where it is deployed to make this work a bit easier.
Note two values below separated by comma: one for Windows deployment and another for Linux deployment, for example in the AWS environment.
DataService will recognize Operating System and will use a proper pathToDeploy value.
2. Provide the description of the data sources in the file located in the deploy directory named as applicationName-ds.xml, for example MyApp-ds.xml.
Map data sources to the application in the web.xml file using context-param elements. Use the names for data source that end with “DataSource”, for example, “MyAppDataSource”.
MyAppDataSource MyAppDataSource
In the case when the number of databases and tables is not very significant, directly describe in the web.xml all metadata.
For example, if you have just one database, list the tables as comma separated parameters.
It is expected that the request will always include url-pattern /Lookup and the following parameters will point to a real action or a page.
Briefly about the ItsBaseServlet class. This class extends HttpServlet and implements the following interfaces:
- HttpSessionListener, // allows to listen to session events, such as createSession and destroySession and insert in these methods a specific behavior
- ServletContextListener // allows to listen to the servlet context events, such as initialization. Implement the initialize() method to provide specific to your application initialization procedures not related to database sources.
The ItsBaseServlet class includes the doPost() and doGet() methods that respond to a request from a browser. Similar to the Dispatcher class in the Spring this class will delegate control to an action class or to a Java Server Page.
This can be done with the simple URL, such as:
/BASE/Lookup?action=content
Or in a more formal REST presentation
/BASE/Lookup/content
How to use the Data Service framework: a) Copy the com.util.jar library to the WEB-INF/lib directory and make it a part of the Java Build Path.
b) Handle data by using the DataService API (see examples below and DataService API)
Example:
Store the select statement below in the file WEB-INF/sql/getUser.sql
You can use Statement or PreparedStatement in the DataService framework
Prefered way is using PreparedStatement, which besides performance benefits also provides more security and has embedded work around symbols, such as a single quote and more, which may appear in SQL statements.
Example with the Statement
getUser.sql – file in the Eclipse project in WebContent/config/sql – directory
Select * from users where loginName = ‘:loginName’
Provide this code in your action class to execute this statement in the MyAppDataSource.
// See recommendations on the action class and find more details on the HashMap keys
keys.put(“loginName”, form.getLoginName()); // common HashMap keys
List records = DataService.getData(“getUser”, keys, User.class, “MyAppDataSource”);
If(records.size() == 0) {
// no users found
return “error”; // forward to the Error page
}
User user = (User) records.get(0);
request.getSession().setAttribute(“user”, user);
return “success”; // forward to the next page
For PreparedStatement:
insertUser.sql – file in the Eclipse project in WebContent/config/sql – directory
INSERT into User values(? , ?)
/* parameters:
- name of the sqlFile - insertUser refers to insertUser.sql file
- String array of run-time parameters to replace "?"
- the name of data source - in this case "myDB"
*/
DataService.setPrepDataBySqlName("insertUser", new String[] {login, psw}, "myDB");
2. Provide the description of the data sources in the file located in the deploy directory named as applicationName-ds.xml, for example MyApp-ds.xml.
Map data sources to the application in the web.xml file using context-param elements. Use the names for data source that end with “DataSource”, for example, “MyAppDataSource”.
In the case when the number of databases and tables is not very significant, directly describe in the web.xml all metadata.
For example, if you have just one database, list the tables as comma separated parameters.
It is expected that the request will always include url-pattern /Lookup and the following parameters will point to a real action or a page.
Briefly about the ItsBaseServlet class. This class extends HttpServlet and implements the following interfaces:
- HttpSessionListener, // allows to listen to session events, such as createSession and destroySession and insert in these methods a specific behavior
- ServletContextListener // allows to listen to the servlet context events, such as initialization. Implement the initialize() method to provide specific to your application initialization procedures not related to database sources.
The ItsBaseServlet class includes the doPost() and doGet() methods that respond to a request from a browser. Similar to the Dispatcher class in the Spring this class will delegate control to an action class or to a Java Server Page.
This can be done with the simple URL, such as:
/BASE/Lookup?action=content
Or in a more formal REST presentation
/BASE/Lookup/content
How to use the Data Service framework: a) Copy the com.util.jar library to the WEB-INF/lib directory and make it a part of the Java Build Path.
b) Handle data by using the DataService API (see examples below and DataService API)
Example:
Store the select statement below in the file WEB-INF/sql/getUser.sql
You can use Statement or PreparedStatement in the DataService framework
Prefered way is using PreparedStatement, which besides performance benefits also provides more security and has embedded work around symbols, such as a single quote and more, which may appear in SQL statements.
Example with the Statement
getUser.sql – file in the Eclipse project in WebContent/config/sql – directory
Select * from users where loginName = ‘:loginName’
Provide this code in your action class to execute this statement in the MyAppDataSource.
// See recommendations on the action class and find more details on the HashMap keys
keys.put(“loginName”, form.getLoginName()); // common HashMap keys
List records = DataService.getData(“getUser”, keys, User.class, “MyAppDataSource”);
If(records.size() == 0) {
// no users found
return “error”; // forward to the Error page
}
User user = (User) records.get(0);
request.getSession().setAttribute(“user”, user);
return “success”; // forward to the next page
For PreparedStatement:
insertUser.sql – file in the Eclipse project in WebContent/config/sql – directory
INSERT into User values(? , ?)
/* parameters:
- name of the sqlFile - insertUser refers to insertUser.sql file
- String array of run-time parameters to replace "?"
- the name of data source - in this case "myDB"
*/
DataService.setPrepDataBySqlName("insertUser", new String[] {login, psw}, "myDB");