How to create a Webclient

Status
Not open for further replies.

Ryan Riot

Baseband Member
Messages
54
Location
U S A
This is my guide to creating a WebClient for you gamers out there that created a game, And want to have a WebClient on there site, So that your users can play straight from there desktop.
-------------------------------------------------------------------------------------------------------------


The Web client is contained in the JSP page j2eetutorial/examples/src/ejb/converter/index.jsp. A JSP page is a text-based document that contains static template data, which can be expressed in any text-based format such as HTML, WML, and XML; and JSP elements, which construct dynamic content.


The statements (in bold in the following code) for locating the home interface, creating an enterprise bean instance, and invoking a business method are nearly identical to those of the J2EE application client. The parameter of the lookup method is the only difference; the motivation for using a different name is discussed in Specifying the JNDI Names.

The classes needed by the client are declared with a JSP page directive (enclosed within the <%@ %> characters). Because locating the home interface and creating the enterprise bean are performed only once, this code appears in a JSP declaration (enclosed within the <%! %> characters) that contains the initialization method, jspInit, of the JSP page. The declaration is followed by standard HTML markup for creating a form with an input field. A scriptlet (enclosed within the <% %> characters) retrieves a parameter from the request and converts it to a BigDecimal object. Finally, JSP expressions (enclosed within <%= %> characters) invoke the enterprise bean's business methods and insert the result into the stream of data returned to the client.

CODE:
Code:
<%@ page import="Converter,ConverterHome,javax.ejb.*,
javax.naming.*, javax.rmi.PortableRemoteObject,
java.rmi.RemoteException" %>
<%!
   private Converter converter = null;
   public void jspInit() {
      try {
         InitialContext ic = new InitialContext();
         Object objRef = ic.lookup("
            java:comp/env/ejb/TheConverter");
         ConverterHome home =
         (ConverterHome)PortableRemoteObject.narrow(
         objRef, ConverterHome.class);
         converter = home.create();
      } catch (RemoteException ex) {
         ...
      } 
   }
   ...
%>
<html>
<head>
    <title>Converter</title>
</head>

<body bgcolor="white">
<h1><center>Converter</center></h1>
<hr>
<p>Enter an amount to convert:</p>
<form method="get">
<input type="text" name="amount" size="25">
<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset">
</form>
<%
   String amount = request.getParameter("amount");
   if ( amount != null && amount.length() > 0 ) {
      BigDecimal d = new BigDecimal (amount);
%>
   <p><%= amount %> dollars are  
      <%= converter.dollarToYen(d) %>  Yen.
   <p><%= amount %> Yen are 
      <%= converter.yenToEuro(d) %>  Euro.
<%
    }
%>
</body>
</html>

COMPILING.
The J2EE server automatically compiles Web clients that are JSP pages. If the Web client were a servlet, you would have to compile it.


Packaging the Web Client

To package a Web client, you run the New Web Component wizard of the deploytool utility. During this process the wizard performs the following tasks.

* Creates the Web application deployment descriptor
* Adds the component files to a WAR file
* Adds the WAR file to the application's ConverterApp.ear file

After the packaging process, you can view the deployment descriptor by selecting ToolsDescriptor Viewer.

To start the New Web Component wizard, select FileNewWeb Component. The wizard displays the following dialog boxes.

1. Introduction dialog box
1. Read the explanatory text for an overview of the wizard's features.
2. Click Next.
2. WAR File dialog box
1. Select Create New WAR File In Application.
2. In the combo box, select ConverterApp.
3. In the WAR Display Name field, enter ConverterWAR.
4. Click Edit.
5. In the tree under Available Files, locate the j2eetutorial/examples/build/ejb/converter directory.
6. Select index.jsp and click Add.
7. Click OK.
8. Click Next.
3. Choose Component Type dialog box
1. Select the JSP radio button.
2. Click Next.
4. Component General Properties dialog box
1. In the JSP Filename combo box, select index.jsp.
2. Click Finish.


Specifying the Web Client's Enterprise Bean Reference

When it invokes the lookup method, the Web client refers to the home of an enterprise bean:

Code:
Object objRef = ic.lookup("java:comp/env/ejb/TheConverter");

You specify this reference as follows:

1. In the tree, select ConverterWAR.
2. Select the EJB Refs tab.
3. Click Add.
4. In the Coded Name column, enter ejb/TheConverter.
5. In the Type column, select Session.
6. In the Interfaces column, select Remote.
7. In the Home Interface column, enter ConverterHome.
8. In the Local/Remote Interface column, enter Converter

INTRODUCTION



One of the joys of developing with .NET is, a significant amount of the ground work which we previously had to code ourselves is now part of the framework. In this article, I show methods for performing HTTP GETs in C# using the WebClient and the StreamReader. I'll use these methods in future articles.

First, let's introduce Stream and StreamReader, which can both be found in the System.IO namespace. The StreamReader implements a TextReader to read UTF-8 characters by default from a stream (the source), which makes it ideal for reading from a URI. Take note that StreamReader is different from Stream, which reads bytes.

For sending data to and from a URI, .NET provides the WebClient class which can be found in the System.Net namespace. Several methods are available to enable us to send and receive files and data, both synchronously and asynchronously. The method we are interested in here is OpenRead(URI), which returns data from the URI as a Stream.


Let's code

The basic code to read from our URI can be achieved in three lines. We create our WebClient instance, create a Stream from the WebClient, and then read into a StreamReader until the end of the file, like so:

Code:
using System.IO;
using System.Net;

String URI = "http://somesite.com/somepage.html";

WebClient webClient = new WebClient();
Stream stream = webClient.OpenRead(URI);
String request = reader.ReadToEnd();

After we have run over this code, the contents of somepage.html will be in the request string variable. This is all great, but we are presuming that the request here is faultless.. i.e., no exceptions are thrown. With exception handling being so easy in .NET, there's no excuse not to make benefit of it... although from experience, it seems not everyone is of the same opinion...

Let's wrap our Stream requests into a try-catch loop. We can catch a WebException to clearly identify what has gone wrong, and deal with it nicely.



Code:
try
{
    WebClient webClient = new WebClient();
    Stream stream = webClient.OpenRead(URI);
    String request = reader.ReadToEnd();
}
catch (WebException ex)
{
    if (ex.Response is HttpWebResponse)
    {
        switch (((HttpWebResponse)ex.Response).StatusCode)
        {
            case HttpStatusCode.NotFound:
                response = null;
                break;

            default:
                throw ex;
        }
    }
}


We can further optimize the code by the wrapping using(...) around the WebClient/Stream, but that's beyond the scope of this article.

If you have a URI which requires authentication, you can add a NetworkCredential to the WebClient reference before you call the OpenRead method, like so:

Code:
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(username, password);
Stream stream = webClient.OpenRead(URI);




If this is informational, And helped you at all? REP me, Please and thank you!
 
Status
Not open for further replies.
Back
Top Bottom