EAR with JMS and PgSQL

earFile.png

Setup postgresql in docker

   1 cd /app/jboss-5.1.0.GA/server/default/lib
   2 wget https://jdbc.postgresql.org/download/postgresql-9.3-1103.jdbc3.jar
   3 apt install postgresql-9.5
   4 
   5 su postgres
   6 mkdir -p /var/run/postgresql/9.5-main.pg_stat_tmp/
   7 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
   8 
   9 su postgres
  10 psql
  11 
  12 create user appuser with login password '12345678';
  13 create database app;
  14 grant all privileges on database app to appuser;
  15 \q
  16 
  17 Datasource in Jboss , Local Tx Datasources
  18 PostgresDS
  19 org.postgresql.Driver
  20 jdbc:postgresql://localhost/app?user=appuser&password=12345678
  21 
  22 /app/jboss-5.1.0.GA/server/default/deploy# cat PostgresDS-ds.xml 
  23 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  24 <datasources>
  25     <local-tx-datasource>
  26         <jndi-name>PostgresDS</jndi-name>
  27         <rar-name>jboss-local-jdbc.rar</rar-name>
  28         <use-java-context>true</use-java-context>
  29         <connection-definition>javax.sql.DataSource</connection-definition>
  30         <jmx-invoker-name>jboss:service=invoker,type=jrmp</jmx-invoker-name>
  31         <min-pool-size>0</min-pool-size>
  32         <max-pool-size>10</max-pool-size>
  33         <blocking-timeout-millis>30000</blocking-timeout-millis>
  34         <idle-timeout-minutes>30</idle-timeout-minutes>
  35         <prefill>false</prefill>
  36         <background-validation>false</background-validation>
  37         <background-validation-millis>0</background-validation-millis>
  38         <validate-on-match>true</validate-on-match>
  39         <statistics-formatter>org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter</statistics-formatter>
  40         <isSameRM-override-value>false</isSameRM-override-value>
  41         <allocation-retry>0</allocation-retry>
  42         <allocation-retry-wait-millis>5000</allocation-retry-wait-millis>
  43         <security-domain xsi:type="securityMetaData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
  44         <metadata/>
  45         <local-transaction/>
  46         <prepared-statement-cache-size>0</prepared-statement-cache-size>
  47         <share-prepared-statements>false</share-prepared-statements>
  48         <set-tx-query-timeout>false</set-tx-query-timeout>
  49         <query-timeout>0</query-timeout>
  50         <use-try-lock>0</use-try-lock>
  51         <driver-class>org.postgresql.Driver</driver-class>
  52         <connection-url>jdbc:postgresql://localhost/app?user=appuser&amp;password=12345678</connection-url>
  53     </local-tx-datasource>
  54 </datasources>

Create JMS queue

   1 mkdir -p /app/jboss-5.1.0.GA/server/default/deploy/queue
   2 nano /app/jboss-5.1.0.GA/server/default/deploy/queue/ContactsQueue-service.xml 
   3 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   4 <server>
   5     <mbean xmbean-dd="xmdesc/Queue-xmbean.xml" name="jboss.messaging.destination:service=Queue,name=ContactsQueue" code="org.jboss.jms.server.destination.QueueService">
   6         <attribute name="JNDIName">/queue/ContactsQueue</attribute>
   7         <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
   8         <depends>jboss.messaging:service=PostOffice</depends>
   9     </mbean>
  10 </server>

Structure

.
├── deploy_to_target.sh
├── earcontacts
│   └── pom.xml
├── ejbcontacts
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── org
│           │       └── allowed
│           │           └── bitarus
│           │               ├── ContactEntity.java
│           │               ├── ContactsLocal.java
│           │               ├── ContactsMDB.java
│           │               ├── ContactsRemote.java
│           │               └── ContactsWS.java
│           └── resources
│               └── META-INF
│                   ├── ejb-jar.xml
│                   └── persistence.xml
├── pom.xml
└── warcontacts
    ├── pom.xml
    └── src
        └── main
            ├── java
            │   └── org
            │       └── allowed
            │           └── bitarus
            │               └── Handler.java
            └── webapp
                ├── add.jsp
                ├── css
                │   ├── bootstrap.min.css
                │   └── warcontacts.css
                ├── delete.jsp
                ├── head.jsp
                ├── index.jsp
                ├── js
                │   ├── bootstrap.min.js
                │   └── jquery-3.5.1.min.js
                ├── nav.jsp
                ├── update.jsp
                ├── view.jsp
                └── WEB-INF
                    └── web.xml

deploy_to_target.sh

   1 #!/bin/sh
   2 rm ../contacts.tgz
   3 tar cvzf ../contacts.tgz ../contacts
   4 cp ../contacts.tgz ../target/
   5 docker exec -it jboss5_container /bin/sh -c 'cd /app;rm -rf contacts; tar xvzf contacts.tgz'
   6 docker exec -it jboss5_container /bin/sh -c 'cd /app/contacts; rm  /app/jboss-5.1.0.GA/server/default/deploy/earcontacts-0.0.1.ear; mvn clean install;'
   7 docker exec -it jboss5_container /bin/sh -c 'cp  /app/contacts/earcontacts/target/earcontacts-0.0.1.ear /app/jboss-5.1.0.GA/server/default/deploy'

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   4                       https://maven.apache.org/xsd/maven-4.0.0.xsd">
   5         <modelVersion>4.0.0</modelVersion>
   6         <groupId>org.allowed.bitarus</groupId>
   7         <artifactId>contacts</artifactId>
   8         <version>1.0</version>
   9         <packaging>pom</packaging>
  10         <modules>
  11                 <module>ejbcontacts</module>
  12                 <module>warcontacts</module>
  13                 <module>earcontacts</module>
  14         </modules>
  15 </project>

earcontacts/pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>earcontacts</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>ear</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>org.allowed.bitarus</groupId>
  12                         <artifactId>warcontacts</artifactId>
  13                         <version>0.0.1</version>
  14                         <type>war</type>
  15                 </dependency>
  16                 <dependency>
  17                         <groupId>org.allowed.bitarus</groupId>
  18                         <artifactId>ejbcontacts</artifactId>
  19                         <version>0.0.1</version>
  20                         <type>ejb</type>
  21                 </dependency>
  22         </dependencies>
  23         <build>
  24                 <plugins>
  25                         <plugin>
  26                                 <groupId>org.apache.maven.plugins</groupId>
  27                                 <artifactId>maven-ear-plugin</artifactId>
  28                                 <version>2.10.1</version>
  29                                 <configuration>
  30                                         <version>5</version>
  31                                         <defaultLibBundleDir>lib</defaultLibBundleDir>
  32                                         <filtering>true</filtering>
  33                                         <modules>
  34                                                 <ejbModule>
  35                                                         <groupId>org.allowed.bitarus</groupId>
  36                                                         <artifactId>ejbcontacts</artifactId>
  37                                                         <bundleDir>/</bundleDir>
  38                                                 </ejbModule>
  39                                                 <webModule>
  40                                                         <groupId>org.allowed.bitarus</groupId>
  41                                                         <artifactId>warcontacts</artifactId>
  42                                                         <bundleDir>/</bundleDir>
  43                                                 </webModule>
  44                                         </modules>
  45                                 </configuration>
  46                         </plugin>
  47                         <plugin>
  48                                 <artifactId>maven-compiler-plugin</artifactId>
  49                                 <configuration>
  50                                         <source>1.5</source>
  51                                         <target>1.5</target>
  52                                 </configuration>
  53                         </plugin>
  54                 </plugins>
  55         </build>
  56 </project>

ejbcontacts/pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>ejbcontacts</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>ejb</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>javaee</groupId>
  12                         <artifactId>javaee-api</artifactId>
  13                         <version>5</version>
  14                         <scope>provided</scope>
  15                 </dependency>
  16         </dependencies>
  17         <build>
  18                 <plugins>
  19                         <plugin>
  20                                 <artifactId>maven-compiler-plugin</artifactId>
  21                                 <configuration>
  22                                         <source>1.5</source>
  23                                         <target>1.5</target>
  24                                 </configuration>
  25                         </plugin>
  26                 </plugins>
  27         </build>
  28 </project>

ejbcontacts/src/main/java/org/allowed/bitarus/ContactEntity.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.Date;
   4 import javax.persistence.Id;
   5 import javax.persistence.Entity;
   6 import javax.persistence.Column;
   7 import javax.persistence.GeneratedValue;
   8 import javax.persistence.GenerationType;
   9 import javax.persistence.TemporalType;
  10 import javax.persistence.Temporal;
  11 import javax.persistence.NamedQueries;
  12 import javax.persistence.NamedQuery;
  13 
  14 @Entity
  15 @NamedQueries( { @NamedQuery(name="Contacts.byId",query="select a from ContactEntity a where a.id = :id")   } )
  16 public class ContactEntity{
  17     @Id
  18     @GeneratedValue(strategy = GenerationType.IDENTITY) 
  19     private long id;
  20 
  21     @Column
  22     @Temporal(TemporalType.TIMESTAMP) 
  23     private Date creationDate;
  24 
  25     @Column
  26     private String firstName;
  27 
  28     @Column
  29     private String lastName;
  30 
  31     @Column
  32     private String address;
  33 
  34     @Column
  35     private String phoneNumber;
  36 
  37     public void setId(long id){this.id=id;}
  38     public long getId(){return this.id;}
  39 
  40     public void setCreationDate(Date creationDate){this.creationDate=creationDate;}
  41     public Date getCreationDate(){return this.creationDate;}
  42 
  43     public void setFirstName(String firstName){this.firstName=firstName;}
  44     public String getFirstName(){return this.firstName;}
  45 
  46     public void setLastName(String lastName){this.lastName=lastName;}
  47     public String  getLastName(){return this.lastName;}
  48 
  49     public void setAddress(String address){this.address=address;}
  50     public String getAddress(){return this.address;}
  51 
  52     public void setPhoneNumber(String phoneNumber){this.phoneNumber=phoneNumber;}
  53     public String getPhoneNumber(){return this.phoneNumber;}
  54 
  55 }

ejbcontacts/src/main/java/org/allowed/bitarus/ContactsLocal.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Local;
   4 import java.util.List;
   5 
   6 @Local
   7 public interface ContactsLocal {
   8         void add(String firstName, String lastName, String address, String phoneNumber);
   9 
  10         List<ContactEntity> getAll();
  11 
  12         ContactEntity get(long id);
  13 
  14         void remove(long id);
  15 
  16         void edit(long id, String firstName, String lastName, String address, String phoneNumber);
  17 
  18         void sendMessage(String message);
  19 }

ejbcontacts/src/main/java/org/allowed/bitarus/ContactsMDB.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.ActivationConfigProperty;
   4 import javax.ejb.MessageDriven;
   5 import javax.jms.Message;
   6 import javax.jms.MessageListener;
   7 import javax.jms.TextMessage;
   8 
   9 /*
  10 mkdir -p /app/jboss-5.1.0.GA/server/default/deploy/queue
  11 nano ContactsQueue-service.xml 
  12 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  13 <server>
  14     <mbean xmbean-dd="xmdesc/Queue-xmbean.xml" name="jboss.messaging.destination:service=Queue,name=ContactsQueue" code="org.jboss.jms.server.destination.QueueService">
  15         <attribute name="JNDIName">/queue/ContactsQueue</attribute>
  16         <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
  17         <depends>jboss.messaging:service=PostOffice</depends>
  18     </mbean>
  19 </server> 
  20  * */
  21 @MessageDriven(mappedName = "queue/ContactsQueue", messageListenerInterface = MessageListener.class, activationConfig = {
  22                 @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  23                 @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/ContactsQueue") })
  24 public class ContactsMDB implements MessageListener {
  25 
  26         public void onMessage(Message inMessage) {
  27                 try {
  28                         String text = ((TextMessage) inMessage).getText();
  29                         System.out.println(String.format("Got message %s", text));
  30                 } catch (Exception ex) {
  31                         ex.printStackTrace();
  32                 }
  33         }
  34 }

ejbcontacts/src/main/java/org/allowed/bitarus/ContactsRemote.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.ejb.Remote;
   4 import java.util.List;
   5 
   6 @Remote
   7 public interface ContactsRemote {
   8         void add(String firstName, String lastName, String address, String phoneNumber);
   9 
  10         List<ContactEntity> getAll();
  11 
  12         ContactEntity get(long id);
  13 
  14         void remove(long id);
  15 
  16         void edit(long id, String firstName, String lastName, String address, String phoneNumber);
  17 
  18         void sendMessage(String message);
  19 }

ejbcontacts/src/main/java/org/allowed/bitarus/ContactsWS.java

   1 package org.allowed.bitarus;
   2 
   3 import javax.annotation.Resource;
   4 import javax.ejb.Stateless;
   5 import javax.jms.Connection;
   6 import javax.jms.ConnectionFactory;
   7 import javax.jms.MessageProducer;
   8 import javax.jms.Queue;
   9 import javax.jms.Session;
  10 import javax.jms.TextMessage;
  11 import javax.jws.WebMethod;
  12 import javax.jws.WebService;
  13 import org.allowed.bitarus.ContactsRemote;
  14 import javax.persistence.PersistenceContext;
  15 import javax.persistence.EntityManager;
  16 import java.util.List;
  17 import javax.persistence.NamedQuery;
  18 
  19 @Stateless
  20 @WebService
  21 public class ContactsWS implements ContactsRemote, ContactsLocal {
  22         @PersistenceContext(unitName = "puContactsDefaultDS")
  23         EntityManager em;
  24         @Resource(mappedName = "ConnectionFactory")
  25         private ConnectionFactory connectionFactory;
  26         @Resource(mappedName = "queue/ContactsQueue")
  27         private Queue queue;
  28 
  29         @WebMethod
  30         public void add(String firstName, String lastName, String address, String phoneNumber) {
  31                 ContactEntity c = new ContactEntity();
  32                 c.setFirstName(firstName);
  33                 c.setLastName(lastName);
  34                 c.setAddress(address);
  35                 c.setPhoneNumber(phoneNumber);
  36                 c.setCreationDate(new java.util.Date());
  37                 em.persist(c);
  38         }
  39 
  40         @WebMethod
  41         public List<ContactEntity> getAll() {
  42                 return em.createQuery("select a from ContactEntity a ").getResultList();
  43         }
  44 
  45         @WebMethod
  46         public ContactEntity get(long id) {
  47                 // return (ContactEntity) em.createQuery("select a from ContactEntity a where
  48                 // a.id = :id").setParameter("id", id).getSingleResult();
  49                 return (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult();
  50         }
  51 
  52         @WebMethod
  53         public void remove(long id) {
  54                 ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id)
  55                                 .getSingleResult();
  56                 em.remove(ent);
  57         }
  58 
  59         @WebMethod
  60         public void edit(long id, String firstName, String lastName, String address, String phoneNumber) {
  61                 ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id)
  62                                 .getSingleResult();
  63                 ent.setFirstName(firstName);
  64                 ent.setLastName(lastName);
  65                 ent.setAddress(address);
  66                 ent.setPhoneNumber(phoneNumber);
  67                 em.persist(ent);
  68         }
  69 
  70         @WebMethod
  71         public void sendMessage(String message) {
  72                 try {
  73                         Connection connection = connectionFactory.createConnection();
  74                         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  75                         MessageProducer messageProducer = session.createProducer(queue);
  76                         TextMessage textMessage = session.createTextMessage();
  77                         textMessage.setText(message);
  78                         messageProducer.send(textMessage);
  79                         messageProducer.close();
  80                         session.close();
  81                         connection.close();
  82                 } catch (Exception ex) {
  83                         ex.printStackTrace();
  84                 }
  85         }
  86 }

ejbcontacts/src/main/resources/META-INF/ejb-jar.xml

   1 <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
   4         version="3.0">
   5         <enterprise-beans>
   6                 <session>
   7                         <ejb-name>ContactsWS</ejb-name>
   8                         <business-local>org.allowed.bitarus.ContactsLocal</business-local>
   9                         <business-remote>org.allowed.bitarus.ContactsRemote</business-remote>
  10                         <ejb-class>org.allowed.bitarus.ContactsWS</ejb-class>
  11                         <session-type>Stateless</session-type>
  12                         <transaction-type>Container</transaction-type>
  13                 </session>
  14         </enterprise-beans>
  15 </ejb-jar>

ejbcontacts/src/main/resources/META-INF/persistence.xml

   1 <persistence version="1.0"
   2         xmlns="http://java.sun.com/xml/ns/persistence"
   3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   4         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
   5         <persistence-unit name="puContactsDefaultDS"
   6                 transaction-type="JTA">
   7                 <jta-data-source>java:/PostgresDS</jta-data-source>
   8                 <properties>
   9                         <property name="hibernate.show_sql" value="true" />
  10                         <property name="hibernate.format_sql" value="true" />
  11                         <property name="hibernate.dialect"
  12                                 value="org.hibernate.dialect.PostgreSQLDialect" />
  13                         <property name="hibernate.hbm2ddl.auto" value="update" />
  14                 </properties>
  15         </persistence-unit>
  16 </persistence>

warcontacts/pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0"
   2         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   3         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   4         <modelVersion>4.0.0</modelVersion>
   5         <groupId>org.allowed.bitarus</groupId>
   6         <artifactId>warcontacts</artifactId>
   7         <version>0.0.1</version>
   8         <packaging>war</packaging>
   9         <dependencies>
  10                 <dependency>
  11                         <groupId>javaee</groupId>
  12                         <artifactId>javaee-api</artifactId>
  13                         <version>5</version>
  14                         <scope>provided</scope>
  15                 </dependency>
  16                 <dependency>
  17                         <groupId>org.allowed.bitarus</groupId>
  18                         <artifactId>ejbcontacts</artifactId>
  19                         <version>0.0.1</version>
  20                         <scope>provided</scope>
  21                 </dependency>
  22         </dependencies>
  23         <build>
  24                 <plugins>
  25                         <plugin>
  26                                 <artifactId>maven-compiler-plugin</artifactId>
  27                                 <configuration>
  28                                         <source>1.5</source>
  29                                         <target>1.5</target>
  30                                 </configuration>
  31                         </plugin>
  32                 </plugins>
  33         </build>
  34 </project>

warcontacts/src/main/java/org/allowed/bitarus/Handler.java

   1 package org.allowed.bitarus;
   2 
   3 import java.util.logging.Logger;
   4 import java.util.logging.Level;
   5 import javax.servlet.ServletRequest;
   6 import javax.servlet.ServletResponse;
   7 import javax.servlet.jsp.JspWriter;
   8 import javax.servlet.jsp.PageContext;
   9 import javax.naming.InitialContext;
  10 
  11 import java.io.IOException;
  12 import java.util.List;
  13 import javax.naming.NamingEnumeration;
  14 import javax.naming.NameClassPair;
  15 import javax.servlet.http.HttpServletRequest;
  16 import javax.servlet.http.HttpServletResponse;
  17 import java.util.Map;
  18 
  19 public class Handler {  
  20         private static final String CONTACTS_WS_LOCAL = "earcontacts-0.0.1/ContactsWS/local";
  21         private ServletRequest request;
  22         private ServletResponse response;
  23         private JspWriter out;
  24 
  25         public Handler(ServletRequest request, ServletResponse response, JspWriter out) {
  26                 this.request = request;
  27                 this.response = response;
  28                 this.out = out;
  29         }
  30 
  31         public Handler(PageContext pageContext) {
  32                 this.request = pageContext.getRequest();
  33                 this.response = pageContext.getResponse();
  34                 this.out = (JspWriter) pageContext.getAttribute(PageContext.OUT);
  35 
  36                 HttpServletRequest hsr = (HttpServletRequest) this.request;
  37                 System.out.println(String.format("Current method %s", hsr.getMethod()));
  38                 System.out.println(String.format("Path %s", hsr.getServletPath()));
  39 
  40                 if ("POST".equals(hsr.getMethod())) {
  41                         Map<String, String> params = hsr.getParameterMap();
  42                         for (String key : params.keySet()) {
  43                                 System.out.println(String.format("%s:%s", key, hsr.getParameter(key)));
  44                         }
  45 
  46                         handleAddPost(hsr);
  47                         handleDeletePost(hsr);
  48                         handleUpdatePost(hsr);
  49                 }
  50         }
  51 
  52         private void handleUpdatePost(HttpServletRequest hsr) {
  53                 if ("/update.jsp".equals(hsr.getServletPath())) {
  54                         try {
  55                                 InitialContext context = new InitialContext();
  56                                 ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL);
  57                                 long id = Integer.parseInt(hsr.getParameter("cid"));
  58                                 String firstName = hsr.getParameter("firstname");
  59                                 String lastName = hsr.getParameter("lastname");
  60                                 String address = hsr.getParameter("address");
  61                                 String phoneNumber = hsr.getParameter("phonenumber");
  62                                 contacts.edit(id, firstName, lastName, address, phoneNumber);
  63                                 redirectToIndex(hsr);
  64                         } catch (Exception ex) {
  65                         }
  66                 }
  67         }
  68 
  69         private void handleDeletePost(HttpServletRequest hsr) {
  70                 if ("/delete.jsp".equals(hsr.getServletPath())) {
  71                         try {
  72                                 InitialContext context = new InitialContext();
  73                                 ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL);
  74                                 contacts.remove(Integer.parseInt(hsr.getParameter("id")));
  75                                 redirectToIndex(hsr);
  76                         } catch (Exception ex) {
  77                         }
  78                 }
  79         }
  80 
  81         private void handleAddPost(HttpServletRequest hsr) {
  82                 if ("/add.jsp".equals(hsr.getServletPath())) {
  83                         try {
  84                                 InitialContext context = new InitialContext();
  85                                 ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL);
  86                                 contacts.add(hsr.getParameter("firstname"), hsr.getParameter("lastname"), hsr.getParameter("address"),
  87                                                 hsr.getParameter("phonenumber"));
  88                                 redirectToIndex(hsr);
  89                         } catch (Exception ex) {
  90                         }
  91                 }
  92         }
  93 
  94         private void redirectToIndex(HttpServletRequest hsr) throws IOException {
  95                 ((HttpServletResponse) this.response).sendRedirect(hsr.getContextPath() + "/index.jsp");
  96         }
  97 
  98         public void writeHandler() {
  99                 try {
 100                         this.out.println(String.format("<p>Handler up %s</p>", this.request.getParameter("user")));
 101                 } catch (Exception ex) {
 102                 }
 103         }
 104 
 105         public void writeIP() {
 106                 try {
 107                         this.out.print("!!! Your IP address is " + this.request.getRemoteAddr());
 108                 } catch (Exception ex) {
 109                 }
 110         }
 111 
 112         public List<ContactEntity> getAllContacts() {
 113                 List<ContactEntity> ret = null;
 114                 try {
 115                         InitialContext context = new InitialContext();
 116 
 117                         ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL);
 118                         ret = contacts.getAll();
 119                 } catch (Exception ex) {
 120                 }
 121                 return ret;
 122         }
 123 
 124         public ContactEntity getContact(long id) {
 125 
 126                 ContactEntity ret = null;
 127                 try {
 128                         InitialContext context = new InitialContext();
 129 
 130                         ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL);
 131                         ret = contacts.get(id);
 132                 } catch (Exception ex) {
 133                 }
 134 
 135                 return ret;
 136         }
 137 }

warcontacts/src/main/webapp/add.jsp

   1 <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %>
   2 <% Handler h = new Handler(pageContext); %>
   3 <html>
   4     <head>
   5         <jsp:include page="head.jsp" />  
   6     </head>
   7     <body>       
   8         <jsp:include page="nav.jsp" />
   9         <div class="container topspace">
  10             <div class="row"> 
  11                 <div class="col-sm-2"></div>
  12                 <div class="col-sm-8">    
  13                     <form  action="add.jsp" method="post"  >
  14                         <label for="firstname">First name:</label><br/>
  15                         <input type="text" id="firstname" name="firstname" class="form-control"><br/>
  16                         <label for="lastname">Last name:</label><br/>
  17                         <input type="text" id="lastname" name="lastname" class="form-control"><br/>
  18                         <label for="address">Address::</label><br/>
  19                         <input type="text" id="Address:" name="address" class="form-control"><br/>
  20                         <label for="phonenumber">Phone number:</label><br/>
  21                         <input type="text" id="phonenumber" name="phonenumber" class="form-control"><br/>
  22                         <input type="submit" value="Add"  class="form-control btn btn-primary button"/>
  23                     </form>    
  24                 </div> 
  25                 <div class="col-sm-2"></div>   
  26             </div>
  27         </div>          
  28     </body>
  29 </html>

warcontacts/src/main/webapp/css/bootstrap.min.css

warcontacts/src/main/webapp/css/warcontacts.css

   1 .button{margin-top:2px; margin-bottom:2px;}
   2 .topspace { padding-top: 10px; }    2020-06-24T18:20:45 [vitor@debian:3 ~/JBoss5Docker/contacts]

warcontacts/src/main/webapp/delete.jsp

   1 <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %>
   2 <% Handler h = new Handler(pageContext); %>
   3 <html>
   4     <head>
   5         <jsp:include page="head.jsp" />  
   6     </head>
   7     <body>       
   8         <% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) );  %> 
   9         <jsp:include page="nav.jsp" />
  10         <div class="container  topspace">
  11             <div class="row"> 
  12                 <div class="col-sm-2"></div>
  13                 <div class="col-sm-8">         
  14                     <% if(ci!=null) { %>
  15                         <p><strong>Id:</strong>  <%= ci.getId() %></p>
  16                         <p><strong>First name:</strong>  <%= ci.getFirstName() %></p>
  17                         <p><strong>Last name:</strong>  <%= ci.getLastName() %></p>
  18                         <p><strong>Creation date:</strong> <%= ci.getCreationDate() %></p>
  19                         <p><strong>Address:</strong>  <%= ci.getAddress() %></p>
  20                         <p><strong>Phone:</strong>  <%= ci.getPhoneNumber() %></p>
  21 
  22                         <form action="delete.jsp" method="post">
  23                             <input type="hidden" id="id" name="id" value="<%=ci.getId()%>"  />
  24                             <input type="submit" class="form-control btn btn-primary button" value="Delete">
  25                         </form>
  26                     <% }  %>
  27                 </div> 
  28                 <div class="col-sm-2"></div>   
  29             </div>
  30         </div>              
  31     </body>
  32 </html>

warcontacts/src/main/webapp/head.jsp

   1   <link rel="stylesheet" href="css/bootstrap.min.css"/>
   2   <link rel="stylesheet" href="css/warcontacts.css"/>
   3   <script src="js/jquery-3.5.1.min.js"></script>
   4   <script src="js/bootstrap.min.js" ></script>

warcontacts/src/main/webapp/index.jsp

   1 <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %>
   2 <% Handler h = new Handler(pageContext); %>
   3 <html>
   4         <head>
   5                 <jsp:include page="head.jsp" />  
   6         </head>
   7     <body>        
   8         <jsp:include page="nav.jsp" />      
   9                 <div class="container topspace">
  10                 <div class="row"> 
  11                         <div class="col-sm-2"></div>
  12                         <div class="col-sm-8">
  13                                 <% List<ContactEntity> contacts = h.getAllContacts(); %>
  14                                 <a href="add.jsp" class="btn btn-primary button">Add</a>
  15                                 <table class="table table-stripped" >
  16                                     <tr>
  17                                                         <th>FirstName</th>
  18                                                         <th>LastName</th>
  19                                                         <th>Actions</th>
  20                                 </tr>
  21                                         <% for(int i=0; i<contacts.size() ;i++){  %>
  22                                         <tr>
  23                                                                 <% ContactEntity contact = ((ContactEntity)contacts.get(i));  %>
  24                                                                 <td><%= contact.getFirstName() %></td>
  25                                                                 <td><%= contact.getLastName() %></td>
  26                                                                 <td>
  27                                                                 <a href="view.jsp?id=<%=contact.getId()%>" class="btn btn-primary button">View</a>
  28                                                                         <a href="update.jsp?id=<%=contact.getId()%>" class="btn btn-primary button">Update</a>
  29                                                                         <a href="delete.jsp?id=<%=contact.getId()%>" class="btn btn-primary button">Delete</a>     
  30                                                                 </td>
  31                                         </tr>
  32                                         <%}%>
  33                                </table>
  34                         </div> 
  35                         <div class="col-sm-2"></div>   
  36                 </div>
  37                 </div> 
  38         </body>
  39 </html>

warcontacts/src/main/webapp/js/bootstrap.min.js

warcontacts/src/main/webapp/js/jquery-3.5.1.min.js

warcontacts/src/main/webapp/nav.jsp

   1 <nav class="navbar navbar-dark bg-dark navbar-fixed-top">
   2     <div class="container">
   3         <div class="navbar-header">
   4             <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
   5                 <span class="sr-only">Toggle navigation</span>
   6                 <span class="icon-bar"></span>
   7                 <span class="icon-bar"></span>
   8                 <span class="icon-bar"></span>
   9             </button>
  10             <a class="navbar-brand" href="index.jsp">Contacts</a>
  11         </div>
  12         <div id="navbar" class="collapse navbar-collapse">
  13           <ul class="nav navbar-nav">
  14               <li class="active"><a href="index.jsp">List</a></li>
  15           </ul>
  16         </div>
  17     </div>
  18 </nav>

warcontacts/src/main/webapp/update.jsp

   1 <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %>
   2 <% Handler h = new Handler(pageContext); %>
   3 <html>
   4     <head>
   5         <jsp:include page="head.jsp" />  
   6     </head>
   7     <body>       
   8         <jsp:include page="nav.jsp" />
   9         <div class="container topspace">
  10             <div class="row"> 
  11                 <div class="col-sm-2"></div>
  12                 <div class="col-sm-8">              
  13                                         <% if( request.getParameter("id") != null ) { %>
  14                                         <% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) );  %>
  15                                                 <form  action="update.jsp" method="post"  >
  16                                                         <label for="firstname">First name:</label><br/>
  17                                                         <input class="form-control" type="text" id="firstname" name="firstname" value="<%= ci.getFirstName() %>"/><br/>
  18                                                         <label for="lastname">Last name:</label><br/>
  19                                                         <input class="form-control" type="text" id="lastname" name="lastname" value="<%= ci.getLastName() %>" /><br/>
  20                                                         <label for="address">Address::</label><br/>
  21                                                         <input class="form-control" type="text" id="Address:" name="address" value="<%= ci.getAddress() %>"/><br/>
  22                                                         <label for="phonenumber">Phone number:</label><br/>
  23                                                         <input class="form-control" type="text" id="phonenumber" name="phonenumber" value="<%= ci.getPhoneNumber() %>"/><br/>
  24                                                         <input class="form-control" type="hidden" id="cid" name="cid" value="<%=ci.getId()%>"/> 
  25                                                         <input type="submit" value="Update" class="form-control btn btn-primary button" >
  26                                                 </form>
  27                                         <% } %> 
  28                 </div> 
  29                 <div class="col-sm-2"></div>   
  30             </div>
  31         </div>                   
  32     </body>
  33 </html>

warcontacts/src/main/webapp/view.jsp

   1 <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %>
   2 <% Handler h = new Handler(pageContext); %>
   3 <html>
   4     <head>
   5         <jsp:include page="head.jsp" />  
   6     </head>
   7     <body>       
   8         <jsp:include page="nav.jsp" />
   9         <div class="container topspace">
  10             <div class="row"> 
  11                 <div class="col-sm-2"></div>
  12                 <div class="col-sm-8">    
  13                     <% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) );  %> 
  14                     <p><strong>Id:</strong>  <%= ci.getId() %></p>
  15                     <p><strong>First name:</strong>  <%= ci.getFirstName() %></p>
  16                     <p><strong>Last name:</strong>  <%= ci.getLastName() %></p>
  17                     <p><strong>Creation date:</strong> <%= ci.getCreationDate() %></p>
  18                     <p><strong>Address:</strong>  <%= ci.getAddress() %></p>
  19                     <p><strong>Phone:</strong>  <%= ci.getPhoneNumber() %></p>
  20                 </div> 
  21                 <div class="col-sm-2"></div>   
  22             </div>
  23         </div>         
  24     </body>
  25 </html>

warcontacts/src/main/webapp/WEB-INF/web.xml

   1 <?xml version="1.0"?>
   2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
   3         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   4         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   5 </web-app>

Java/JBoss5/EAR (last edited 2021-01-15 16:19:49 by localhost)