Deciding Where to Put the Presentation Data for Wireless Clients

Nel documento Oracle9 Application Server (pagine 97-102)

Supporting Wireless Clients

Screen 2 shows the sample application’s starting point, which is the empbft.xml file

7.3 Deciding Where to Put the Presentation Data for Wireless Clients

You can write the XML presentation data for wireless clients in the same JSP file as the one that generates the HTML, or in a different JSP file. Regardless of where you put the presentation data, you still need to determine if a request came from a wireless or desktop client.

7.3.1 Determining the Origin of a Request

You can determine the origin of a request by inserting a parameter in the request to identify wireless clients. You can include the parameter and its value using a hidden input form element.

The sample application uses a parameter name of clientType and parameter value of wireless to identify wireless clients. Each wireless client request contains this parameter. For example, in empbft.xml, which is the first file in the application that wireless clients see:

// empbft.xml

<?xml version = "1.0" encoding = "ISO-8859-1"?>

<SimpleResult>

<SimpleContainer>

<SimpleForm title="Query Employee" target="/empbft/controller">

<SimpleFormItem name="empID" format="*N">Enter Emp ID: </SimpleFormItem>

<SimpleFormItem name="action" type="hidden" value="queryEmployee" />

<SimpleFormItem name="clientType" type="hidden" value="wireless" />

</SimpleForm>

</SimpleContainer>

</SimpleResult>

You can then check for the clientType parameter in servlets or JSPs in the same way that you check for other parameters.

In servlets:

String client = req.getParameter(SessionHelper.CLIENT_TYPE_PARAMETER);

boolean wireless =

client != null && client.equals(SessionHelper.CLIENT_TYPE_WIRELESS);

In JSPs:

<%

String client = request.getParameter(SessionHelper.CLIENT_TYPE_PARAMETER);

boolean wireless =

client != null && client.equals(SessionHelper.CLIENT_TYPE_WIRELESS);

%>

7.3.2 Combining Presentation Data in the Same JSP File

If you use this method, determine the origin of the request (whether it came from a wireless or desktop client) in the JSP file itself. You can then generate HTML or XML depending on the origin. For example:

// import classes for both wireless and browsers

<%@ page import="java.util.*" %>

<%@ page import="empbft.component.employee.ejb.*" %>

<%@ page import="empbft.component.employee.helper.*" %>

<%@ page import="empbft.util.*" %>

// check the client type that sent the request

<%

String client = request.getParameter(SessionHelper.CLIENT_TYPE_PARAMETER);

boolean wireless = ( (client != null) &&

client.equals(SessionHelper.CLIENT_TYPE_WIRELESS) );

if (wireless) {

%>

<?xml version = "1.0" encoding = "ISO-8859-1"?>

<%@ page contentType="text/vnd.oracle.mobilexml; charset=ISO-8859-1" %>

<SimpleResult>

<SimpleContainer>

<SimpleForm title="Query Employee" target="/empbft/controller">

<SimpleFormItem name="empID" format="*N">Enter Emp ID:

</SimpleFormItem>

<SimpleFormItem name="action" type="hidden" value="queryEmployee" />

<SimpleFormItem name="clientType" type="hidden" value="wireless" />

</SimpleForm>

</SimpleContainer>

</SimpleResult>

<%

} else {

%>

<%@ page contentType="text/html;charset=ISO-8859-1"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<link rel="stylesheet" href="css/blaf.css" type="text/css">

<title>Query Employee</title>

</head>

<body>

Deciding Where to Put the Presentation Data for Wireless Clients

Supporting Wireless Clients 7-9 <h2>Employee Benefit Application</h2>

<%

String empId = request.getParameter(SessionHelper.EMP_ID_PARAMETER);

if (empId == null) {

%>

<h4>Query Employee</h4>

<form method=get action="/empbft/controller">

<input type=hidden name=action value=queryEmployee>

<table>

<tr>

<td>Employee ID:</td>

<td><input type=text name=empID size=4></td>

<td><input type=submit value="Query Employee"></td>

</tr>

int id = Integer.parseInt(empId);

EmployeeManager mgr = SessionHelper.getEmployeeManager(request);

EmployeeModel emp = mgr.getEmployeeDetails(id);

%>

<h4>Employee Details</h4>

<table>

<tr><td>Employee ID: </td><td colspan=3><b><%=id%></b></td></tr>

<tr><td>First Name: </td><td><b><%=emp.getFirstName()%></b></td><td>Last Name:

</td><td><b><%=emp.getLastName()%></b></td></tr>

<tr><td>Email: </td><td><b><%=emp.getEmail()%></b></td><td>Phone Number:

</td><td><b><%=emp.getPhoneNumber()%></b></td></tr>

Collection benefits = emp.getBenefits();

if (benefits == null || benefits.size() == 0) { %>

<tr><td>None</td></tr>

<%

} else {

Iterator it = benefits.iterator();

while (it.hasNext()) {

BenefitItem item = (BenefitItem)it.next();

%>

<tr><td><%=item.getName()%></td></tr>

<% benefits to the employee</a></td></tr>

<tr><td><a

href="/empbft/controller?empID=<%=id%>&amp;action=removeBenefitFromEmployee">Rem ove benefits from the employee</a></td></tr>

<tr><td><a href="/empbft/controller?action=queryEmployee">Query other employee</a></td></tr>

7.3.3 Separating Presentation Data into Separate Files

If you are using different files, edit the subclasses of ActionHandler to check the origin of the request, and forward the request to the proper JSP file. For example:

public void performAction(HttpServletRequest req, HttpServletResponse res) throws ServletException

{

String client = req.getParameter(SessionHelper.CLIENT_TYPE_PARAMETER);

boolean wireless =

client != null && client.equals(SessionHelper.CLIENT_TYPE_WIRELESS);

String empIdString = req.getParameter(SessionHelper.EMP_ID_PARAMETER);

Deciding Where to Put the Presentation Data for Wireless Clients

Supporting Wireless Clients 7-11 boolean validEmpId = true;

if (empIdString != null) {

int empId = Integer.parseInt(empIdString);

validEmpId = (empId >= 100 && empId <= 206) ? true : false;

}

// Forward to appropriate page if (wireless) {

if (validEmpId) {

forward(req, res, "/queryEmployeeWireless.jsp");

} else {

The value of CLIENT_TYPE_PARAMETER is defined in SessionHelper to be clientType. This is the name of the parameter.

The value of CLIENT_TYPE_WIRELESS is defined in SessionHelper to be wireless. This is the value of the parameter.

This parameter and the value of the parameter are defined in empbft.xml. This file corresponds to the ID page for wireless. It enables users to enter a number in a text input field.

// empbft.xml

<?xml version = "1.0" encoding = "ISO-8859-1"?>

<SimpleResult>

<SimpleContainer>

<SimpleForm title="Query Employee" target="/empbft/controller">

<SimpleFormItem name="empID" format="*N">Enter Emp ID: </SimpleFormItem>

<SimpleFormItem name="action" type="hidden" value="queryEmployee" />

<SimpleFormItem name="clientType" type="hidden" value="wireless" />

</SimpleForm>

</SimpleContainer>

</SimpleResult>

Nel documento Oracle9 Application Server (pagine 97-102)