...
Code Block |
---|
| JAVA |
---|
| JAVA |
---|
borderStyle | solid |
---|
title | sample.jpa.Test.java |
---|
|
package sample.jpa;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Test extends
javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
public Test() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
PrintWriter out = response.getWriter();
try {
java.util.Properties env =
new java.util.Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY
,"org.apache.openejb.client.RemoteInitialContextFactory");
env.put(Context.PROVIDER_URL,
"ejbd://127.0.0.1:4201");
Context ctx = new InitialContext(env);
out.println("Looking up ShareHolderBean");
ShareHolder shareHolder = (ShareHolder)
ctx.lookup("ShareHolderBeanRemote");
out.println("Creating ShareAccount 1,
Phani, 10");
ShareAccount sa =
shareHolder.openShareAccount(1,"phani", 10);
if(sa == null){
out.println("account could not be created.");
out.println("May be, account already
exists. Check the database.");
}
else {
out.println("Account is successfully created");
}
out.println("Looking up the ShareAccountNumber 1");
sa = shareHolder.findShareAccount(1);
out.println("Printing the details of ShareAccountNumber 1");
printShareAccount(sa,out);
out.println("");
out.println("buying shares 100");
sa = shareHolder.buyShares(1, 100);
out.println("Printing the details of ShareAccountNumber 1");
printShareAccount(sa,out);
out.println("");
out.println("selling 50 shares of ShareAccountNumber 1");
int numberShares = shareHolder.sellShares(1, 50);
if(numberShares == 50){
out.println("Printing the details of ShareAccountNumber 1");
sa = shareHolder.findShareAccount(1);
printShareAccount(sa,out);
}
else if(numberShares == -1){
out.println("ShareAccountNo can not be found");
}else {
out.println("The number shares available are less than 50");
}
out.println("");
List<ShareAccount> saList = shareHolder.listAccounts();
out.println("Printing all the available accounts");
for (int i = 0; i < saList.size(); i++){
out.println("*******");
printShareAccount(saList.get(i),out);
out.println("*******");
out.println("");
}
out.println("");
out.println("Setting the ShareAccount 1 with 500 shares
and updating the database");
sa = new ShareAccount(1,"phani",0);
sa.setNumberOfShares(500);
shareHolder.updateShareAccount(sa);
out.println("Printing the details of ShareAccountNumber 1");
printShareAccount(sa,out);
out.println("");
out.println("Closing ShareAccountNumber 1");
sa = shareHolder.close(1);
if(sa == null){
out.println("Account is not found="+1);
}else{
out.println("Printing the details of ShareAccountNumber 1");
printShareAccount(sa,out);
}
}
catch(Exception e){
e.printStackTrace();
throw new ServletException(e);
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
}
private void printShareAccount(ShareAccount sa, PrintWriter out){
if(sa != null){
out.println("Account Number = "+sa.getShareAccountNo());
out.println("Owner Name = "+sa.getOwnerName());
out.println("number of shares "+sa.getNumberOfShares());
out.println("version="+sa.getVersion());
}else{
out.println("ShareAccountNo can not be found");
}
}
}
|
Code Block |
---|
| XML |
---|
| XML |
---|
borderStyle | solid |
---|
title | web.xml |
---|
|
<!--
The deployment descriptor the web client.
-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ShareHolderWEB</display-name>
<servlet>
<description></description>
<display-name>Test</display-name>
<servlet-name>Test</servlet-name>
<servlet-class>sample.jpa.Test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
</web-app>
|
The Servlet client Test.java
, looks up the ShareHolderBean
and executes various life cycle methods on the ShareAccount
entity. When the url http://localhost:8080/ShareHolderWEB/Test is hit on a browser window, the following output is displayed.