48
Develop at least 5 components such as Order Processing, Payment Processing, etc., using .NET component technology. consoleapplication ------------------------------------ using System; using System.Collections.Generic; using System.Text; using customer; using computer; using com_det; using invoice; using orderclass; using bill; namespace ConsoleApplication { class Program { static void Main(string[] args) { string i; int h; Console.WriteLine("ONLINE SHOPPING"); Console.WriteLine("COMPUTER SALE"); Console.WriteLine("1.VIEW CATALOGUE"); Console.WriteLine("2.EXIT"); Console.WriteLine("ENTER YOUR CHOICE"); i = Console.ReadLine(); h = int.Parse(i); switch (h) { case 1: { com o2 = new com(); o2.display1(); co_det O3 = new co_det(); O3.detail3(); cus o1 = new cus(); o1.display();

New Lab Manual

Embed Size (px)

Citation preview

Page 1: New Lab Manual

Develop at least 5 components such as Order Processing, Payment

Processing,

etc., using .NET component technology.

consoleapplication

------------------------------------

using System;

using System.Collections.Generic;

using System.Text;

using customer;

using computer;

using com_det;

using invoice;

using orderclass;

using bill;

namespace ConsoleApplication

{

class Program

{

static void Main(string[] args)

{

string i;

int h;

Console.WriteLine("ONLINE SHOPPING");

Console.WriteLine("COMPUTER SALE");

Console.WriteLine("1.VIEW CATALOGUE");

Console.WriteLine("2.EXIT");

Console.WriteLine("ENTER YOUR CHOICE");

i = Console.ReadLine();

h = int.Parse(i);

switch (h)

{

case 1:

{

com o2 = new com();

o2.display1();

co_det O3 = new co_det();

O3.detail3();

cus o1 = new cus();

o1.display();

Page 2: New Lab Manual

Corder o4 = new Corder();

o4.display4();

inv o5=new inv();

o5.display5();

bll x1 = new bll();

x1.billdisp();

Console.ReadLine();

}

break;

case 2:

break;

}

}

}

}

components

------------------------

computer:

.....................

using System;

using System.Collections.Generic;

using System.Text;

namespace computer

{

public class com

{

int a;

public void display1()

{

Console.WriteLine("COMPUTERS");

Console.WriteLine("1.dell");

Console.WriteLine("2.hcl");

Console.WriteLine("3.lava");

Console.WriteLine("4.sony");

Console.WriteLine("5.samsung");

}

}

}

Page 3: New Lab Manual

computer details:

..............................

using System;

using System.Collections.Generic;

using System.Text;

namespace com_det

{

public class co_det

{

int d;

string w;

public void detail3()

{

Console.WriteLine("SELECTED YOUR DESIRED COMPUTER");

w=Console.ReadLine();

d=int.Parse(w);

switch (d)

{

case 1:

{

Console.WriteLine("YOU HAVE BEEN SELECTED DELL");

Console.WriteLine("ITEM ID:09");

Console.WriteLine("ITEM PRICE:34,000");

Console.WriteLine("MODEL:INSPIRON");

}

break;

case 2:

{

Console.WriteLine("YOU HAVE BEEN SELECTED HCL");

Console.WriteLine("ITEM ID:59");

Console.WriteLine("ITEM PRICE:24,000");

Console.WriteLine("MODEL:ASPIRE");

}

break;

case 3:

{

Console.WriteLine("YOU HAVE BEEN SELECTED LAVA");

Console.WriteLine("ITEM ID:05");

Console.WriteLine("ITEM PRICE:55,000");

Console.WriteLine("MODEL:LAVAW");

Page 4: New Lab Manual

}

break;

case 4:

{

Console.WriteLine("YOU HAVE BEEN SELECTED SONY");

Console.WriteLine("ITEM ID:75");

Console.WriteLine("ITEM PRICE:23,000");

Console.WriteLine("MODEL:DEVINE");

}

break;

case 5:

{

Console.WriteLine("YOU HAVE BEEN SELECTED SAMSUNG");

Console.WriteLine("ITEM ID:03");

Console.WriteLine("ITEM PRICE:65,000");

Console.WriteLine("MODEL:GALLERY");

}

break;

case 6:

break;

}

}

}

}

customer:

.................

using System;

using System.Collections.Generic;

using System.Text;

namespace customer

{

public class cus

{

string name;

string add;

string phno;

Page 5: New Lab Manual

string email;

public void display()

{

int q;

string v;

Console.WriteLine("if you are conformed with this product fill the customer form");

Console.WriteLine("1.yes/2.no");

v=Console.ReadLine();

q=int.Parse(v);

switch(q)

{

case 1:

{

Console.WriteLine("enter your name:");

name = Console.ReadLine();

Console.WriteLine("enter your address:");

add = Console.ReadLine();

Console.WriteLine("enter your phone number:");

phno = Console.ReadLine();

Console.WriteLine("enter your email id:");

email = Console.ReadLine();

Console.WriteLine("CHECK YOUR DETAILS");

Console.WriteLine("");

Console.WriteLine("NAME={0}", name);

Console.WriteLine("ADDRESS={0}", add);

Console.WriteLine("PHONE NUMBER={0}", phno);

Console.WriteLine("EMAIL ID={0}", email);

Console.WriteLine("");

Console.ReadLine();

}

break;

case 2:

break;

}

}

}

}

order:

................

using System;

Page 6: New Lab Manual

using System.Collections.Generic;

using System.Text;

namespace orderclass

{

public class Corder

{

int s;

string f;

public void display4()

{

Console.WriteLine("select payment method");

Console.WriteLine("1.CHEQUE/2.CASH");

f = Console.ReadLine();

s = int.Parse(f);

switch (s)

{

case 1:

{

Console.WriteLine("enter your account no:");

Console.ReadLine();

Console.WriteLine("name of bank:");

Console.ReadLine();

Console.WriteLine("your order conformed");

}

break;

case 2:

{

Console.WriteLine("your order conformed");

}

break;

case 3:

break;

}

}

}

}

invoice:

Page 7: New Lab Manual

.............

using System;

using System.Collections.Generic;

using System.Text;

namespace invoice

{

public class inv

{

public void display5()

{

Console.WriteLine("YOUR PRODUCT WILL BE DELIVERED ON 12.9.2011");

Console.ReadLine();

}}

}

Page 8: New Lab Manual

Develop at least 5 components such as Order Processing, Payment Processing,

etc., using EJB component technology.

Aim: To Create Components in EJB using EJB Componet Technology

Procedure:

Experiment No. 2(b)

List of Components:

1.Catlogue

Description:

The Catlogue componet is going to return the menu in string form .Which wil be directly printed in servlet page

2.Itemdb

Description:

This componet stores every product in a menu form with there prices.Also provide methods to traslate between product code and prices.

3.customername

Description:

This componet stores name and address.Also provide methods to traslate between name and address.

4.payment

Description:

Handles a simple paymenttype string which stores VISA/MasterCard

Page 9: New Lab Manual

CODING:

Component Name:

1.Catlogue

Description:

The Catlogue componet is going to return the menu in string form .Which wil be directly printed in servlet page

Code:

package allbeans;

import javax.ejb.Stateless;

@Stateless

public class Catlogue implements CatlogueRemote

{

public String Menu()

{ return "<center>"

+"<table bgcolor=lightblue cellpadding=2 border=1 cellspacing=3 >"

+ "<tr><th>Manufacturer</th><th>Model</th><th>Specification</th><th>Price</th></tr>"

+ "<tr><td>Dell</td><td>T2334</td><td>Core i5 4GB RAM 500GB SATAII </td><td>Rs 43,000</td></tr>"

+ "<tr><td>HP</td><td>E2334</td><td>Core i5 4GB RAM 500GB SATAII </td><td>Rs 43,000</td></tr>"

+ "<tr><td>SONY</td><td>K2334</td><td>Core i5 4GB RAM 500GB SATAII </td><td>Rs 43,000</td></tr>"

+ "</center>"

+ "</table>";

}}

Componetname:

Itemdb

Description:

This componet stores every product in a menu form with there prices.Also provide methods to traslate between product code and prices.

Page 10: New Lab Manual

Code:

package allbeans;

import javax.ejb.Stateless;

@Stateless

public class Itemdb implements ItemdbRemote {

public String[] productname=new String[3];

public int userprice;

public String userproduct;

public int price[]=new int[3];

public Itemdb()

{

productname[0]="dellT2334";price[0]=43000;

productname[1]="HPE2334";price[1]=42000;

productname[2]="SONYK2334";price[2]=41000;

}

public int finditem(String tproductcode)

{

userproduct=tproductcode;

int i=0;

while(i<3)

{

if( productname[i].equals(tproductcode))

{

userprice=price[i];

return price[i];

}

i++;

Page 11: New Lab Manual

}

return -1;

}

public int getprice()

{ return userprice;

}

public String getproduct()

{

return userproduct; }}

Componetname:

Customername

Description:

This componet stores name and address.Also provide methods to traslate between name and address.

Code:

package allbeans;

import javax.ejb.Stateless;

@Stateless

public class customername implements customernameRemote

{

public String name;

public String address;

public void setdetails(String tname, String taddress)

{name=tname;

address=taddress;

}

Page 12: New Lab Manual

public String getname() {

return name;

}

public String getaddress() {

return address;

}

}

ComponetName:

Payment

Description:

Handles a simple paymenttype string which stores VISA/MasterCard

Code:

package allbeans;

import javax.ejb.Stateless;

@Stateless

public class payment implements paymentRemote

{

String paymenttype;

public void setpaymentype(String type)

{ paymenttype=type;

}

public String getpaymenttype()

{

return paymenttype;

}

}

Page 13: New Lab Manual

ServletName:

Menu

Description:

Prints the Menu from catlogue component

Code:

public class Menu extends HttpServlet

{ @EJB

private CatlogueRemote catlogue;

@EJB

private customernameRemote customername;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String cname=request.getParameter("custname");

String caddress=request.getParameter("Address");

customername.setdetails(cname, caddress);

try {

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet Menu</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>" + catlogue.Menu() + "</h1>");

out.println("<form method=POST action=payment>");

out.println("<center><select name=choice ><option >dellT2334</option><option>HPE2334</option><option>SONYK2334</option></select></center>");

Page 14: New Lab Manual

out.println("<center><input type=submit value=Proceed ></center>");

out.println("</form>");

out.println("</html>");

} finally {

out.close();

}

}

ServletName:

Payment

Description:

Handles Payment component,

Sets the option VISA/Mastercard .

Code:

public class payment extends HttpServlet {

@EJB

private ItemdbRemote itemdb;

@EJB

private paymentRemote payment;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

String productid=request.getParameter("choice");

int price=itemdb.finditem(productid);

try {

out.println("<html>");

out.println("<head>");

Page 15: New Lab Manual

out.println("<title>Servlet payment</title>");

out.println("</head>");

out.println("<body>");

out.println("<form method=POST action=report>");

out.println("<center>");

out.println("<p > Select the payment type </p>");

out.println("<select name=ptype><option >Visa</option><option>MasterCard</option></select>");

out.println("<input type=submit value=proceed>");

out.println("</center>");

out.println("</body>");

out.println("</html>");

} finally {

out.close();

}

}

ServletName:

Report:

Description:

Generates reports .Make use of all componets and prints the member data types.

Code:

public class report extends HttpServlet {

@EJB

private paymentRemote payment;

@EJB

private customernameRemote customername;

@EJB

Page 16: New Lab Manual

private ItemdbRemote itemdb;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

String choice=request.getParameter("ptype");

payment.setpaymentype(choice);

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet report</title>");

out.println("</head>");

out.println("<body>");

out.println("<center>");

out.println("<br>");

out.println("<table border=1 >");

out.println("<tr>");

out.println("<td>");

out.println("Name :"+customername.getname());

out.println("<br>");

out.println("</td>");

out.println("</tr>");

out.println("<tr>");

out.println("<td>");

out.println("Address :"+customername.getaddress());

out.println("<br>");

out.println("</td>");

Page 17: New Lab Manual

out.println("</tr>");

out.println("<tr>");

out.println("<td>");

out.println("ProductChoice :"+itemdb.getproduct());

out.println("<br>");

out.println("</td>");

out.println("</tr>");

out.println("<tr>");

out.println("<td>");

out.println("Price :"+itemdb.getprice());

out.println("<br>");

out.println("</td>");

out.println("</tr>");

out.println("<tr>");

out.println("<td>");

out.println("PaymentType :"+payment.getpaymenttype());

out.println("</td>");

out.println("</tr>");

out.println("<br>");

out.println("</center>");

out.println("</body>");

out.println("</html>");

} finally {

out.close();

}

}

WebPage:

Page 18: New Lab Manual

Index.jsp

Description:

This is home page/welcome screen

Code:

<body>

<center>

<h1>Welcome</h1>

<form method="post" action="Menu" >

<p>Name : <input type="TEXT" name="custname"></p><br>

<p>Address : <input type="TEXT" name="Address" ></p><br>

<input type="SUBMIT" id="submit" value="proceed">

</form>

</center>

</body>

Page 19: New Lab Manual

Invoke .NET components as web services.

Steps:

1. Open Visual Studio .net editor

2. Click on File->New->Website -> Asp.net Webservice

3. Add web methods.

4. Build -> Build Solution.

5. Debug -> start without debugging.

Click service description-> copy the url.

Page 20: New Lab Manual

6. Open Visual Studio .net editor

7. Click on File->New->Website -> Asp.net Website

8. Design view -> design your page. (add a button where we to write code that invokes web

method)

9. Code view -> add this line -> localhost.service Mycalc = new localhost.service();

10. mycalc is just a reference name – userdefined.

11. To invoke the web methods use the reference name. Say if u have add as a web method u can

invoke add using [myclac.add()]

12. to make this work , initially in Solution Explorer -> right click on C:\...\website.. -> select add web

reference -> now a dialogue box opens.

Here paste http://localhost:49158/WebSite2/Service.asmx?WSDL ( this is actually the url copied )

Then click go -> then click Add reference.

Now -> build -> build solution and start without debugging.

Page 21: New Lab Manual

Invoke EJB components as web services

http://wiki.netbeans.org/CreatingEJB3UsingNetbeansAndGlassfish

Install NETBeans 6.5

Page 22: New Lab Manual
Page 23: New Lab Manual
Page 24: New Lab Manual
Page 25: New Lab Manual

Change Return null to “Hello EJB World” with in quotes

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package Stateless;

import javax.ejb.Stateless;

@Stateless

public class TestEJBBean implements TestEJBRemote {

public String getMessage() {

return "“Hello EJB World" ;

} // Add business logic below. (Right-click in editor and choose

// "Insert Code > Add Business Method" or "Web Service > Add Operation")

}

Page 26: New Lab Manual

Using the Web Module as EJB Client

We have already created the Web Module previously when the EAR has been created, so let’s

use it as EJB Client.

When the Web module is created, by default Netbeans already creates a file called index.jsp in

the Web Pages section. Open this file to add a call to the servlet. The code looks like below:

Now Test1-war

Index.jsp

Add code :

<a href="TestServlet">Click here to call the EJB component</a>

As you can see, we must create a servlet called TestServlet. it will call the EJB component when

the link from User Interface is clicked.

To create the servlet itself, right click on Web project (in my case Test-web), New -> Servlet.

Page 27: New Lab Manual

On the new screen, select the Class Name and the Package. On next screen, you can keep all

options and click on Finish button. The Servlet will be created and its code will appear on the

Main tab.

Page 28: New Lab Manual

Add these Header file

import java.io.*;

import javax.ejb.EJB;

import javax.servlet.*;

import javax.servlet.http.*;

import stateless.TestEJBRemote;

Page 29: New Lab Manual
Page 30: New Lab Manual

@EJB

private TestEJBRemote testEJBBean;

Change Process Request to goGet at there places

Page 31: New Lab Manual

remove this part of coding from comment line and change the code

from:

out.println("<h1>Servlet TestServlet at " + request.getContextPath () + "</h1>");

Page 32: New Lab Manual

To:

out.println(testEJBBean.getMessage());

now clean and build , then run

Page 33: New Lab Manual

O/P:

Page 34: New Lab Manual
Page 35: New Lab Manual

Develop a J2EE client to access a .NET web service.

Program 6

Object: Development of a J2EE Application for consuming the .NET web service.

Procedure:-

Step 1: open Microsoft visual studio 2008.

Step 2: click on

File->New->Project

Select project Types:visual C# projects

Templates: ASP.NET web service Application

Click ok.

Step 3: Right click Service1.asmx

Click on view code

Enable the methods to use by removing comments and adding the new

methods.

Step 4: Build solution

Debug->start without debugging.

Copy the wsdl link.

Step 5: web service client

Click on new -> new web service client

Paste the wsdl link

Give the name of the package

Page 36: New Lab Manual

Click finish

Step 6: browse index.jsp

Right click->select call web service

Select the operation and click ok

Write the required code

This creates the client

Step 7: Right click the index.jsp and run.

Web Service:-

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Xml.Linq;

namespace program7

{

/// <summary>

/// Summary description for Service1

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ToolboxItem(false)]

// To allow this Web Service to be called from script, using ASP.NET

AJAX, uncomment the following line.

// [System.Web.Script.Services.ScriptService]

public class Service1 : System.Web.Services.WebService

{

[WebMethod]

public string HelloWorld()

{

return "Hello World";

}

[WebMethod]

Page 37: New Lab Manual

public string mymailid(string firstname,string lastname)

{

String strjoin;

String[] inputstring=new String[2];

inputstring[0] = firstname;

inputstring[1] = lastname;

strjoin = string.Join("@", inputstring);

return strjoin;

}

}

}

Client Code:-

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-

8">

<title>JSP Page</title>

</head>

<body>

<h2>Hello World!</h2>

<%-- start web service invocation --%><hr/>

<%

Page 38: New Lab Manual

try {

wer.Service1 service = new wer.Service1();

wer.Service1Soap port = service.getService1Soap();

// TODO initialize WS operation arguments here

java.lang.String firstname = "kannan";

java.lang.String lastname = "gmail.com";

// TODO process result here

java.lang.String result = port.mymailid(firstname, lastname);

out.println("My Mail id is "+result);

} catch (Exception ex) {

// TODO handle custom exceptions here

}

%>

<%-- end web service invocation --%><hr/>

</body>

</html>

Page 39: New Lab Manual

EX-NO:8 DEVELOP A .NET CLIENT TO ACCESS A J2EE WEB

SERVICE

AIM: To Develop a .NET client to access a J2EE web service

PROCEDURE:

Create a Java Web Service (Java EE, JAX-WS)

1. Create a new Web Application

In the NetBeans 6.1 IDE, choose File –> New Project. In the New Project Dialog select the Web category, and choose

Web Application from the projects list. Then, Click Next.

* If the web category is not available in this dialog, it means that the NetBeans version you have installed isn’t the

Web and Java EE package.

In the Name and Location page, set the location where you want to create the web application, and provide a name

for the project. Click Next.

Page 40: New Lab Manual

In the Server and Settings page, leave the default settings (Java EE 5, Use GlassFish V2) and Click Finish.

This creates the initial web application and opens the project for editing.

Page 41: New Lab Manual

2. Create the Web Service

Add a new web service to the project. Right click the project node and choose New –> Web Service.

* Notice that the location of the Web Service option in the menu may change from this image and your IDE.

In the New Web Service dialog, provide the Web Service Name, and the Package. The name of the service will affect

the final URL for calling the service, and the package name will be the namespace of the service contract. Click Finish.

Page 42: New Lab Manual

The Web Service now appears in the project tree.

To implement the service, double click the service node in the project tree (in the figure above – CalculatorService).

This will open the Web Service in Design mode, that lets you graphically design your service.

Page 43: New Lab Manual

Change to Source View by clicking on the Source button in the upper toolbar, and this will open the

CalculatorService.Java file for editing. Here is a sample implementation of the service. Notice how Java Annotations

are similar to .Net Attributes, especially how similar they are to the Web Services attributes we know…

package org.bursteg.calculator; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class CalculatorService { @WebMethod public int Add(@WebParam(name="a") int a, @WebParam(name="b") int b) { return a + b; } }

Deploy the web service to the web application server. From the NetBeans IDE this is done by right clicking the

project node, and choosing Undeploy and Deploy.

Page 44: New Lab Manual

After the web application has been deployed, just to make sure the web service works as expected, you can right

click the web service node, and choose Test Web Service.

This will open the browser and navigate to a test page with the url of the service

(http://localhost:9232/Calculator/CalculatorServiceService) with a ?Tester suffix.

Page 45: New Lab Manual

Call the Java Web Service from a .Net Client

In Visual Studio 2008, create a new console application.

This creates a new solution with a single Console Application project in it.

Right click the project node and choose Add Service Reference.

In the Add Service Reference Dialog, paste the address of the service metadata endpoint (service address + ?wsdl

suffix: http://localhost:9232/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service

metadata and understand the service contract.

Page 46: New Lab Manual

Provide a namespace for the service reference, and click OK.

This will generate the client side proxy that lets you consume the service easily, and the required configuration

settings into the configuration file.

To call the service using the generated client side proxy, open Program.cs and use the following code:

Page 47: New Lab Manual

static void Main(string[] args) { CalculatorServiceClient proxy = new CalculatorServiceClient(); int result = proxy.Add(2, 3); Console.WriteLine("Calculator Service returned: " + result.ToString()); }

Run the program and see that the web service is being called and the result is correct.

Conclusion

Since Java EE Web Services (JAX-WS) are standard SOAP services, they are easily interoperable from a .Net client

application with only several clicks. Visual Studio generated a .Net client proxy that makes it very easy to connect

and call the service.

RESULT:

Thus an experiment to Develop a .NET client to access a J2EE web service has been successfully

executed.

Page 48: New Lab Manual