Showing posts with label webservices. Show all posts
Showing posts with label webservices. Show all posts

Thursday, 15 December 2011

Accessing the Web Context


The context in which Web components execute is an object that implements the ServletContext interface. You retrieve the Web context with the getServletContext method. The Web context provides methods for accessing:
  • Initialization parameters
  • Resources associated with the Web context
  • Object-valued attributes
  • Logging capabilities
The Web context is used by the Duke's Bookstore filters filters.HitCounterFilter and OrderFilter, discussed in the section Filtering Requests and Responses. The filters store a counter as a context attribute. Recall from Controlling Concurrent Access to Shared Resources that the counter's access methods are synchronized to prevent incompatible operations by servlets that are running concurrently. A filter retrieves the counter object with the context's getAttribute method. The incremented value of the counter is recorded with the context's log method.
public final class HitCounterFilter implements Filter {
   private FilterConfig filterConfig = null;
   public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
      ...
      StringWriter sw = new StringWriter();
      PrintWriter writer = new PrintWriter(sw);
      ServletContext context = filterConfig.
         getServletContext();
      Counter counter = (Counter)context.
         getAttribute("hitCounter");
      ...
      writer.println("The number of hits is: " +
         counter.incCounter());
      ...
      context.log(sw.getBuffer().toString());
      ...
   }
}

Monday, 5 December 2011

EJB Based Web Services


File: jndi.properties
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
 
File: Main.java
import java.util.Date;
import javax.naming.InitialContext;
import bean.EmployeeServiceRemote;
 
public class Main {
  public static void main(String[] a) throws Exception {
    EmployeeServiceRemote service = null;
    // Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
    // service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
    service = (EmployeeServiceRemote) new InitialContext().lookup("EmployeeBean/remote");
 
 
 
 
    //service.doAction();
  }
}
 
File: Employee.java
package bean;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.PostRemove;
 
@Entity
public class Employee implements java.io.Serializable {
  private int id;
  private String firstName;
  private String lastName;
  @Id
  @GeneratedValue
  public int getId() {
    return id;
  }
 
  @PostRemove
  public void postRemove()
  {
     System.out.println("@PostRemove");
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String first) {
    this.firstName = first;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String last) {
    this.lastName = last;
  }
}
 
File: EmployeeBean.java
package bean;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
 
@WebService(name = "MyEmployee", serviceName = "MyEmployeeService")
//@Stateless
public class EmployeeBean implements EmployeeServiceLocal, EmployeeServiceRemote {
  @PersistenceContext(unitName = "EmployeeService")
  private EntityManager entityManager;
  public EmployeeBean() {
  }
  @WebMethod
  public void createEmployee(@WebParam(name = "employee")Employee c) {
    entityManager.persist(c);
  }
  @WebMethod
  @WebResult(name = "Employee")
  public Employee findEmployee(@WebParam(name = "ID")int pKey) {
    return entityManager.find(Employee.class, pKey);
  }
}
 
File: EmployeeServiceLocal.java
package bean;
 
import javax.ejb.Local;
 
@Local
public interface EmployeeServiceLocal {
  public void createEmployee(Employee c);
  public Employee findEmployee(int id);
}
 
File: EmployeeServiceRemote.java
package bean;
import javax.ejb.Remote;
 
@Remote
public interface EmployeeServiceRemote {
  public void createEmployee(Employee c);
  public Employee findEmployee(int id);

Friday, 18 November 2011

What Are Web Services?



Web services are distributed application components that are externally available. You can use them to integrate computer applications that are written in different languages and run on different platforms. Web services are language and platform independent because vendors have agreed on common web service standards.
Web service applications are deployed to a Java EE application server, such as GlassFish / Sun Java System Application Server.
This page provides links to some of the NetBeans documents and resources that can help you learn how to develop web service applications with NetBeans IDE.