Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Staless Session Bean – EJB2, ví dụ 2 – Xác nhận đăng nhập

DÙNG STATELESS SESSION BEAN ĐỂ XÁC NHẬN ĐĂNG NHẬP

I. Mô hình làm việc của stateless session bean:

  1. Đầu tiên, EJB container đăng ký tất cả các enterprise beans đã triển khai, bao gồm stateless session beans, với dịch vụ JNDI dựa trên tên đăng ký JNDI được chỉ định bởi tập tin đặc tả triển khai (deployment descriptor).

2. EJB Container quyết định tạo 1 thể hiện của stateless session bean theo chính sách caching của hệ thống. Trong ví dụ này, EJB container khởi tạo SignOn bean bắng cách sử dụng Class.newInstance("SignOnEJB.class") rồi gọi phương thức setSessionContext() ejbCreate()trên thể hiện này. Bây giờ bean đã sẵn sàng phục vụ cho bất kỳ client nào.

  1. Client tìm kiếm home interface của enterprise bean đã được triển khai thông qua JNDI. Ví dụ, remote home interface của SignOn stateless bean có thể được xác định bằng đoạn code sau:
Context initialContext = new InitialContext();
Object obj = initialContext.lookup("hamy/SignOn");
SignOnHome signOnHome = (SignOnHome)
javax.rmi.PortableRemoteObject.narrow(obj, SignOnHome.class);

4. Client sử dụng remote home interface để tạo ra đối tượng remote sign-on session. Ví dụ:

SignOn signOn =  (SignOn)signOnHome.create();

5. Client gọi business method trên remote object. Ví dụ, client sẽ xác nhận tên đăng nhập và mật khẩu trong ví dụ của chúng ta:

signOn.validateUser("student1", "password1");

Ejb container sẽ gán 1 stateless session bean từ thể hiện dùng chung(pool) cho việc phục vụ yêu cầu của client. EJB container gọi phương thức đã được implements cho dịch vụ trên thể hiện của đối tượng stateless session. Ví dụ EJB container sẽ gọi phương thức validateUser(). Sau khi bean phục vụ cho yêu cầu từ client, EJB container sẽ đưa session bean trở lại vào các thể hiện dùng chung.

  1. EJB container quyết định kết thúc thể hiện của session bean bằng cách gọi phương thức của thể hiện bean.

II. Thiết kế:

III. Hiện thực Stateless Session Bean

Tạo file hamy/SignOn.java với nội dung

package hamy;

import java.util.*;

import java.rmi.*;

import javax.ejb.*;

public interface SignOn extends EJBObject {

public boolean validateUser(String login, String password)

throws InvalidLoginException, RemoteException;

}

Tạo file với hamy/SignOnHome.java nội dung

package hamy;

import java.rmi.RemoteException;

import javax.ejb.*;

public interface SignOnHome extends EJBHome {

SignOn create() throws CreateException, RemoteException;

}

Tạo file với hamy/SignOnEJB.java với nội dung:

package hamy;

import java.util.*;

import javax.ejb.*;

import javax.naming.*;

public class SignOnEJB implements SessionBean {

private SessionContext ctx;

private Context environment;

public SignOnEJB() {

print(“The container created this instance.\n”);

}

/* — Callback methods — */

public void setSessionContext(SessionContext c) {

print(“The container called the setSessionContext method “);

print(“to associate session bean instance with its context.\n”);

ctx = c;

}

public void ejbCreate() throws CreateException {

print(“The container called the ejbCreate method\n”);

print(“so that we can initialize the bean instance.\n”);

try {

InitialContext ic = new InitialContext();

environment = (Context) ic.lookup(“java:comp/env”);

} catch (NamingException ne) {

throw new CreateException(“Could not look up context”);

}

}

/* Methods ejbActivate and ejbPassivate are

not used by stateless session beans

*/

public void ejbActivate() {}

public void ejbPassivate() {}

public void ejbRemove() {

print(“This instance is in the process of being removed “);

print(“by the container.\n”);

}

/* —Here you implement all business methods

as defined in the component interface—

*/

public boolean validateUser(String userName, String password)

throws InvalidLoginException {

try {

String storedPassword = (String) environment.lookup(userName);

if ( storedPassword.equals(password) ) {

return true;

}

else {

throw new InvalidLoginException(“Invalid login/password”);

}

} catch(NamingException ne) {

throw new InvalidLoginException(“Invalid login/password”);

}

}

void print(String s) {

System.out.println(s);

}

}

Tạo file hamy/InvalidLoginException.java với nội dung:

package hamy;

public class InvalidLoginException extends Exception {

public InvalidLoginException() {

super();

}

public InvalidLoginException(Exception e) {

super(e.toString());

}

public InvalidLoginException(String s) {

super(s);

}

}

Tạo file META-INF/ejb-jar.xml với nội dung

<?xml version=”1.0″?>

<ejb-jar version=”2.1″ xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd”&gt;

<enterprise-beans>

<session>

<ejb-name>SignOnEJB</ejb-name>

<home>hamy.SignOnHome</home>

<remote>hamy.SignOn</remote>

<ejb-class>hamy.SignOnEJB</ejb-class>

<session-type>Stateless</session-type>

<transaction-type>Container</transaction-type>

<env-entry>

<env-entry-name>student</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>password</env-entry-value>

</env-entry>

<env-entry>

<env-entry-name>student1</env-entry-name>

<env-entry-type>java.lang.String</env-entry-type>

<env-entry-value>password1</env-entry-value>

</env-entry>

</session>

</enterprise-beans>

</ejb-jar>

Tạo file jboss.xml có nội dung sau:

<?xml version=”1.0″ encoding=”UTF-8″?>

<jboss>

<enterprise-beans>

<session>

<ejb-name>SignOnEJB</ejb-name>

<jndi-name>hamy/SignOn</jndi-name>

</session>

</enterprise-beans>

</jboss>

IV. Triển khai

Tạo file compile.bat có nội dung sau để biên dịch các file java:

javac *.java

pause

Thực thi file này. Sau đó ta có thư mục hamy gồm có ngoài các file .java còn có thêm các file .class.

Tạo file CreateJarFile.bat có nội dung:

jar cvf SignOn.jar hamy/*.class META-INF/*.xml

pause

Chạy file này ta sẽ có được file SignOn.jar. Mở file này với winrar ta có cấu trúc như sau

hamy

InvalidLoginException.class

SignOn.class

SignOnEJB.class

SignOnHome.class

META-INF

ejb-jar.xml

jboss.xml

MANIFEST.MF

Copy tập tin SignOn.jar vào thư mục JBOSS-HOME\server\default\deploy\

Nếu Jboss đang chạy nó sẽ tự động triển khai.

Nếu chưa chạy Jboss thì vào thư mục JBOSS-HOME\bin, chạy file run.bat.

Kết quả trong console jboss:

14:31:42,149 INFO [EjbModule] Deploying SignOnEJB

14:31:42,180 INFO [ProxyFactory] Bound EJB Home ‘SignOnEJB’ to jndi ‘hamy/SignOn’

14:31:42,196 INFO [EJBDeployer] Deployed: file:/C:/javaSoft/jboss-4.2.2.GA/server/default/deploy/SignOn.jar

Tạo client

Tạo thư mục client

Tạo file Client.java với nội dung

import java.util.Properties;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import hamy.*;

public class Client extends JFrame implements ActionListener

{

private JButton btLogon,btExit;

private JTextField tfUserName;

private JPasswordField pfPassword;

public Client(){

super(“Logon with EJB”);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(350,150);

this.setResizable(false);

this.setLocation(300,200);

JLabel l1,l2,l3;

Box b=Box.createVerticalBox();

Box b1=Box.createHorizontalBox();

Box b2=Box.createHorizontalBox();

Box b3=Box.createHorizontalBox();

b.add(Box.createHorizontalStrut(27));

b.add(b1);b.add(Box.createVerticalStrut(7));

b.add(b2);b.add(Box.createVerticalStrut(10));

b.add(b3);b.add(Box.createVerticalStrut(7));

b1.add(l1=new JLabel(“User Name:”,JLabel.RIGHT));b1.add(tfUserName=new JTextField(15));

b2.add(l2=new JLabel(“Password:”,JLabel.RIGHT));b2.add(pfPassword=new JPasswordField(15));

b3.add(btLogon=new JButton(“Logon”));b3.add(Box.createHorizontalStrut(7));

b3.add(btExit=new JButton(“Exit”));

btExit.addActionListener(this);

btLogon.addActionListener(this);

l2.setPreferredSize(l1.getPreferredSize());

this.add(l3=new JLabel(“LOGON”,JLabel.CENTER),BorderLayout.NORTH);

l3.setFont(new Font(“Arial”,Font.BOLD,18));

l3.setForeground(java.awt.Color.red);

this.add(b,BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e){

Object o=e.getSource();

if(o.equals(btExit))

System.exit(1);

else{

try {

Properties props = System.getProperties();

props.put( Context.INITIAL_CONTEXT_FACTORY,”org.jnp.interfaces.NamingContextFactory” );

props.put( Context.PROVIDER_URL, “localhost:1099” );

props.put( Context.URL_PKG_PREFIXES, “org.jboss.naming” );

Context initialContext = new InitialContext();

Object obj = initialContext.lookup(“hamy/SignOn”);

SignOnHome signOnHome = (SignOnHome)

javax.rmi.PortableRemoteObject.narrow(obj, SignOnHome.class);

SignOn signOn = (SignOn)signOnHome.create();

boolean isOK=signOn.validateUser(tfUserName.getText(),pfPassword.getText());

if(isOK){

JOptionPane.showMessageDialog(null,”Logon OK”);

System.exit(1);

}

}catch(InvalidLoginException ile){

JOptionPane.showMessageDialog(null,”Invalid username or password”);

}

catch(Exception ex){

ex.printStackTrace();

}

}

}

public static void main(String[]agrs)

{

new Client().setVisible(true);

/*

*/

}

}

Tạo thư mục client\hamy, copy các file .class sau vào đó:

InvalidLoginException.class

SignOn.class

SignOnHome.class

Tạo file client/compile.bat với nội dung sau để biên dịch các tập tin java

javac *.java

pause

Tạo file clinet/client.bat để thực thi ứng dụng với nội dung sau :

java Client

pause

Chạy file compile.bat.

Chạy file client.bat để chạy client. Kết quả như hình sau

7 Responses to “Staless Session Bean – EJB2, ví dụ 2 – Xác nhận đăng nhập”

  1. Xin chào .
    Tình cờ biết được trang web này . Thật có nhiều điều lý thú quá .
    Tại sao mình chạy phần clien trên máy khác lại không được .
    Mình đã thay đổi phần : localhost:1099 thành Ipaddress của máy mình :1099 . Mà tại sao vẫn không chạy được .
    Mong bạn giúp đỡ

  2. maiami said

    Tạo file compile.bat có nội dung sau để biên dịch các file java:

    javac *.java

    pause
    Thực thi file này. Sau đó ta có thư mục hamy gồm có ngoài các file .java còn có thêm các file .class.
    cả file này nữa thầy a:CreateJarFile.bat
    Thây fgiáo cho em hỏi là file compile.bat đặt ở đâu trong project hamy a.Em có đặt vào dist nhưng không được nó báo khôgnt ồn tại file .java thì phải :[IMG]http://ca7.upanh.com/16.80.20402248.8Qm0/7.jpg[/IMG]
    Thầy giáo có thể giúp em được không ạ.

  3. maiami said

    Tại sao em chạy project thì vẫn chạy đượcnhưng chạy file compile.bat thì nó báo lỗi thế này hả thầy:

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy
    SignOnBean.java:2: package javax.ejb does not exist
    import javax.ejb.SessionBean;
    ^
    SignOnBean.java:3: package javax.ejb does not exist
    import javax.ejb.SessionContext;
    ^
    SignOnBean.java:5: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnBean.java:7: cannot find symbol
    symbol: class SessionBean
    public class SignOnBean implements SessionBean {
    ^
    SignOnBean.java:8: cannot find symbol
    symbol : class SessionContext
    location: class hamy.SignOnBean
    private SessionContext ctx;
    ^
    SignOnBean.java:13: cannot find symbol
    symbol : class SessionContext
    location: class hamy.SignOnBean
    public void setSessionContext(SessionContext c) {
    ^
    SignOnBean.java:18: cannot find symbol
    symbol : class CreateException
    location: class hamy.SignOnBean
    public void ejbCreate() throws CreateException {
    ^
    SignOnRemote.java:4: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnRemote.java:5: package javax.ejb does not exist
    import javax.ejb.EJBObject;
    ^
    SignOnRemote.java:6: cannot find symbol
    symbol: class EJBObject
    public interface SignOnRemote extends EJBObject {
    ^
    SignOnRemoteHome.java:2: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnRemoteHome.java:4: package javax.ejb does not exist
    import javax.ejb.CreateException;
    ^
    SignOnRemoteHome.java:5: package javax.ejb does not exist
    import javax.ejb.EJBHome;
    ^
    SignOnRemoteHome.java:6: cannot find symbol
    symbol: class EJBHome
    public interface SignOnRemoteHome extends EJBHome {
    ^
    SignOnRemoteHome.java:7: cannot find symbol
    symbol : class CreateException
    location: interface hamy.SignOnRemoteHome
    hamy.SignOnRemote create() throws CreateException, RemoteException;
    ^
    SignOnBean.java:25: cannot find symbol
    symbol : class CreateException
    location: class hamy.SignOnBean
    throw new CreateException(“Could not look up context”);
    ^
    16 errors
    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy
    SignOnBean.java:2: package javax.ejb does not exist
    import javax.ejb.SessionBean;
    ^
    SignOnBean.java:3: package javax.ejb does not exist
    import javax.ejb.SessionContext;
    ^
    SignOnBean.java:5: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnBean.java:7: cannot find symbol
    symbol: class SessionBean
    public class SignOnBean implements SessionBean {
    ^
    SignOnBean.java:8: cannot find symbol
    symbol : class SessionContext
    location: class hamy.SignOnBean
    private SessionContext ctx;
    ^
    SignOnBean.java:13: cannot find symbol
    symbol : class SessionContext
    location: class hamy.SignOnBean
    public void setSessionContext(SessionContext c) {
    ^

    SignOnBean.java:18: cannot find symbol
    symbol : class CreateException
    location: class hamy.SignOnBean
    public void ejbCreate() throws CreateException {
    ^
    SignOnRemote.java:4: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnRemote.java:5: package javax.ejb does not exist
    import javax.ejb.EJBObject;
    ^
    SignOnRemote.java:6: cannot find symbol
    symbol: class EJBObject
    public interface SignOnRemote extends EJBObject {
    ^
    SignOnRemoteHome.java:2: package javax.ejb does not exist
    import javax.ejb.*;
    ^
    SignOnRemoteHome.java:4: package javax.ejb does not exist
    import javax.ejb.CreateException;

    SignOnRemoteHome.java:5: package javax.ejb does not exist
    import javax.ejb.EJBHome;
    ^
    SignOnRemoteHome.java:6: cannot find symbol
    symbol: class EJBHome
    public interface SignOnRemoteHome extends EJBHome {
    ^
    SignOnRemoteHome.java:7: cannot find symbol
    symbol : class CreateException
    location: interface hamy.SignOnRemoteHome
    hamy.SignOnRemote create() throws CreateException, RemoteException;
    ^
    SignOnBean.java:25: cannot find symbol
    symbol : class CreateException
    location: class hamy.SignOnBean
    throw new CreateException(“Could not look up context”);
    ^

    16 errors
    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy
    SignOnBean.java:2: package javax.ejb does not exist
    import javax.ejb.SessionBean;
    ^
    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Running ” compile.bat” in C:\Users\SonDaiCa\Documents\NetBeansProjects\EnterpriseApplication2\EnterpriseApplication2-ejb\src\java\hamy

    SignOnBean.java:2: package javax.ejb does not exist

    import javax.ejb.SessionBean;

    ^

    SignOnBean.java:3: package javax.ejb does not exist

    import javax.ejb.SessionContext;

    ^

    SignOnBean.java:5: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnBean.java:7: cannot find symbol

    symbol: class SessionBean

    public class SignOnBean implements SessionBean {

    ^

    SignOnBean.java:8: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    private SessionContext ctx;

    ^

    SignOnBean.java:13: cannot find symbol

    symbol : class SessionContext

    location: class hamy.SignOnBean

    public void setSessionContext(SessionContext c) {

    ^

    SignOnBean.java:18: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    public void ejbCreate() throws CreateException {

    ^

    SignOnRemote.java:4: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemote.java:5: package javax.ejb does not exist

    import javax.ejb.EJBObject;

    ^

    SignOnRemote.java:6: cannot find symbol

    symbol: class EJBObject

    public interface SignOnRemote extends EJBObject {

    ^

    SignOnRemoteHome.java:2: package javax.ejb does not exist

    import javax.ejb.*;

    ^

    SignOnRemoteHome.java:4: package javax.ejb does not exist

    import javax.ejb.CreateException;

    ^

    SignOnRemoteHome.java:5: package javax.ejb does not exist

    import javax.ejb.EJBHome;

    ^

    SignOnRemoteHome.java:6: cannot find symbol

    symbol: class EJBHome

    public interface SignOnRemoteHome extends EJBHome {

    ^

    SignOnRemoteHome.java:7: cannot find symbol

    symbol : class CreateException

    location: interface hamy.SignOnRemoteHome

    hamy.SignOnRemote create() throws CreateException, RemoteException;

    ^

    SignOnBean.java:25: cannot find symbol

    symbol : class CreateException

    location: class hamy.SignOnBean

    throw new CreateException(“Could not look up context”);

    ^

    16 errors

    Phải sửa thế nào hả thầy .Thầy giáo trả lời giúp em nhanh với.

  4. Võ Văn Hải said

    Khủng bố quá!😦.
    Cách sửa đó là em phải add các gói có liên quan vào classpath. Chúc vui.

  5. THONGMONEY said

    sao em run được ra trang login đăng nhập rồi mà khi đăng nhập thì ko thể chạy được LoginServlet. code như sau:

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
    response.setContentType(“text/html;charset=UTF-8”);
    PrintWriter out = response.getWriter();
    String empID = request.getParameter(“txtUsername”);
    String password = request.getParameter(“txtPassword”);
    Context c = new InitialContext();
    TblEmployeeFacadeRemote empR = (TblEmployeeFacadeRemote) c.lookup(“TblEmployeeFacade/remote”);
    List checkusername = empR.findAll();
    Iterator icu = checkusername.iterator();
    boolean flag = false;
    while (icu.hasNext()) {
    TblEmployee emp = (TblEmployee) icu.next();
    if (empID.equals(emp.getEmpID())) {
    flag = true;
    }
    }
    if (flag) {
    TblEmployee tblemp = empR.find(empID);
    if (tblemp.getPassword().equalsIgnoreCase(password)) {
    if (!tblemp.getIscheck().booleanValue()) {
    LoginSession ls = new LoginSession();
    ls.setEmpID(empID);
    ls.setIsAdmin(tblemp.getIsAdmin());
    ls.setEmpName(tblemp.getFirstname());

    HttpSession session = request.getSession();
    session.setAttribute(“Login”, ls);
    if (tblemp.getIsAdmin()) {
    response.sendRedirect(“Admin/ViewEvent.jsp”);
    } else {
    response.sendRedirect(“registerEvent.jsp”);
    }
    } else {
    // response.sendRedirect(“LoginFail.jsp”);
    }
    } else {
    response.sendRedirect(“LoginFail.jsp”);
    }
    } else {
    response.sendRedirect(“LoginFail.jsp”);
    }
    } catch (NamingException ex) {
    Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    }

  6. Duong Nguyen said

    e chào thầy,e có câu hỏi về EJB mà ko biết hỏi ở đâu nên e hỏi ở đây mong thầy trả lời e ah :

    e làm ứng dụng về EJB: deploy ok
    khi e gọi ct.lookup(jndiname);
    thì nó luôn báo lỗi …jndi not bound
    e kiểm tra tên jndi trong file jboss.xml xong cop sang,nhưng vẫn bị lỗi đấy
    e có tham khảo trên mạng
    http://localhost:1099/xm-jboss
    để hiển thị xem JNDIView
    thì lại ko có tên jndi trong danh sách tên các JNDI (lúc trước e đã tạo và chạy ổn định các JNDI này)
    e xin thầy cách giải quyết cho vấn đề này ah.
    E cảm ơn thầy!

  7. Võ Văn Hải said

    Deploy ok nhưng nó có báo là nó bound vào tên gì trên server không? bạn thử copy cái log lúc deploy xong thử như thế nào.
    Nếu bạn dùng EJB 3x thì khi bạn dùng lookup thì ngoài cái tên bạn thêm vào /remote nếu là dùng remote. Còn nếu dùng local thì bạn nên tạo JNDI name trong jboss.xml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: