1. Trang chủ
  2. » Công Nghệ Thông Tin

Ôn thực hành EJB

16 711 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 16
Dung lượng 210 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

On Thuc Thanh EJBCMB Create Enterprise Application chọn J2EE 1.4 Chú ý: - Cấu hình CMB: trước tiên delete tất cả kết nối đến database - URL: jdbc:sqlserver://localhost:1433;databaseName

Trang 1

On Thuc Thanh EJB

CMB

Create Enterprise Application (chọn J2EE 1.4)

Chú ý:

- Cấu hình CMB: trước tiên delete tất cả kết nối đến database

- URL: jdbc:sqlserver://localhost:1433;databaseName=DEAE

- File: JDBCSQL.RAR phải bỏ vào sun=>lib

- Start sun server

- Vào source packages add databaseName và đăng kí microsoft_sqlPool && .sun-resource lên sun server

- Khóa ngoại trong table thì cũng phải có khóa

- New Timestamp(2011-1900,5,6,0,0,0,0) nếu search theo ngày thì tháng -1

Cách sử dụng StringTokenizer để cắt chuổi

String myCd = req.getParameter("CD");

StringTokenizer t = new StringTokenizer(myCd, "|");

String album = t.nextToken();

String artist = t.nextToken();

String country = t.nextToken();

Cách select dữ liệu từ 3 bảng

SalesLocalHome slh = this.lookupSalesBean();

Collection clSales;

Iterator it;

SalesLocal sl;

ProductsLocal pl;

CustomersLocal cl;

try {

clSales = slh.findByAllSales();

it = clSales.iterator();

while (it.hasNext()) {

sl = (SalesLocal)it.next();

pl = (ProductsLocal)sl.getProductsBean();

cl = (CustomersLocal)sl.getCustomersBean();

}

} catch (FinderException ex) {

ex.printStackTrace();

}

Code Count Customer

CustDetailsLocalHome cus=this.lookupCustDetailsBean2();

try

{

CustDetailsLocal cuslocal;

Collection c=cus.findByAll();

Iterator iter=c.iterator();

Trang 2

int i=0;

while(iter.hasNext())

{

cuslocal=(CustDetailsLocal)iter.next();

i++;

}

out.println("Total customer: "+i);

}catch (Exception ex){}

out.close();

Code Login

CustDetailsLocalHome cus =this.lookupCustDetailsBean1(); String id=request.getParameter("id");

String name=request.getParameter("name");

try

{

CustDetailsLocal cuslocal;

Collection c=cus.findByLogin(id,name);

Iterator iter=c.iterator();

out.println(c.size());

if(c.size()>0)

{

cuslocal=(CustDetailsLocal)iter.next();

String cusid= cuslocal.getCustID();

String cusname=cuslocal.getName();

if(cusid.equals(id)&&cusname.equals(name))

{

response.sendRedirect("loginsuccess.jsp");

}

else

{

response.sendRedirect("error.jsp");

}

}

else

{

response.sendRedirect("error.jsp");

}

}catch(Exception ex){}

out.close();

Cách Bất JavaScript form nút button onClick=”ruturn Dn_()”

Trang 3

Create class khai báo thuộc tính

public class CD {

private String CUS;

private String IDCD;

private Double price;

public String getCUS() {

return CUS;

}

public void setCUS(String CUS) { this.CUS = CUS;

}

public String getIDCD() {

return IDCD;

}

public void setIDCD(String IDCD) { this.IDCD = IDCD;

}

public Double getPrice() {

return price;

}

public void setPrice(Double price) { this.price = price;

}

}

Page Order

Trang 4

<form action="EventOrder" method="get">

<%

String id=request.getParameter("ID");

if(id!=null)

{

%>

<input type="hidden" name="action" value="ADD"> Code CD: <label><%=id%></label><br>

<input type="hidden" value="<%=id%>" name="idDV" /> <%

}

%>

Price: <input type="text" name="price"/><br>

<input type="submit" value="Order" />

</form>

Code add sản phẩm vào cart

protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

HttpSession session = req.getSession(false);

if (session == null) {

res.sendRedirect("error.html");

}

Vector buylist = (Vector) session.getAttribute("shoppingcart"); String action = req.getParameter("action");

if (action.equals("ADD")) {

boolean match = false;

CD aCD = getCD(req);

if (buylist == null) {

buylist = new Vector();

buylist.addElement(aCD);

} else {

buylist.addElement(aCD);

}

session.setAttribute("shoppingcart", buylist);

String url=res.encodeRedirectURL("Cart.jsp");

res.sendRedirect(url);

}

}

private CD getCD(HttpServletRequest req) {

String iddv = req.getParameter("idDV");

String price = req.getParameter("price");

Trang 5

CD cd = new CD();

cd.setIDCD(iddv);

cd.setPrice((new Double(price)));

return cd;

}

Display cart

<%@ page session="true" import="java.util.*, mypackage.*" %>

<%

Vector buylist = (Vector) session.getAttribute("shoppingcart");

if (buylist != null && (buylist.size() >0)) {

%>

<center><h2>Cart Of You</h2>

<form action="index.jsp" method="POST">

<input type="submit" value="Back List CD">

</form>

<table border="1" cellpadding="0" width="50%" bgcolor="#FFFFFF">

<tr>

<td><b>Identity</b></td>

<td><b>idDV</b></td>

<td><b>PRICE</b></td>

<td><b>Remove</b></td>

</tr>

<%

for (int index=0; index< buylist.size();index++) {

CD anOrder = (CD) buylist.elementAt(index);

%>

<tr>

<td><b><%= index%></b></td>

<td><b><%= anOrder.getIDCD() %></b></td>

<td><b><%= anOrder.getPrice() %></b></td>

<td>

<form name="deleteForm" action="ShoppingServlet" method="POST">

<input type="submit" value="Remove">

<input type="hidden" name= "delindex" value="<%= index %>">

<input type="hidden" name="action" value="DELETE">

</form>

</td>

Trang 6

<% }%>

</table>

<form action="payment.jsp" method="POST">

<input type="hidden" name="erro" value="null">

<input type="submit" value="payment">

</form>

</center>

<% }else

{

%>

<h2 align="center">No items in cart</h2>

<h4 align="center"><a href="DisplayListCD">Back List CD</a></h4>

<%

}

%>

Page Payment

<%@ page session="true" import="java.util.*, mypackage.*" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title>

</head>

<body>

<%

String erro=request.getParameter("erro");

if(erro!=null)

{

out.println("<h4>"+erro+"</h4>");

}

%>

<h1>Bill</h1>

<%

Vector buylist = (Vector) session.getAttribute("shoppingcart");

if (buylist != null && (buylist.size() >0)) {

%>

<%

Trang 7

double total=0;

for (int index=0; index< buylist.size();index++) {

CD anOrder = (CD) buylist.elementAt(index);

total=total+anOrder.getPrice();

%>

Code CD : <label><%=anOrder.getIDCD()%></label>

Price : <label><%=anOrder.getPrice()%></label><br>

<% }%>

Total Money: <label><%=total %></label>

<% }%>

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

Input Code Customer:<input type="text" name="makh" />

<input type="submit" value="Confirm" />

</form>

</body>

</html>

Add sản phẩm vào bảng booking in database(code sử lý)

BookingDetailsLocalHome bill=this.lookupBookingDetailsBean();

try{

HttpSession session = request.getSession(false);

if (session == null) {

response.sendRedirect("error.html");

}

Vector buylist = (Vector) session.getAttribute("shoppingcart");

if (buylist != null && (buylist.size() >0))

{

String kh=request.getParameter("makh");

for (int index=0; index< buylist.size();index++)

{

// Random rand=new Random();

CD anOrder = (CD) buylist.elementAt(index);

String iddv=anOrder.getIDCD();

Double price=anOrder.getPrice();

bill.create((i+1),kh,price,new Timestamp(2011-1900,5-1,6,0,0,0,0)); }

}

System.out.println("ok!");

}catch (Exception ex){}

out.close();

BMP

Trang 8

Create driver =>chuột phải vào database chọn new connection =>tạo kết nôi đến database giống CMP

Trang 9

JMS Resources => Connection Factories=>new

Destination resources= new

Trang 10

QUEUE >add gói thư viện imqjmx.jar

SendQueueMessage

import javax.jms.*;

public class SendQueueMessage {

public static void main(String args[]) {

try {

//declare all the local variables QueueConnectionFactory queueConnectionFactory = null;

Queue queue = null;

QueueConnection queueConnection = null;

QueueSession queueSession = null;

QueueSender queueSender = null;

TextMessage message = null;

//lookup for connectionfactory queueConnectionFactory = new com.sun.messaging.QueueConnectionFactory();

//lookup for the destination queue queue = new com.sun.messaging.Queue("sampleQueue");

//create a connection queueConnection = queueConnectionFactory.createQueueConnection();

//create a session queueSession = queueConnection.createQueueSession(false,

Session.AUTO_ACKNOWLEDGE);

//create a sender queueSender = queueSession.createSender(queue);

//create a message message = queueSession.createTextMessage();

message.setText("This is a test message!");

//send the message queueSender.send(message);

//close the connection queueConnection.close();

Trang 11

System.out.println("Message has been sent to the MQ");

} catch(JMSException e) {

System.out.println(e);

} }

}

ReceiveQueueMessage

import javax.jms.*;

public class ReceiveQueueMessage {

public static void main(String args[]) {

try {

//declare all the local variables QueueConnectionFactory queueConnectionFactory = null;

Queue queue = null;

QueueConnection queueConnection = null;

QueueSession queueSession = null;

QueueReceiver queueReceiver = null;

TextMessage message = null;

//lookup for connectionfactory queueConnectionFactory = new com.sun.messaging.QueueConnectionFactory();

//lookup for the destination queue queue = new com.sun.messaging.Queue("sampleQueue");

//create a connection queueConnection = queueConnectionFactory.createQueueConnection();

//create a session queueSession = queueConnection.createQueueSession(false,

Session.AUTO_ACKNOWLEDGE);

//create a receiver queueReceiver = queueSession.createReceiver(queue);

//start the connection queueConnection.start();

//wait for a message message = (TextMessage) queueReceiver.receive();

System.out.println("Received message : "+ message.getText());

//close the connection queueConnection.close();

} catch(JMSException e) {

System.out.println(e);

} }

}

ReceiveQueueMessage2

import java.io.*;

Trang 12

import javax.jms.*;

public class ReceiveQueueMessage2 {

public static void main(String args[]) {

try {

//declare all the local variables

QueueConnectionFactory queueConnectionFactory = null;

Queue queue = null;

QueueConnection queueConnection = null;

QueueSession queueSession = null;

QueueReceiver queueReceiver = null;

TextMessage message = null;

TextListener listener = null;

//lookup for connectionfactory queueConnectionFactory = new com.sun.messaging.QueueConnectionFactory();

//lookup for the destination queue queue = new com.sun.messaging.Queue("sampleQueue");

//create a connection queueConnection = queueConnectionFactory.createQueueConnection();

//create a session queueSession = queueConnection.createQueueSession(false,

Session.AUTO_ACKNOWLEDGE);

//create a receiver queueReceiver = queueSession.createReceiver(queue);

//register the listener

listener = new TextListener();

queueReceiver.setMessageListener(listener);

queueConnection.start();

//wait for user input to terminate

InputStreamReader console = new InputStreamReader(System.in);

char ans = (char)console.read();

if(ans == 'q' || ans == 'Q')

queueConnection.close();

}

catch(JMSException e) {

System.out.println(e);

}

catch(IOException e) {

System.out.println(e);

}

}

}

class TextListener implements MessageListener {

public void onMessage(Message msg) {

Trang 13

TextMessage message = (TextMessage) msg;

System.out.println("Message Received : "+ message.getText());

} catch(JMSException j){

System.out.println("Exception:"+j.toString());

} }

}

Topic

PublishTopicMessage

import javax.jms.*;

public class PublishTopicMessage {

public static void main(String[] args) {

try {

//declare all local variables TopicConnectionFactory topicConnectionFactory = null;

Topic topic = null;

TopicConnection topicConnection = null;

TopicSession topicSession = null;

TopicPublisher topicPublisher = null;

TextMessage message = null;

//lookup for connectionfactory topicConnectionFactory = new com.sun.messaging.TopicConnectionFactory();

//lookup for the destination queue topic = new com.sun.messaging.Topic("sampleTopic");

//create a connection topicConnection = topicConnectionFactory.createTopicConnection();

//create a session topicSession = topicConnection.createTopicSession(false,

Session.AUTO_ACKNOWLEDGE);

//create a publisher topicPublisher = topicSession.createPublisher(topic);

//create a message message = topicSession.createTextMessage();

message.setText("This is a test message!");

//publish the message topicPublisher.publish(message);

//close the connection topicConnection.close();

} catch (JMSException e) {

System.out.println(e);

} }

}

Trang 14

import javax.jms.*;

public class SubscribeTopicMessage {

public static void main(String[] args) {

try {

//declare all local variables TopicConnectionFactory topicConnectionFactory = null;

Topic topic = null;

TopicConnection topicConnection = null;

TopicSession topicSession = null;

TopicSubscriber topicSubscriber = null;

TextListener listener = null;

//lookup for connectionfactory topicConnectionFactory = new com.sun.messaging.TopicConnectionFactory();

//lookup for the destination queue topic = new com.sun.messaging.Topic("sampleTopic");

//create a connection topicConnection = topicConnectionFactory.createTopicConnection(); //create a session

topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

//create a subscriber topicSubscriber = topicSession.createSubscriber(topic);

//create a listener listener = new TextListener();

//register the listener topicSubscriber.setMessageListener(listener);

topicConnection.start();

//wait for user input to terminate InputStreamReader console = new InputStreamReader(System.in); char ans = (char)console.read();

if(ans == 'q' || ans == 'Q')

topicConnection.close();

} catch(JMSException e) {

System.out.println(e);

} catch(IOException e) {

System.out.println(e);

} }

}

class TextListener implements MessageListener {

public void onMessage(Message msg) {

Ngày đăng: 29/12/2016, 09:51

TỪ KHÓA LIÊN QUAN

w