5562N/A/*
5562N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5562N/A *
5562N/A * Copyright (c) 2009-2010 Oracle and/or its affiliates. All rights reserved.
5562N/A *
5562N/A * The contents of this file are subject to the terms of either the GNU
5562N/A * General Public License Version 2 only ("GPL") or the Common Development
5562N/A * and Distribution License("CDDL") (collectively, the "License"). You
5562N/A * may not use this file except in compliance with the License. You can
5562N/A * obtain a copy of the License at
5562N/A * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
5562N/A * or packager/legal/LICENSE.txt. See the License for the specific
5562N/A * language governing permissions and limitations under the License.
5562N/A *
5562N/A * When distributing the software, include this License Header Notice in each
5562N/A * file and include the License file at packager/legal/LICENSE.txt.
5562N/A *
5562N/A * GPL Classpath Exception:
5562N/A * Oracle designates this particular file as subject to the "Classpath"
5562N/A * exception as provided by Oracle in the GPL Version 2 section of the License
5562N/A * file that accompanied this code.
5562N/A *
5562N/A * Modifications:
5562N/A * If applicable, add the following below the License Header, with the fields
5562N/A * enclosed by brackets [] replaced by your own identifying information:
5562N/A * "Portions Copyright [year] [name of copyright owner]"
5562N/A *
5562N/A * Contributor(s):
5562N/A * If you wish your version of this file to be governed by only the CDDL or
5562N/A * only the GPL Version 2, indicate your decision by adding "[Contributor]
5562N/A * elects to include this software in this distribution under the [CDDL or GPL
5562N/A * Version 2] license." If you don't indicate a single choice of license, a
5562N/A * recipient has the option to distribute your version of this file under
5562N/A * either the CDDL, the GPL Version 2 or to extend the choice of license to
5562N/A * its licensees as provided above. However, if you add GPL Version 2 code
5562N/A * and therefore, elected the GPL Version 2 license, then the option applies
5562N/A * only if the new code is made subject to such option by the copyright
5562N/A * holder.
5562N/A */
5562N/A
5562N/Apackage client;
5562N/A
5562N/Aimport javax.annotation.Resource;
5562N/Aimport javax.servlet.*;
5562N/Aimport javax.servlet.http.*;
5562N/Aimport javax.naming.InitialContext;
5562N/Aimport javax.transaction.UserTransaction;
5562N/Aimport javax.transaction.Status;
5562N/Aimport java.io.IOException;
5562N/Aimport java.io.PrintWriter;
5562N/Aimport java.sql.*;
5562N/Aimport javax.sql.DataSource;
5562N/A
5562N/Aimport javax.xml.ws.*;
5562N/Aimport endpoint.ejb.*;
6370N/A
6370N/Apublic class Client extends HttpServlet {
5562N/A
5562N/A //@WebServiceRef(name="sun-web.serviceref/HelloEJBService")
5562N/A HelloEJBService service = new HelloEJBService();
5562N/A
5562N/A @Resource(mappedName="jdbc/__default") private DataSource ds;
5562N/A
5562N/A public void doGet(HttpServletRequest req, HttpServletResponse resp)
5562N/A throws javax.servlet.ServletException {
5562N/A doPost(req, resp);
5562N/A }
5562N/A
5562N/A public void doPost(HttpServletRequest req, HttpServletResponse resp)
5562N/A throws javax.servlet.ServletException {
5562N/A UserTransaction ut = null;
5562N/A // Create Table with name CUSTOMER_rb. This name will be used in the EJB
5562N/A String tableName = "CUSTOMER_rb";
5562N/A Connection con = null;
5562N/A try {
5562N/A con = ds.getConnection();
5562N/A con.setAutoCommit(true);
5562N/A createTable(con, tableName);
5562N/A ut = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
5562N/A ut.begin();
5562N/A
5562N/A System.out.println(" Service is :" + service);
5562N/A Hello port = service.getHelloEJBPort();
5562N/A
5562N/A String ret = port.sayHello("Appserver Tester !");
5562N/A System.out.println("Return value from webservice:"+ret);
5562N/A System.out.println("**** rollbacking transaction");
5562N/A // ut.rollback();
5562N/A ut.setRollbackOnly();
5562N/A if(ut.getStatus() != Status.STATUS_MARKED_ROLLBACK) {
5562N/A ret += "FAILED";
5562N/A }
5562N/A if(isDataPresent(con, tableName)) {
5562N/A ret += "FAILED";
5562N/A }
5562N/A PrintWriter out = resp.getWriter();
5562N/A resp.setContentType("text/html");
5562N/A out.println("<html>");
5562N/A out.println("<head>");
5562N/A out.println("<title>TestServlet</title>");
5562N/A out.println("</head>");
5562N/A out.println("<body>");
5562N/A out.println("<p>");
5562N/A out.println("So the RESULT OF EJB webservice IS :");
5562N/A out.println("</p>");
5562N/A out.println("[" + ret + "]");
5562N/A out.println("</body>");
5562N/A out.println("</html>");
5562N/A dropTable(con, tableName);
5562N/A } catch(Exception e) {
5562N/A e.printStackTrace();
5562N/A }finally {
5562N/A try {
5562N/A if(con != null) con.close();
5562N/A } catch (SQLException se) {}
5562N/A }
5562N/A }
5562N/A
5562N/A // use this table in the EJB webservice
5562N/A private void createTable(Connection con, String tableName) throws Exception {
5562N/A System.out.println("**** auto commit = " + con.getAutoCommit());
5562N/A PreparedStatement pStmt =
5562N/A con.prepareStatement("CREATE TABLE "+tableName+" (NAME VARCHAR(30) NOT NULL PRIMARY KEY, EMAIL VARCHAR(30))");
5562N/A pStmt.executeUpdate();
5562N/A }
5562N/A
5562N/A private void dropTable(Connection con, String tableName) throws Exception {
5562N/A PreparedStatement pStmt = con.prepareStatement("DROP TABLE "+tableName);
5562N/A pStmt.executeUpdate();
5562N/A }
5562N/A
5562N/A // Check whether the EJB webservice has updated the data in the table.
5562N/A private boolean isDataPresent(Connection con, String tableName) throws Exception {
5562N/A PreparedStatement pStmt = con.prepareStatement("SELECT NAME, EMAIL FROM "+tableName);
5562N/A ResultSet rs = pStmt.executeQuery();
5562N/A while(rs.next()) {
5562N/A String db_Name = rs.getString(1);
5562N/A String db_Email = rs.getString(2);
5562N/A System.out.println("NAME="+db_Name+", EMAIL="+db_Email);
5562N/A rs.close();
5562N/A return true;
5562N/A }
5562N/A return false;
5562N/A }
5562N/A}
5562N/A