Académique Documents
Professionnel Documents
Culture Documents
Hands-On Lab
Lab Manual
Build a simple Login Portlet App with MySQL.
Page i
RECORD OF CHANGE
*A - Added M - Modified D Deleted
Effective Date
PIC
A*
Change Description
New Version
M, D
13-Feb-10
DoanNX.
Create.
1.0
14-Mar-10
DoanNX.
Modify.
1.1
Page
ii
Contents
Build a simple Login Portlet App with MySQL...............................................................................................i
LAB: BUILDING A SIMPLE LOGIN PORTLET APP WITH MYSQL...................................................................5
Lab Objectives...............................................................................................................................................5
Exercise 1 Create a simple portlet application SimpleLoginPortlet...............................................................6
1. Create a new Dynamic Web Project............................................................................................................6
1. In the next dialog, click Next button.............................................................................................................6
2. In this dialog, let modify the value of Context root: change from SimpleLoginPortlet to portletdriver/dt.
.......................................................................................................................................................................7
4. Create new portlet object: SimpleLoginPortlet.............................................................................................8
3. Input information for SimpleLoginPortlet portlet as following:.......................................................................8
6. Modify the JSPs..........................................................................................................................................9
Open the SimpleLoginPortlet\WebContent\WEB-INF\jsp\SimpleLoginPortlet_view.jsp file. You can use the
hot key Ctrl + Shift + R to open one file/resource quickly.................................................................................9
.......................................................................................................................................................................9
4. Refresh and then, run the project..............................................................................................................10
..........................................................................................................................................................................11
Exercise 2 Implement coding for SimpleLoginPortlet project......................................................................12
1. Create database query..............................................................................................................................12
In MySQL, create new database with name is simplelogin and run the script as following:...........................12
2. Import the necessary libraries into SimpleLoginPortlet project...................................................................12
There are two libraries needed to import, they are:.......................................................................................12
log4j-1.2.15.jar: this is used for log4j............................................................................................................12
mysql-connector-java-5.1.8-bin.jar: this is MySQL driver..............................................................................12
standard.jar: this is used for JSTL................................................................................................................12
jstl.jar: this is used for JSTL..........................................................................................................................12
Download these *.jar file and copy them to \WEB-INF\lib folder of SimpleLoginPortlet project, then refresh
project again.................................................................................................................................................12
....................................................................................................................................................................12
Add these libraries into the classpath of project............................................................................................13
Right click on project name, choose Build Path/Configure Build Path...........................................................13
....................................................................................................................................................................13
3. Create *.properties files............................................................................................................................14
Create db.properties file:.............................................................................................................................14
Right click on project name, choose New/File and input as following figure:.................................................14
....................................................................................................................................................................14
The content of db.properties file:.................................................................................................................14
....................................................................................................................................................................14
Create log4j.properties file:...........................................................................................................................14
Get the log4j.properties standard file from rai-server and copy it to the same location with db.properties file.
.....................................................................................................................................................................14
Search this line ...........................................................................................................................................14
....................................................................................................................................................................14
and changed to............................................................................................................................................14
....................................................................................................................................................................14
4. Create utility class(s).................................................................................................................................15
Create Connection class as following:..........................................................................................................15
Create a new class with name Constants to contain all project constants and the content of this class as
following (until now):......................................................................................................................................16
....................................................................................................................................................................16
And now the getConnection() method is changed as following:....................................................................17
....................................................................................................................................................................17
5. Create DTO class(s)..................................................................................................................................17
Create new class as following:.....................................................................................................................17
6. Create DAO class(s).................................................................................................................................19
Create new class as following:.....................................................................................................................19
Page
iii
Page
iv
Portlet .
3-layers model.
MySQL DBMS.
Using *.properties file in Java with Properties object and ResourceBundle object.
References:
Log4j: http://www.vipan.com/htdocs/log4jhelp.html
Goining to menu File > New > Dynamic Web Project . Input the values as following
figure.
2. In this dialog, let modify the value of Context root : change from SimpleLoginPortlet to
portletdriver/dt .
</b>
If no available server exists, you will add a new server to run project.
Then, deploy into uPortal, after you deploy it successfully in uPortal 1, browse to it:
How to publish one portlet into uPortal, please refer to HandOnLabs_Portlet_Create Hello Portlet App with
Eclipse_v2.0 lab.
In MySQL, create new database with name is simplelogin and run the script as
following:
Download these *.jar file and copy them to \WEB-INF\lib folder of SimpleLoginPortlet
project, then refresh project again.
Right click on project name, choose New/File and input as following figure:
Get the log4j.properties standard file from rai-server and copy it to the
same location with db.properties file.
and changed to
log4j.appender.R.File=SimpleLoginPortlet.log
ject.
}
// second way: using ResourceBundle class: fast and easy.
private static ResourceBundle dbResource = ResourceBundle.getBundle("db");
public static java.sql.Connection getConnection() {
// load the driver and establish a connection.
try {
// using resource bundle to get jdbcDriver
String jdbcDriver = dbResource.getString(Constants.JDBD_DRIVER);
String jdbcUrl = dbResource.getString(Constants.JDBD_URL);
// using properties object.
String jdbcUser = props.getProperty(Constants.JDBD_USER);
String jdbcPassword = props.getProperty(Constants.JDBD_PASS);
Class.forName(jdbcDriver);
return (java.sql.Connection) DriverManager.getConnection(jdbcUrl,
jdbcUser, jdbcPassword);
} catch (Exception ex) {
// in case occur exception, log it.
log.error("getConnection", ex);
}
return null;
}
Create a new class with name Constants to contain all project constants and the
content of this class as following (until now):
package rai.util;
/**
* Constants of project.
* @author joanie
*
*/
public class Constants {
public static String
public static String
public static String
public static String
}
JDBD_DRIVER = "jdbcDriver";
JDBD_URL = "jdbcUrl";
JDBD_USER = "jdbcUser";
JDBD_PASS = "jdbcPassword";
Class.forName(jdbcDriver);
return (java.sql.Connection) DriverManager.getConnection(jdbcUrl,
jdbcUser, jdbcPassword);
} catch (Exception ex) {
// in case occur exception, log it.
log.error("getConnection", ex);
}
return null;
java.sql.Connection;
java.sql.PreparedStatement;
java.sql.ResultSet;
java.sql.SQLException;
java.util.ArrayList;
java.util.List;
import org.apache.log4j.Logger;
import rai.dto.SimpleUserDTO;
import rai.util.Constants;
/**
* DAO class for SimpleLoginPortlet.
* @author joanie
*
*/
public class SimpleLoginDAO {
/**
* log property.
*/
private static Logger log = Logger.getLogger("SimpleLoginDAO");
/**
* Get the information of one specify user.
* @param name
* @param pass
* @return
*/
public List<SimpleUserDTO> getUser(String name, String pass) {
SimpleUserDTO dto;
List<SimpleUserDTO> result = new ArrayList<SimpleUserDTO>();
Connection conn = null;
String query = Constants.BLANK_VALUE;
ResultSet rs = null;
PreparedStatement pStat = null;
try {
// query command
query = "select * from simpleuser where sname=? and spass=?";
// get connection
conn = rai.util.Connection.getConnection();
// prepare statement
pStat = conn.prepareStatement(query);
pStat.setString(1, name);
pStat.setString(2, pass);
// execute query
rs = pStat.executeQuery();
// get result
while (rs.next()) {
dto = new SimpleUserDTO();
dto.setId(rs.getInt("id"));
dto.setSname(rs.getString("sname"));
dto.setSpass(rs.getString("spass"));
result.add(dto);
}
} catch (Exception sqlEx) {
log.error("SimpleLoginDAO.getUser " + sqlEx);
} finally {
try {
rs.close();
pStat.close();
conn.close();
} catch (SQLException sqlEx) {
log.error("SimpleLoginDAO.getUser " + sqlEx);
}
}
return result;
}
log.debug("isValidUser.end");
return res;
<tr>
</tr>
<tr>
</tr>
<tr>
</form>
</tr>
</table>
<td>Username</td>
<td><input type="text" name="txtName"
value='${sessionScope.name}' /></td>
<td>Pass</td>
<td><input type="text" name="txtPass" /></td>
<td colspan="2"><input type="button" value="submit"
onclick="submitForm('<portlet:namespace/>','loginForm')" /></td>
Create new JSP page named Welcome.jsp in jsp folder and the content as following:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<script type="text/javascript">
function getForm(namespace, name) {
return document.forms[namespace + name];
}
function submitForm(namespace, name) {
var frm = getForm(namespace, name);
frm.txtAction.value = "back";
frm.submit();
}
</script>
<portlet:defineObjects />
SimpleLoginPortlet<br>
<h2>Hello ${sessionScope.name}!</h2>
<br />
<form name="<portlet:namespace/>backForm" action="<portlet:actionURL/>"
method="POST">
<input type="hidden" name="txtAction" />
<input type="button" value="Back"
onclick="submitForm('<portlet:namespace/>', 'backForm')" />
</form>
Build entire project: right click on project name, choose Refresh , then choose
Project menu/Clean submenu .
Export to *.war file and deploy again into uPortal (you dont need to publish a new
channel, the changes in new *.war file will be updated automatically into the created
channel).
This lab will execute follow the second way, validate at server-side.
For validation at server-side, we can put the validation code at any place, but, for clearly
and exactly (about meaning), we will implement validation in bean/form class.
if (action.equals(Constants.LOGIN_ACTION)) {
log.debug("doView.action = login");
// validate the input data
List<String> errorLst = form.validate(request, response);
// in case there is no error
if (errorLst.isEmpty()) {
blo = new SimpleLoginBLO();
dispatcher = getPortletContext().getRequestDispatcher(
Constants.LOGIN_VIEW_PATH);
dispatcher.include(request, response);
}
} else {
// show error message in case username is blank
request.getPortletSession().setAttribute(
Constants.ERR_MESS_PORT_SESS_ATT, errorLst.get(0),
PortletSession.APPLICATION_SCOPE);
dispatcher = getPortletContext().getRequestDispatcher(
Constants.LOGIN_VIEW_PATH);
dispatcher.include(request, response);
}
} else if (action.equals(Constants.BACK_ACTION)) {