Você está na página 1de 23

J2EE 11 th program Step 1: Right Click ---------New Project------->Java Web------->Web Application-------> Next----------->Project Name :TestTag--------->Next Server------>Apache Tomcat 6.

.0 (this will be listed select this)---------->Next-----> Press Finish Button Step 2: Right click on the project ------New------->Others------>a new window will pop up in the categories column select :web and file type:Tag library Descriptor click Next Specify TLD Name: test Click on Finish Button Step 3: Again Right click on the project ------New------->Others------>a new window will pop up in the categories column select :web and file type:Tag Handler Class Name : ParentTagHandler Package tld TLD File : test.tld (click on the browser option and select this file) Click the new Button in the window

When you click on New Button a window will pop up (this is the window)

Type the Atrribute Name: country check the option for required attribute check value evaluated at JSP translation time click on OK button Click on Finish Button

Step 4: Again Right click on the project ------New------->Others------>a new window will pop up in the categories column select :web and file type:Tag Handler Class Name : ChildTagHandler Package tld TLD File : test.tld (click on the browser option and select this file) Click the new Button in the window

When you click on New Button a window will pop up (this is the window)

Step 5: openthe index.jsp file (which is available in web pages folder) in the 1st line type this following code <%@taglib prefix="custom" uri="/WEB-INF/tlds/test.tld" %>

In the body tag type this following code.

<custom:ParentTagHandler country="india" > <custom:ChildTagHandler country="india">I am in India</custom:ChildTagHandler> <custom:ChildTagHandler country="uk">I am in UK</custom:ChildTagHandler> </custom:ParentTagHandler> Step 6: go to ParentTagHandler File There will be a statement like this public class ParentTagHandler extends SimpleTagSupport instead of SimpleTagSupport replace it with TagSupport (public class ParentTagHandler extends TagSupport) and change even the import package to import javax.servlet.jsp.tagext.TagSupport; Delete the contents of the file except setCountry() function code in the same class Right Click-------->Insert Code-------->Override Method------>select the option doStartTag()-------->Click On Generate Button. Inside doStartTag() function write this statement return EVAL_BODY_INCLUDE; Again Right Click-------->Insert Code-------->Getter-------->select the option country (there will be a check box click on that)--------->click on Generate Button.

public String getCountry() { return country; } This code will be generated in the class.

Step 7:go to ChildTagHandler File There will be a statement like this public class ChildtTagHandler extends SimpleTagSupport instead of SimpleTagSupport replace it with TagSupport (public class ChildTagHandler extends TagSupport) and change even the import package to import javax.servlet.jsp.tagext.TagSupport; Delete the contents of the file except setCountry() function code in the same class Right Click-------->Insert Code-------->Override Method------>select the option doStartTag()-------->Click On Generate Button. Declare the variable of type Tag write this statement (public Tag parent;) one error will pop up you canfind the bulb symbol just click on that and select the option javax.servlet.jsp.tagext.Tag Step 8: Inside the same ChildTagHandler class Right click-------->Insert Code------->Override Method ---------->select the option setParent (Tag t)(click on the checkbox) ------------>Generate Step 9 :Right Click-------->Insert Code-------->Getter-------->select the option country (there will be a check box click on that)--------->click on Generate Button. Step 10: inside setParent(Tag t) function type this code parent=t; Step 11:Inside doStartTag() function type this following code ParentTagHandler pth=(ParentTagHandler)parent; if(getCountry().equals(pth.getCountry())) { return EVAL_BODY_INCLUDE; } return SKIP_BODY; Step 12: save all the files Step 13: right Click ----->Clean And Build right Click------->deploy Right Click------->run OUTPUT WILL BE DISPLAYED ON THE BROWSER

J2EE 12Th Program Step 1: Select the project Tab and right Click and say New Project ----->Java EE-------->Enterprise Application --------->Next----------------->Project Name: StudentSession------>Next------>Check for GlassFish V2.1 server is selected or not --------------------->Finish Step 2: Click on the WAR module double click on index.jsp file write the following code in the body tag. <h1>Calculator</h1> <hr> <form action="callcalculator"> <p>Enter first value: <input type="text" name="num1" size="25"></p> <br> <p>Enter second value: <input type="text" name="num2" size="25"></p> <br> <b>Select your choice:</b><br> <input type="radio" name="operation" value ="add">Addition<br> <input type="radio" name="operation" value ="sub">Subtraction<br> <input type="radio" name="operation" value ="multi">Multiplication<br> <input type="radio" name="operation" value ="div">Division<br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"></p> </form> Step 3: In the War Module Right Click On ------------->New--------------->Servlet---------> Class Name:callcalculator Package com Next------------------>Finish Step 4: Right Click On EJB module and New-------------------->Session Bean---------------> Class Name callogic Package com Select Stateless option Check the box of Both Remote and Local Click On Finish Step 5:Right click on the class and Select the option Insert Code--------------->Add Bussiness Method---------Name:add Type double // To add parameters Click On the add button Name :num1 Type double Click On the add button Name :num2 Type double

Use of Interface Press OK Button

Both

Step 6 : Write this statement in add function return num1+num2; Step 7: Right click on the class and Select the option Insert Code--------------->Add Bussiness Method---------Name: sub Type double // To add parameters Click On the add button Name :num1 Type double Click On the add button Name :num2 Type double Use of Interface Press OK Button Both

Step 8:Write this statement in sub function return num1-num2; Step 9:Right click on the class and Select the option Insert Code--------------->Add Bussiness Method--------- Name: multi Type double // To add parameters Click On the add button Name :num1 Type double Click On the add button Name :num2 Type double Use of Interface Press OK Button Both

Step 10: Write this statement in multi function return num1*num2; Step 11: Right click on the class and Select the option Insert Code--------------->Add Bussiness Method--------- Name: div Type double // To add parameters Click On the add button Name :num1 Type double Click On the add button Name :num2 Type double Use of Interface Press OK Button Both

Step 12: Write this statement in div function return num1/num2; Step 13: In the War Module ------------->source packages------------>com--------->callcalculator(Servlet Class) Double Click on the file Right Click inside the Class and Insert Code--------------->Call Enterprise Bean---------> A new Window will be poped up-------------->Select callogic Bean(Session Bean) Press OK Button

Step 14: In the processRequest method just write the following code String num1=request.getParameter("num1"); String num2=request.getParameter("num2"); String opt=request.getParameter("operation"); double result; if(opt.equals("add")) { result=callogicBean.add(Double.valueOf(num1),Double.valueOf(num2)); out.println("Result is "+result); } if(opt.equals("sub")) { result=callogicBean.sub(Double.valueOf(num1),Double.valueOf(num2)); out.println("Result is "+result); } if(opt.equals("multi"))

{ result=callogicBean.multi(Double.valueOf(num1),Double.valueOf(num2)); out.println("Result is "+result); } if(opt.equals("div")) { result=callogicBean.div(Double.valueOf(num1),Double.valueOf(num2)); out.println("Result is "+result); } Step 15: Save all the files and Right Click on the Project--------->Clean And Bulid----------> Deploy----------->Run.

J2EE 13 th Program Step 1: Create database with a name student command is create database student; use student; create table stud(id varchar(10) primary key,name varchar(10),marks varchar(5)); note : while creating a table there should be a primary key. Step 2: Go to Services Tab(if not available go to window----->services then services tab will be available on left side of the netbeans) Click on Databases ------->Right Click -------->New Connection--------->A new Window will pop up. New Database Connection window will be opened Name : MySQL(Connector/JDriver) Host: localhost Port:3306 Database :student User Name : root Password: mysql(specify your mysql password) Check the check box of Remember Password Check the check box of Show JBDC Click On OK BUTTON

Step 3: In the databases tab you can see the connection jdbc:mysql://localhost:3306/student and if the symbol is broken just right click and say connect it will be conneected.

Step 4: Select the project Tab and right Click and say New Project ----->Java EE-------->Enterprise Application --------->Next----------------->Project Name: StudentEntity------>Next------>Check for GlassFish V2.1 server is selected or not --------------------->Finish Step 5: Select the EJB module and Right Click ------->New------>Entity Classes From Databases--->A new Window will be popped up.... Step 6: Datasource Name :Select the option New Connection a new window will be poped up. JNDI Name :teststudent Database Connection: From the drop down menu select the jdbc:mysql://localhost:3306/student option Press the OK Button. Step 7: Available Tables you find all the tables that are available in the database select the stud Table and click on the Add button ------>Next--------->type Package : com Click on the button Create Persistence Unit----------->a Window will be popped up----------->just click on create Button------------>That window will be closed------->Next------>Finish Step 8: Click on The EJB module ------>Right Click-------->New------>Session Bean------>EJB Name:callStudentEntity---------->Package :com------------->Click on Stateless--------->Select the Remote option and Local Option also-------Click On Finish Step 9 : Right click in the class and select the persistence unit------------>Use Entity Manager This code will be automatically generated.. @PersistenceContext private EntityManager em; public void persist(Object object) { em.persist(object); } Step 10:Click on the WAR module double click on index.jsp file write the following code in the body tag. <form action="callEntity"> ID :<input type="text" name="id"> NAME :<input type="text" name="name"> MARKS :<input type="text" name="marks"> <input type="radio" name="operation" value="add">ADD <input type="radio" name="operation" value="delete">DELETE <input type="radio" name="operation" value="viewall">ADD <input type="radio" name="operation" value="search">SEARCH <input type="submit" value="SUBMIT"> </form> Step 11: Right Click on War Module and Select New---------->Servlet-------->Class Name: callEntity ---------------->package com Next----------------->Finish Right click inside the servlet class and select Insert Code------->Call Enterprise Bean -------->A New Window will be popped up and then select callStudentEntityBean-------

>instead of Local Select the Remote Option and --------------------->OK

Step 12: Go to ejb module---------------->source packages----------------->select callSudentEntityBeanBean.java and write the following code inside this class Inside this class right click-------->Insert Code ------->Add Business Method------

Name: add Return Type: void Click on the Add Button : add the parameters Name id Type java.lang.String Again Click on the Add Button : add the parameters Name name Type java.lang.String Again Click on the Add Button : add the parameters Name marks Type java.lang.String Use of Interface Both and Press OK Button

Step 13: Write the following code inside the add method Stud s=new Stud(); s.setId(id); s.setMarks(marks); s.setName(name); em.persist(s); Step 14: inside this class right click-------->Insert Code ------->Add Business Method----- a new window will be popped up

type the following content Name : viewall Return Type : List Use in Interface Both Press OK Button

Step 15: Write the following code inside viewall() function Query q = em.createNativeQuery("select * from stud"); List l = q.getResultList(); return l; Near List an error will be popped up Just Right Click on the bulb and You can find an option Add import java..util.List Just Select the option Near Query an error will be popped up Just Right Click on the bulb and You can find an option Add import javax.Persistence.Query Just Select the option In callStudentEntityRemote and callStudentEntityLocal files you can see error near List word an error will be popped up Just Right Click on the bulb and You can find an option Add import java..util.List Just Select the option

Step 16: Inside this class right click-------->Insert Code ------->Add Business Method----- A new window will be popped up type the following content

Name : search Return Type : List Click on Add Button Name id Type java.lang.String select the option Both Press OK Button

Write the following Code Query q = em.createNativeQuery("select * from stud where id = " + id); List l = q.getResultList(); return l; Step 17: inside this class right click-------->Insert Code ------->Add Bussiness Method----- A new window will be popped up type the following content Name : delete Return Type : int Click on Add Button Name id Type java.lang.String select the option Both

Press OK Button

Step 18: Write the following Code Query q = em.createNativeQuery("delete from stud where id = " + id ); int flag = q.executeUpdate(); return flag; Step 19: Go to War module ----->Source packages----------->com------------->callEntity(double click on this file) and write the following code in the try block try { String name = request.getParameter("name"); String id = request.getParameter("id"); String marks = request.getParameter("marks"); String opt = request.getParameter("operation"); if(opt.equals("add")) { callStudentEntityBean.add(id,name,marks); response.sendRedirect("index.jsp"); } if(opt.equals("search")) { List l = callStudentEntityBean.search(id); Iterator i= l.listIterator(); while(i.hasNext()) { out.println(i.next()); } } if(opt.equals("viewall")) { List l = callStudentEntityBean.viewall(); Iterator i= l.listIterator(); while(i.hasNext()) { out.println(i.next()); out.println("\n"); } } if(opt.equals("delete")) { int flag = callStudentEntityBean.delete(id); if(flag == 1) { out.println("sucessfully deleted"); } else out.println("deletion failed"); } Step 20: Right Click on Project Tab------------->Clean and Build-----------------------

>then--------------------->Deploy-------------------------------->Run-------------------> Step 21: Cross check with the database.

J2EE 14 TH Program Step 1: Open the services Tab it will be available on left side of the netbeans window if not available the go to Window---> Services (then it will be available on left side of netbeans window) Step 2: You can find Servers (click on + symbol) you can find GlassFish and apache been listed SELECT Glassfish v2 Right Click on the GlassFish v2 and select the option Start Again Right Click And select the option View Admin Console Step 3: Browser will be opened. Enter the username : admin and password: adminadmin Step 4: This new page will be opened

Step 5: Resource---->Destination Resources----->New Type JNDI Name:jms/testq Physical Destination Name: testing ResorceType:javax.jms.Queue Click on OK button Step 6: Select Resources Option which is available on left side of the page.

Select JMS Resources Select Connection Factories A new window will be opened on right side after selecting connection factories option Click on New Button Type JNDI Name:jms/testqueue ResorceType:javax.QueueConnectionFactory Click on OK button

Step 7: Minimize the browser and open Netbeans window and select on project window Right Click ---------New Project------->JavaEE------->Enterprise Application-------> Next----------->Project Name :TestMDB--------->Next----->Finish Select EJB module and right click New------>Message Driven Bean------>EJB Name:processMSG Package com Check the Server Destinations: jms/testq (this will be listed select this destination name) Press Finish Button

Step 8: Now select the war module Right Click ------->New------->Servlet------->ServletName: callMDB Package com Next------>Finish In this servlet file just right Click and select the option Insert Code----->send JMS_Message option A new window will be opened type Connection factory as: jms/testqueue (the connection factory name which you had specified initially).

And click OK Button. In the same class you have processRequest method Write this code this.sendJMSMessageToTestq(request); An error will be poped up just click on that bulb and fix the error (Surround the try with catch block) this option you need to select. Save the file press ctrl+s In createJMSMessageForjmsTestq() method write this code

TextMessage tm = session.createTextMessage(); HttpServletRequest rq=(HttpServletRequest)messageData; tm.setText(rq.getParameter("msg")); return tm; Step 9: Go do war module ----->click on + symbol you can find Web Pages --->select index.jsp file Type the following code inside the <body> tag <form action="callMDB"> SEND MSG:<input type="text" name="msg"> <br> <input type="submit" name="SEND"> <br> </form> Save the file press ctrl+s Step 10: Go to ejb module----->click on + symbol-------->find source package-------find com folder and double click on processMSGBean file There will be onMessage() function type the following code TextMessage t =null; t=(TextMessage)message; try { System.out.println(t.getText()); } catch (JMSException ex) { Logger.getLogger(processMSGBean.class.getName()).log(Level.SEVERE, null, ex); } Errors will be popped up fix the errors and select the option add import javax.jms.JMSExeption; Step 11:Run the program and output will be displayed in output window

Você também pode gostar