• No results found

Lab Exercise

Lab 2 Learn to Accelerate your web App development with the WebSphere Application Server Liberty profile

2.4 Configuring the server and running the application

2.4.1 Configuring the Data Source to connect to the database

__1. In this lab, a simple embedded derby database is required. In this step, you will make the derby.jar available as a shared library for the Liberty Profile Server where the Derby library will be available to the runtime environment.

__a. To do this, you will use eclipse to import the derby.jar file, from the Lab Files directory, into the Liberty Server shared resources folder.

__b. If not already started, Launch Eclipse and open the Java EE perspective.

__c. In the Enterprise Explorer view, Navigate to the shared resources folder in the Liberty Server configuration: WebSphere Application Server V8.5 Liberty Profile ! shared !

resources

__d. Create a new folder named derby.

__ii. Enter derby in the folder name field. And then click Finish to create the new folder.

__e. Import the derby.jar into the Derby shard resources folder.

__i. Right mouse click on the derby folder. And then select Import from the context menu.

__ii. On the Import dialog, select General ! File System. And then click the Next button.

__iii. On the File System Import Dialog, use the Browse button, next to the From Directory field, and navigate to “X:\Student\LabFiles\Lab02\”.

__v. Click the Finish Button to import the derby.jar file.

__2. The server does not start the necessary components required for database access unless configured to do so. This means the server is currently unable to access databases. To enable the database support the jdbc-4.0 feature needs to be configured.

__a. In Eclipse, Switch to the Java EE Perspective. This perspective includes the Servers view.

__b. Navigate to the Servers view and expand WebSphere Application Server V8.5 Liberty Profile at localhost [labServer]

This item can be expanded to show a logical view of the configuration. However you need to edit rather than view the configuration.

__c. Right click Server Configuration [server.xml] Liberty Server for Labs and click open.

__d. Select the Design tab on the configuration editor.

__e. Click the Feature Manager option from the Configuration Structure section.

__f. On the right side of the editor the details are viewable. To add a new feature click Add... to add a new feature into the list.

__g. Click the down arrow and select jdbc-4.0.

__h. Save the file.

If the server is still running after the earlier activity, switch to the console view to see the server report that it has responded to the new feature being configured. The server can now support database access; however no DataSources have been configured.

__3. The next step is to configure the server so it can locate the database jar from the previous step. __a. In the Design tab of the server configuration editor, click Server Configuration and click

Add.

__b. Start typing Shared Library and when highlighted click OK.

__c. The shared library needs to have an

id

so it can be used elsewhere. In the Shared Library Details section in the id field enter derby.

__d. In the Configuration Structure section, click Shared Library. And, then click Add.

__e. Click Fileset Reference. And then click the OK button.

__g. Save the configuration

This has created a shared library that contains the derby jar. If you switch to the Source view it looks like this:

<library id="derby">

<fileset dir="${shared.resource.dir}/derby"/> </library>

Notice that the Design view shows all the values, but the Source view only includes values which are different from the defaults.

__4. Back in the Design tab the next step is to define the JDBC driver. __a. Click ServerConfiguration and click Add.

__b. Type JDBC Driver and when selected click OK.

__c. On the JDBC Driver Details enter derbyDriver into the Id field. __d. In the Shared Libraries field select derby from the pull down menu.

__5. The next step is to define a DataSource

In the persistence.xml, this was created using the resource ref JNDI name java:comp/env/jdbc/blogDB.

By default, if no binding is provided, then the resource ref name of jdbc/blogDB is used to look up an object in JNDI.

__a. In the Design tab click Server Configuration and click Add. __b. Type Data Source and when highlighted click OK.

__c. In the JNDI name field on the Data Source Details enter jdbc/blogDB __d. In the JDBC driver field select derbyDriver from the pull down menu.

__6. Now, configure the derby specific properties:

__a. In the “Configuration Structure” click the DataSource and click Add. __b. Select Properties for Derby Embedded. And, thenclick OK.

__c. In the Properties forDerby Embedded Details, choose false from the Create Database pull down menu.

TIP: The Derby database required for the lab has already been created in the virtual image. Therefore, the create database flag should be set to false.

__d. In the Database name enter C:\Student\Derby\databases\blogDB

__e. Save the configuration.

Looking in the source view you should see the following:

<dataSource jndiName="jdbc/blogDB" jdbcDriverRef="derbyDriver" type="javax.sql.ConnectionPoolDataSource">

<properties.derby.embedded createDatabase="false" databaseName="C:\Student\Derby\databases\blogDB"/>

</dataSource>

This configures a derby Data Source in JNDI at jdbc/blogDB and specifies the location of the blogDB database.

__7. Add the JPA-2.0 feature to the Liberty Server configuration. __a. Select the Design tab on the configuration editor.

__c. On the right side of the editor the details are viewable. To add a new feature click Add... to add a new feature into the list.

__d. Click the down arrow and select jpa-2.0.

__8. The application has now been created, and the server configured to support it. The next step is to run the application.

__a. If the server is running, Stop the Liberty Profile Server [labServer], using the STOP icon located on the Server view.

__b. Start the Liberty Profile Server. Use the Start icon in the Servers view to start the server if necessary.

__c. When the server starts, navigate to the createBlogPost.html file in the Blog Web Project.

__d. Right click createBlogPost.html file in the Enterprise Explorer view. __e. Select RunAs > Run on Server.

__f. Select the WebSphere Application Server V8.5 Liberty Profile andclick Finish. __g. The application is be added to the server and the web page opened.

__9. Test the Blog application

__a. Enter a title and body into the blog and click the Submit Query button. The post is written to the database and a list of the posts shown.

__b. To prove that the post was persisted, select the server in the servers view, rightclick on the server, and select restart.

__c. Then refresh the web browser to see the content remains unchanged.

2.4.2

Summary

In this exercise you have learned how to:

• Configure a Liberty profile server.

• Add features to the server configuration to enable additional capabilities. • Configure a library that contains a JDBC driver for database access.

• Configure a DataSource that can be used by applications, or JPA to persist data.

• Have a feature automatically added to the configuration based on the Liberty Profile’s intelligence

about features being used in the Blog application.

This demonstrates how little configuration is required to access a database.

2.5

Securing an Application

In this next section of the lab, you are going to secure access to the blog application. The Liberty Profile maintains security in a default install by ensuring that it only listens to requests from the local host. In this exercise you learn how to configure the server to listen for https requests, configure a basic user registry, and protect access so only authorized users can post to the blog.

Related documents