steps for ejb

Upload: nivi-senthil

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Steps for EJB

    1/20

    The following steps are for Netbeans 6.5.1.

    Pre-requisites: MySQL should be already installed. Incase of any problems,

    contact Moses sir

    1) Open Netbeans.2) File-> new Project -> Java EE -> Enterprise Application

    JSP Servlet Session

    Bean

    Entity Bean

    Databas

  • 7/30/2019 Steps for EJB

    2/20

    Click Finish

  • 7/30/2019 Steps for EJB

    3/20

    Three folders will be created.

    Bidding

    Bidding-ejb

    Bidding-war

    Bidding-ejb - Enterprise bean - right click - new - session bean

    Give a name and package name

    Choose stateful and Remote

  • 7/30/2019 Steps for EJB

    4/20

    Give finish

    This will create two java files in (Bidding-ejb - source packages session)

    This is nothing but the bean class and the remote interface that maam explained

    in class.

    There will be three tabs on the left pane (Project, Files and Services)

    Click on the services tab.

    Open Databases.

    If you have installed MySQL, the first option in that will be MySQL server at

    localhost:3306[root].

    Right clock on that and give connect.

    Again right click on it and give Create database.

    Give a name for the database.

    This name will now appear in the list in the Services tab.

  • 7/30/2019 Steps for EJB

    5/20

    Click on the + mark in it. Right click on Tables and choose Create Table.

    Give a table name and whatever fields you want. Make only one field Primary

    key by choosing Key

  • 7/30/2019 Steps for EJB

    6/20

    Click OK.

    Right click on the table and choose view data.

    No data is available. To add data, choose the small button(with a + symbol)

    displayed above the column names. Add the data and click OK.

    Go back to Project tab.

    Bidding-ejb Enterprise Bean right click new Entity Class from database.

    Choose New Data Source

  • 7/30/2019 Steps for EJB

    7/20

    Give a JNDI name

    And choose the database that you created.

    The tables available in the mentioned database will be displayed. Add the table

    to the Selected Tables list.

  • 7/30/2019 Steps for EJB

    8/20

    Give Next. In the screen that follows, Click on Create Persistance Unit. This is

    important.(you should remember this name. This is again used in the program).

    Give a name and click on create in the Table Generation Strategy. Then click

    Create.

  • 7/30/2019 Steps for EJB

    9/20

    Choose Next -> Finish. Now, you have finished creating the entity bean and the

    session bean. Now, you have to make a connection between your session and

    entity bean.

    Goto your session bean class. (Bidding-ejb source packages session)

    This shows three java files now. The one with the same name as your database

    table is your entity bean class. It contains a get and set method for each field in

    the table.

    Open the session bean class. Right click - Persistence Use Persistence

    manager

  • 7/30/2019 Steps for EJB

    10/20

    This will add the required code automatically.

    Inside the class, there will be a line that goes like this

    private EntityManager em;

    Below this line, type the following line.

    private EntityManagerFactory emf;

    A small bulb symbol will appear at the beginning of the line when you finish

    typing this. Just click on it and choose Add import for

    javax..persistence.EntityManagerFactory.

    There will be another bulb symbol below. Click on whatever option it shows.

  • 7/30/2019 Steps for EJB

    11/20

    Go to the bean class again. Right click and choose insert Code and choose

    constructor. Just click on generate without selecting anything.

    Type in the following lines inside the constructor.

    emf = Persistence.createEntityManagerFactory("prod");

    em = emf.createEntityManager();

  • 7/30/2019 Steps for EJB

    12/20

    The prod inside the double quotes is the name of the persistent unit that you

    gave previously.

    As of now, I have created a database table that has two columns (name and

    ID(primary key)). I have created a entity bean which the mapping of this

    database table and I have created a Session Bean.

    Now all that remains on the server side is to connect this session bean with the

    entity bean.

    In this program, the user will enter an ID and the corresponding name will be

    returned.

    Now, right click on the bean class and choose insert code and Add Business

    Method.

  • 7/30/2019 Steps for EJB

    13/20

    This method gives in and ID and gets the Name

    Inside the getName() method,Type in

    Name n = em. find(Name.class, id);

    i.e you are creating an object for the entity bean class(Name) and find file called

    Name.class passing the primary key id.

  • 7/30/2019 Steps for EJB

    14/20

    Instead of returning null, type in

    return n.getName();

  • 7/30/2019 Steps for EJB

    15/20

    The server side is over. Now, we just have to create the client side JSP file.

    Goto Bidding-war webpages index.jsp

    In index.jsp, provide the a text box and a button in a form whose action is calling

    a servlet named getServlet

    Code:-

  • 7/30/2019 Steps for EJB

    16/20

    JSP Page

    Hello World!

    To create the servlet, Right click on WEB-INF new servlet

    Give the servlet name as getServlet, give a package name , give next and then

    finish.

    Right click in the getServlet class insert code Call Enterprise Bean

    Choose the session bean class and click OK.

  • 7/30/2019 Steps for EJB

    17/20

    This will create an object for the Remote Interface inside the servlet.

    We first have to obtain the ID from the JSP and send it to the Session bean class

    which will in turn send it to the entity bean class and retrieve the corresponding

    Name.

    Inside the try block, type in the following.

  • 7/30/2019 Steps for EJB

    18/20

    int id = Integer.parseInt(request.getParameter("id"));

    String name = statefulBean.getName(id);

    out.println("the ID is " + id + "\n");

    out.println("The correspoding name is " + name);

    TO INSERT A ROW INTO THE DATABASE:-

    I have considered a simple signup module where the user will enter a new

    username and password and send it to a servlet which will inturn send it to a

    method present in the session bean class.

    Here, I have created a method named setPassword to obtain the username and

    password from the servlet , create an Entity class object and insert the data into

    the database.

    public void setPassword(String username, String password)

    {

    Login l = new Login();

    l.setUsername(username);

    l.setPassword(password);

    em.persist(l);

    }

  • 7/30/2019 Steps for EJB

    19/20

    The method persist() is used to insert the elements.

    P.S: I havent validated the data input by the user. If you want, it can be done

    using client side javascript, or the servlet that can check if that username already

    exists in the database.

    Now, Clean and Build all the three components and run Bidding.

    If everything goes right, for the input

    The output should be

  • 7/30/2019 Steps for EJB

    20/20