Monday 5 December 2011

Eclipse Glassfish EE example

Hi, this is a simple ear application that demonstrates how to use eclipse with glassfish to build enterprise applications (J2EE). It combines the standard hello example of J2EE examples from NetBeans with the calculator example of web services example of NetBeans again. There is a simple trick in the web.xml of the war file to run the two examples seperately from the IE command line.


1.1 Create an Enterprise Application (name=ServletStatelessARSEAR)
1.2 Say OK to create an ejbClient (name = ServletStatelessARS-ejbClient)
1.3 Leave the rest as it is, you will add the EJB and war projects later on.
As seen in the picture.



2.1 Create under ejbModule an interface file at the ServletStatelessARS-ejbClient client application (loc/name=enterpriseARS.servlet_stateless_ejbClient/StatelessSessionARS.java)

package enterpriseARS.servlet_stateless_ejbClient;

public interface StatelessSessionARS {

public String sayHelloARS(String name);
public int add2Parms(int parm1, int parm2);

}





2.2 Create an EJB application (name=ServletStatelessARS-ejb) and attach it to the Enterprise application using the related creation option.
2.3 Create under ejbModule (loc/name=enterpriseARS.servlet_stateless_ejb/StatelessSessionARSBean.java)
package enterpriseARS.servlet_stateless_ejb;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jws.WebService;

import enterpriseARS.servlet_stateless_ejbClient.StatelessSessionARS;

@WebService (only for diagnostic purposes, to be used for Glassfish endpoint testing)
@Stateless
public class StatelessSessionARSBean
implements StatelessSessionARS {

public String sayHelloARS(String name) {
return "HelloARS, " + name + "!\n";
}

public int add2Parms(int parm1, int parm2){
return(parm1+parm2);
}

}
3.1 Create a dynamic web application (name=ServletStatelessARS-war) and attach it to the Enterprise application using the related creation option.





3.2 Web/index.jsp should be:
<%@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>GlassFish JSP Page</title>
</head>
<body>
<h1>Calculator Service</h1>
<form name="Submit" action="Servlet2ParmsAddARS">
<input type="text" name="value1" value="2" size="3"/>+
<input type="text" name="value2" value="2" size="3"/>=
<input type="submit" value="Get Result" name="getResult" />
</form>
</body>

</html>
3.3 Web.xml should be:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>ServletStatelessARS</display-name>
<distributable/>
<servlet>
<servlet-name>Servlet2StatelessARS</servlet-name>
<servlet-class>enterpriseARS.servlet_stateless_war.Servlet2StatelessARS</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2StatelessARS</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet2ParmsAddARS</servlet-name>
<servlet-class>enterpriseARS.servlet_stateless_war.Servlet2ParmsAddARS</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2ParmsAddARS</servlet-name>
<url-pattern>/Servlet2ParmsAddARS</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
servlet
</welcome-file>
</welcome-file-list>
</web-app>

3.4 In the Java Resources/src/enterpriseARS.servlet_stateless_war package,
Create Servlet2ParmsAddARS.java

package enterpriseARS.servlet_stateless_war;
import java.io.*;

import javax.ejb.EJB;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.naming.*;

import enterpriseARS.servlet_stateless_ejbClient.*;

// Though it is perfectly fine to declare the dependency on the bean
// at the type level, it is not required for stateless session bean
// Hence the next two lines are commented and we rely on the
// container to inject the bean.
// @EJB(name="StatelessSession", beanInterface=StatelessSession.class)

public class Servlet2ParmsAddARS
extends HttpServlet {

// Using injection for Stateless session bean is still thread-safe since
// the ejb container will route every request to different
// bean instances. However, for Stateful session beans the
// dependency on the bean must be declared at the type level

@EJB
private StatelessSessionARS sless;

public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html");
PrintWriter out = resp.getWriter();

try {

out.println("<h2>Servlet ClientServlet at " + req.getContextPath () + "</h2>");

int i = Integer.parseInt(req.getParameter("value1"));
int j = Integer.parseInt(req.getParameter("value2"));

int result = sless.add2Parms(i,j);

out.println("<br/>");
out.println("Result:");
out.println("" + i + " + " + j + " = " + result);

} catch (Exception ex) {
ex.printStackTrace();
System.out.println("webclient servlet test failed");
throw new ServletException(ex);
}
}

}
3.5 create Servlet2StatelessARS.java at the same package location.

package enterpriseARS.servlet_stateless_war;

import java.io.*;

import javax.ejb.EJB;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.naming.*;

import enterpriseARS.servlet_stateless_ejbClient.*;
public class Servlet2StatelessARS
extends HttpServlet {
@EJB
private StatelessSessionARS sless;

public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html");
PrintWriter out = resp.getWriter();

try {

out.println("<HTML> <HEAD> <TITLE> Servlet Output </TITLE> </HEAD> <BODY BGCOLOR=white>");
out.println("<CENTER> <FONT size=+1> Servlet2Stateless:: Please enter your name </FONT> </CENTER> <p> ");
out.println("<form method=\"POST\">");
out.println("<TABLE>");
out.println("<tr><td>Name: </td>");
out.println("<td><input type=\"text\" name=\"name\"> </td>");
out.println("</tr><tr><td></td>");
out.println("<td><input type=\"submit\" name=\"sub\"> </td>");
out.println("</tr>");
out.println("</TABLE>");
out.println("</form>");
String val = req.getParameter("name");

if ((val != null) && (val.trim().length() > 0)) {
out
.println("<FONT size=+1 color=red> Greeting from StatelessSessionBean: </FONT> "
+ sless.sayHelloARS(val) + "<br>");
}
out.println("</BODY> </HTML> ");

} catch (Exception ex) {
ex.printStackTrace();
System.out.println("webclient servlet test failed");
throw new ServletException(ex);
}
}

}

4.1 It will give IDE and compile error for the EJB because currently the dynamic web application does not see the EJB application.
4.2 Right click on the dynamic web application and check that Project references indicates ejb and ejbClient applications as referred.
4.3 Go to the Java Build Path and click on the Projects tab. Add the ejb and ejbClient applications.
4.4 The IDE – compile error for the EJB disappears.
5. You may also check yhe ear application for the same items but they are done automatically for it.
6. Make sure you test the application in an orderly manner. Build the EAR application and also build the others if necessary. When you run the war application the calculator works as default. If you run
http://localhost:8080/ServletStatelessARS-war/servlet then the hello message works.








7. If you experience any problems export the ear application to a war file at the
C:\Program Files\glassfish-3.1\glassfish\domains\domain1\autodeploy
Autodeploy directory of Glassfish. Then restart Glassfish, everything will be OK.


It is free of charge to request a copy of the source files. I am sometimes hecticly busy but I promise to respond in a couple of hours.

Cheers.

Ali R+ SARAL








Sunday 4 December 2011

Referring to another project using Eclipse

There are two seperate projects. Main project calls a method in the ReferredProject.


ReferredProject has
referredProjectPackage/ReferenceClass.java
package referredProjectPackage;

public class ReferenceClass {
public void sayHello(){
System.out.println("Hello from the reference class.");
}
}
MainProject has
mainPackage/MainClass.java
package mainPackage;
import referredProjectPackage.*;

public class MainClass {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("MainClass works.");
ReferenceClass rc = new ReferenceClass();
rc.sayHello();
}

}
It gives error:



Right Click on the MainProject in the PackageExplorer. Select properties at the bottom. Properties for the MainProject window opens. Select JAVA build path and then select the Projects tab.



Select ReferredProject and then click OK. The errors disappear and the program works.
But to be on the safe side, do not forget to do also:
Right click on the MainProject, select Project References, select ReferredProject and click OK.




Saturday 3 December 2011

How to use Eclipse with GlassFish server(2)

13- Right Click on the application name and slide to the bottom and select ‘properties’ for our web app TestEclipseGlassFishARS.

14- Select libraries and observe the source of the problem.




15- Select JRE System Library and click the edit button on the right.






16- Click the Add button the Installed JREs window.





17- Select Standard VM on the JRE type window.




18- The JRE Definition window opens. Select JRE Home using the directory button and go to the JDK 1.6 installation directory.




19- Your choice fills in all the necessary fields on the JRE Definition window. Click the finish button.



20- Installed JRE’s window appears again with the newly added JDK 1.6 directory.



21- Select JDK 1.6 and return back to the JRE System Library window which also has the new addition as seen in the picture. Select JDK 1.6 as Alternate JRE and then click Finish.




22- JRE System Library is now changed to JDK 1.6 as required by GlassFish.




23- Try once more to run the test web app. It works. Don’t g’ve up! You will make it.



Cheers.

Ali R+ SARAL

Note: I know that the picture quality may not prove to be good. If you like you may obtain a Word format of this tutorial for free from arsaral(at)yahoo.com. Sometimes I may be hecticly busy but I promise a response with in a couple of hours.






How to use Eclipse with GlassFish server (1)


I am going to explain how to use Eclipse with Glassfish as a server. This tutorial uses a very simple, picture based approach. So trust me and take my advises you will make it to the end for sure.
1- Create a dynamic web project:
Open eclipse, file, new, [web], dynamic web project









2- Create a new server
Go to bottom right frame, select servers, right click in the frame content area, server, new






3- If GlassFish connection has been made for your Eclipse installation previously you will get this picture. But in your case, GlassFish will not appear.



4- Click ‘Download additional web servers’. It will open ‘ install new extension’ window and begin searching the internet. This will take a long time.



5- Be patient and wait till the end when finally it finds GlassFish and Jboss.






6- Select GlassFish and see that the previous window has the GlassFish items now.






7- Select GlassFish 3.1 and a new screen taht asks for the location of Glassfish on your computer opens. It also calls for your choice of jre.


8- Then you are asked for the admin directory and the admin password which happens to be adminadmin









9- Now it is time to try the very simple dynamic web program that we have created.




Do not forget to put index.jsp as a welcome file in the web.xml.
Index.jsp is on the picture.





10. Run the webapp on the new server.





11- Select Glassfish






12- ERROR: Glassfish requires JDK 1.6 and not a JRE.






You can request a better printed World version of this tutorial from arsaral(at)yahoo.com

The pictures are much better and scripts can be easily read in that version.