Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A simple hello world program

hello world j2ee java

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 FaizalRahman

FaizalRahman

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 18 May 2013 - 08:18 PM

Hi. I'm new to EJB. I created a simple hello world program using Netbeans 7.3. The structure of the application and codes are shown below. I tried the codes but I couldn't figure out why the method does not print. Please help.
 
Structure of application:
+ HelloWorld_Application
+ HelloWorld_Application-ejb
   -> HelloWorldBean.java
   -> HelloWorldBeanLocal.java
+ HelloWorld_Application-war
   -> HelloWorldServlet.java
 
 
The codes:
 
------------------------------------------------------HelloWorldBean.java-----------------------------------------------------------------
package ejb;

import javax.ejb.Stateless;

@Stateless
public class HelloWorldBean implements HelloWorldBeanLocal
{
public HelloWorldBean() {}
public String sayHello() {return "I say hello";}
}
------------------------------------------------------End of HelloWorldBean.java----------------------------------------------------------

------------------------------------------------------HelloWorldBeanLocal.java------------------------------------------------------------
package ejb;

import javax.ejb.Local;

@Local
public interface HelloWorldBeanLocal
{
String sayHello();
}
------------------------------------------------------End of HelloWorldBeanLocal.java-----------------------------------------------------
import ejb.HelloWorldBeanLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class HelloWorldServlet extends HttpServlet
{
@EJB
private static HelloWorldBeanLocal helloUser;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloWorldServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet HelloWorldServlet at " + request.getContextPath() + "</h1>");
out.println("<h1>" + helloUser.sayHello() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}Attached File  HelloWorld_Application.zip   76.81KB   173 downloads




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download