<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-3595585352812650883</atom:id><lastBuildDate>Fri, 08 Nov 2024 15:38:30 +0000</lastBuildDate><category>J2EE</category><category>Java</category><category>Email</category><category>Framework</category><category>JavaMail</category><category>Design Patterns</category><category>Eclipse</category><category>mysql</category><category>Annotation</category><category>Apache Commons</category><category>FileUpload</category><category>Hibernate</category><category>Interoperability</category><category>JNI</category><category>MVC</category><category>SOA</category><category>Servicemix</category><category>Validator</category><category>installation</category><category>linux</category><title>The Java Trail</title><description></description><link>http://thejavatrail.blogspot.com/</link><managingEditor>noreply@blogger.com (Jay Rajani)</managingEditor><generator>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-7369017130745207579</guid><pubDate>Sat, 01 Sep 2012 18:28:00 +0000</pubDate><atom:updated>2012-09-03T00:19:18.155+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Apache Commons</category><category domain="http://www.blogger.com/atom/ns#">FileUpload</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><title>Form-based File Upload in J2EE Web Application Part 1</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Many times we may have a file upload control on view asking user to upload a file or an image from local machine. HTML provides&amp;nbsp; a file control to upload a file. But on server side, we need to write a code to save it on a file server.&lt;br /&gt;
&lt;br /&gt;
In J2EE, file upload functionality can be best achieved using Commons File Upload. A file upload request comprises an ordered list of &lt;i&gt;items&lt;/i&gt; that
      are encoded according to RFC-1867. FileUpload can parse such a request
      and provide your application with a list of the individual uploaded
      items. Each such item implements the &lt;code&gt;FileItem&lt;/code&gt; interface,
      regardless of its underlying implementation.
    &lt;br /&gt;
&lt;br /&gt;
Each file item has a number of properties that might be of interest for
      your application. For example, every item has a name and a content type,
      and can provide an &lt;code&gt;InputStream&lt;/code&gt; to access its data. On the
      other hand, you may need to process items differently, depending upon
      whether the item is a regular form field - that is, the data came from
      an ordinary text box or similar HTML field - or an uploaded file. The
      &lt;code&gt;FileItem&lt;/code&gt; interface provides the methods to make such a
      determination, and to access the data in the most appropriate manner. &lt;br /&gt;
&lt;br /&gt;
Before you can work with the uploaded items, of course, you need to parse
      the request itself. Ensuring that the request is actually a file upload
      request is straightforward, but FileUpload makes it simplicity itself, by
      providing a static method to do just that.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s make a very simple application to upload a file using JSP/Servlets as shown in following image.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoJMxGQoOTNK8hhCwVxzGmyypRWby5Mczvm05rMTyKr67F8oVTVgwsqHZauUHpou97NC5MJps2oOBIJobzrBmeT9XBq-T3vmRj5YI7KBoysj30UadCrY7s2Dv5XsckRExAnD7TtJbOYDXG/s1600/File+upload+2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoJMxGQoOTNK8hhCwVxzGmyypRWby5Mczvm05rMTyKr67F8oVTVgwsqHZauUHpou97NC5MJps2oOBIJobzrBmeT9XBq-T3vmRj5YI7KBoysj30UadCrY7s2Dv5XsckRExAnD7TtJbOYDXG/s1600/File+upload+2.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Prerequisites&lt;/h3&gt;
&lt;br /&gt;
For this tutorial, we will need the following tools: (The older or newer
 version should also works). Moreover, basic Java knowledge is assumed.&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.eclipse.org/downloads/&quot; target=&quot;_blank&quot;&gt;Eclipse IDE for Java EE Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tomcat.apache.org/index.html&quot; target=&quot;_blank&quot;&gt;Apache Tomcat v6 or later&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://commons.apache.org/io/&quot; target=&quot;_blank&quot;&gt;Apache Commons IO&lt;/a&gt;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://commons.apache.org/fileupload/&quot; target=&quot;_blank&quot;&gt;Apache Commons File Upload&lt;/a&gt; &lt;/li&gt;
&lt;/ol&gt;
Apache commons IO and File Upload jars has to be put in your web application lib folder. Following is the folder structure for this example.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdZ-M5doH7ZrsIWpXUhiMSzpH1lkKedVm0S64iH5oWOnD7L1kisku-H6RsnKidX-xf5mW8Ia5i_XUlFDGZtXUgNPok8PHz3Mbj6RdFHlAhfrlVFH19w9VhACs6EWLDUENavV0T6TBrNkK7/s1600/File+upload.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjdZ-M5doH7ZrsIWpXUhiMSzpH1lkKedVm0S64iH5oWOnD7L1kisku-H6RsnKidX-xf5mW8Ia5i_XUlFDGZtXUgNPok8PHz3Mbj6RdFHlAhfrlVFH19w9VhACs6EWLDUENavV0T6TBrNkK7/s1600/File+upload.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Create a View using JSP &lt;/h3&gt;
&lt;br /&gt;
The view includes a simple file upload control with submit button. Following is the code:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;&amp;lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
    pageEncoding=&quot;ISO-8859-1&quot;%&amp;gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;&amp;gt;
&amp;lt;title&amp;gt;Insert title here&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

&amp;lt;form action=&quot;upload.do&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot;&amp;gt;
 Select file to upload: &amp;lt;input type=&quot;file&quot; name=&quot;selectFile&quot; /&amp;gt; &amp;lt;br /&amp;gt;
 &amp;lt;input type=&quot;submit&quot;&amp;gt; 
&amp;lt;/form&amp;gt;


&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&amp;nbsp;&lt;/pre&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&amp;nbsp;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating a Controller&amp;nbsp;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Controller will receive the multipart request. It will save the file at folder location configured in context parameters of the web application. It also creates a map of regular form parameters if any. We dont have any regular form parameter in this example. But that&#39;s put for your reference only.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;package org.avid.upload.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

/**
 * @author Jay Rajani
 * 
 * Servlet implementation class UploadController
 */
public class UploadController extends HttpServlet {
 private static final long serialVersionUID = 1L;
 
 private String folderLocation = null; 
 
 @Override
 public void init() throws ServletException {
  super.init();
  this.folderLocation = getServletContext().getInitParameter(&quot;UPLOAD_FOLDER&quot;);
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  HashMap&amp;lt;String, String&amp;gt; formParams = new HashMap&amp;lt;String, String&amp;gt;();
  
  // Check that we have a file upload request
  boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  
  try{
   if (isMultipart){
    
    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);

    // Parse the request
    List&amp;lt;FileItem&amp;gt; items = upload.parseRequest(request);
    
    // Process the uploaded items
    Iterator&amp;lt;FileItem&amp;gt; iter = items.iterator();
    while (iter.hasNext()) {
        FileItem item = (FileItem) iter.next();

        if (item.isFormField()) {
         
         // Process a regular form field
         formParams.put(item.getFieldName(), item.getString());
        } else {
         
         // Process a file upload
         String fileName = item.getName();
         
         File uploadedFile = new File(folderLocation+File.separator+fileName);
            item.write(uploadedFile);
        }
    }
   }
  }catch(FileUploadException fue){
   fue.printStackTrace();
   throw new ServletException(fue.getMessage());
  }catch(Exception e){
   e.printStackTrace();
   throw new ServletException(e.getMessage());
  }
 }

}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Deployment Descriptor&lt;/h3&gt;
&lt;br /&gt;
Here is the snippet of final deployment descriptor.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;web-app id=&quot;WebApp_ID&quot; version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&amp;gt;
 
 &amp;lt;context-param&amp;gt;
  &amp;lt;param-name&amp;gt;UPLOAD_FOLDER&amp;lt;/param-name&amp;gt;
  &amp;lt;param-value&amp;gt;E:/temp&amp;lt;/param-value&amp;gt;
 &amp;lt;/context-param&amp;gt;
 
 &amp;lt;display-name&amp;gt;FileUpload&amp;lt;/display-name&amp;gt;
 
 &amp;lt;servlet&amp;gt;
  &amp;lt;description&amp;gt;
  &amp;lt;/description&amp;gt;
  &amp;lt;display-name&amp;gt;UploadController&amp;lt;/display-name&amp;gt;
  &amp;lt;servlet-name&amp;gt;UploadController&amp;lt;/servlet-name&amp;gt;
  &amp;lt;servlet-class&amp;gt;
  org.avid.upload.controller.UploadController&amp;lt;/servlet-class&amp;gt;
 &amp;lt;/servlet&amp;gt;
 &amp;lt;servlet-mapping&amp;gt;
  &amp;lt;servlet-name&amp;gt;UploadController&amp;lt;/servlet-name&amp;gt;
  &amp;lt;url-pattern&amp;gt;/upload.do&amp;lt;/url-pattern&amp;gt;
 &amp;lt;/servlet-mapping&amp;gt;

&amp;lt;/web-app&amp;gt;
&lt;/pre&gt;
&lt;br /&gt;
The code will save the file to location specified in web.xml. It can be a local folder or shared folder or ftp location depending on your requirement.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/09/upload-file-or-images-in-j2ee-web.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoJMxGQoOTNK8hhCwVxzGmyypRWby5Mczvm05rMTyKr67F8oVTVgwsqHZauUHpou97NC5MJps2oOBIJobzrBmeT9XBq-T3vmRj5YI7KBoysj30UadCrY7s2Dv5XsckRExAnD7TtJbOYDXG/s72-c/File+upload+2.jpg" height="72" width="72"/><thr:total>4</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-7157199281298083930</guid><pubDate>Mon, 27 Aug 2012 20:40:00 +0000</pubDate><atom:updated>2012-09-02T23:43:37.051+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Design Patterns</category><category domain="http://www.blogger.com/atom/ns#">Eclipse</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><category domain="http://www.blogger.com/atom/ns#">MVC</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><title>Step by step guide - Developing a MVC application using J2EE and MySQL</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
In this tutorial, we will create a simple J2EE application that performs CRUD (Create Read Update Delete) operations for User Management using Jsp, Servlet and MySQL.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Prerequisites&lt;/b&gt;&lt;/h3&gt;
&lt;br /&gt;
For this tutorial, we will need the following tools: (The older or newer version should also works). Moreover, basic Java knowledge is assumed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsMjfYyzFseehnV5vETPVOvBTtqsZF-r5iHz2JhepGweiO_vXfglHrWNApWOjhpqfpHz0NPNpwADUz_RvEa4YWftly8AXzQ8Zil8Jh_hhVFEKcwH_wpS-JlXGhTKXS80xKhw3SMlhvaJqX/s1600/mysql-java-jdbc.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;130&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsMjfYyzFseehnV5vETPVOvBTtqsZF-r5iHz2JhepGweiO_vXfglHrWNApWOjhpqfpHz0NPNpwADUz_RvEa4YWftly8AXzQ8Zil8Jh_hhVFEKcwH_wpS-JlXGhTKXS80xKhw3SMlhvaJqX/s200/mysql-java-jdbc.jpg&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;
&lt;li&gt;&lt;a href=&quot;http://www.eclipse.org/downloads/&quot; target=&quot;_blank&quot;&gt;Eclipse IDE for Java EE Developers&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://tomcat.apache.org/index.html&quot; target=&quot;_blank&quot;&gt;Apache Tomcat v6 or later&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://dev.mysql.com/downloads/&quot; target=&quot;_blank&quot;&gt;MySQL Community Server and MySQL Workbench (GUI Tool)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://dev.mysql.com/downloads/connector/j/&quot; target=&quot;_blank&quot;&gt;MySQL Connector for Java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;jstl.jar&lt;/b&gt; and &lt;b&gt;standard.jar&lt;/b&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
You can get the required jars from your Tomcat. Check in this directory : (your tomcat directory)—&amp;gt;apache-tomcat-7.0.26-windows-x86—&amp;gt;apache-tomcat-7.0.26—&amp;gt;webapps—&amp;gt;examples—&amp;gt;WEB-INF—&amp;gt;lib&lt;br /&gt;
&lt;br /&gt;
I will tell you where you should put these jars later.&lt;br /&gt;
&lt;br /&gt;
jQuery for javascript capability. In this case, we only use it for the datepicker component&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Create the database&lt;/b&gt;&lt;/h3&gt;
&lt;br /&gt;
First, lets create the database and table for User using the following SQL scripts:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;sql&quot; name=&quot;code&quot;&gt;
create database UserDB;&lt;br /&gt;
use UserDB;&lt;br /&gt;
grant all on UserDB.* to &#39;admin&#39;@&#39;localhost&#39; identified by &#39;test&#39;;&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE UserDB.`users` (&lt;br /&gt;
&amp;nbsp; `userid` int(11) NOT NULL AUTO_INCREMENT,&lt;br /&gt;
&amp;nbsp; `firstname` varchar(45) DEFAULT NULL,&lt;br /&gt;
&amp;nbsp; `lastname` varchar(45) DEFAULT NULL,&lt;br /&gt;
&amp;nbsp; `dob` date DEFAULT NULL,&lt;br /&gt;
&amp;nbsp; `email` varchar(100) DEFAULT NULL,&lt;br /&gt;
&amp;nbsp; PRIMARY KEY (`userid`)&lt;br /&gt;
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Setting up Eclipse and Tomcat&lt;/h3&gt;
&lt;br /&gt;
Go to eclipse. Before we create a new project for our application, we need to setup the server. Select File—&amp;gt;New—&amp;gt;Other. From the tree, Select Server.&lt;br /&gt;
&lt;br /&gt;
Choose Apache—&amp;gt;Tomcat v7.0 Server and set the runtime environment.&lt;br /&gt;
&lt;br /&gt;
Next, create a new project. Select File—&amp;gt;New—&amp;gt;Dynamic Web Project.&lt;br /&gt;
&lt;br /&gt;
Enter “SimpleJspServletDB” as the project name. Select target runtime to Apache Tomcat v7.0 which we already setup before. Click Finish.&lt;br /&gt;
&lt;br /&gt;
Copy the standard.jar, mysql-connector jar and jstl jar to WEB-INF—&amp;gt;lib folder.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating Package structure&lt;/h3&gt;
&lt;br /&gt;
Create four packages in the src folder.&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;b&gt;org.avid.controller&lt;/b&gt;: contains the servlets&lt;/li&gt;
&lt;li&gt;&lt;b&gt;org.avid.dao&lt;/b&gt;: contains the logic for database operation&lt;/li&gt;
&lt;li&gt;&lt;b&gt;org.avid.model&lt;/b&gt;: contains the POJO (Plain Old Java Object). Each class in this package represents the database table. For this tutorial, however, we only have one table.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;org.avid.util&lt;/b&gt; : contains the class for initiating database connection&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating Model - POJOs&lt;/h3&gt;
&lt;br /&gt;
Next, create a new Java class. in org.avid.model folder. Name it “User.java” and insert these following codes. Each of the variables in this class represents the field in USERS table in our database.&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
package org.avid.model;&lt;br /&gt;
&lt;br /&gt;
import java.util.Date;&lt;br /&gt;
&lt;br /&gt;
public class User {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private int userid;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private String firstName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private String lastName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private Date dob;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private String email;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public int getUserid() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return userid;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void setUserid(int userid) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.userid = userid;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public String getFirstName() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return firstName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void setFirstName(String firstName) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.firstName = firstName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public String getLastName() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return lastName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void setLastName(String lastName) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.lastName = lastName;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public Date getDob() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return dob;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void setDob(Date dob) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.dob = dob;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public String getEmail() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return email;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void setEmail(String email) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; this.email = email;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; @Override&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public String toString() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return &quot;User [userid=&quot; + userid + &quot;, firstName=&quot; + firstName&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &quot;, lastName=&quot; + lastName + &quot;, dob=&quot; + dob + &quot;, email=&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + email + &quot;]&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; } &amp;nbsp; &lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating DB Connection Utility&lt;/h3&gt;
&lt;br /&gt;
Create a new class in org.avid.util package and name it DbUtil.java. This class handles the database connection to our MySQL server. In this class, we read a .properties file which contains the information necessary for the connection.&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
package org.avid.util;&lt;br /&gt;
&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.InputStream;&lt;br /&gt;
import java.sql.Connection;&lt;br /&gt;
import java.sql.DriverManager;&lt;br /&gt;
import java.sql.SQLException;&lt;br /&gt;
import java.util.Properties;&lt;br /&gt;
&lt;br /&gt;
public class DbUtil {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private static Connection connection = null;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public static Connection getConnection() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (connection != null)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return connection;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Properties prop = new Properties();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; InputStream inputStream = DbUtil.class.getClassLoader().getResourceAsStream(&quot;/db.properties&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prop.load(inputStream);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String driver = prop.getProperty(&quot;driver&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String url = prop.getProperty(&quot;url&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String user = prop.getProperty(&quot;user&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String password = prop.getProperty(&quot;password&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Class.forName(driver);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; connection = DriverManager.getConnection(url, user, password);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (ClassNotFoundException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (FileNotFoundException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (IOException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return connection;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
Create the properties file directly under the src folder. Create a new file, name it db.properties. Put the following information inside.&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
driver=com.mysql.jdbc.Driver&lt;br /&gt;
url=jdbc:mysql://localhost:3306/test&lt;br /&gt;
user=admin&lt;br /&gt;
password=test&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating Data Access Object (DAO)&lt;/h3&gt;
&lt;br /&gt;
Next, create a new class in org.avid.dao package, name it UserDao.java. Dao stands for Data Access Object. It contains the logic for &amp;nbsp;database operation.&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
package org.avid.dao;&lt;br /&gt;
&lt;br /&gt;
import java.sql.Connection;&lt;br /&gt;
import java.sql.PreparedStatement;&lt;br /&gt;
import java.sql.ResultSet;&lt;br /&gt;
import java.sql.SQLException;&lt;br /&gt;
import java.sql.Statement;&lt;br /&gt;
import java.util.ArrayList;&lt;br /&gt;
import java.util.List;&lt;br /&gt;
import org.avid.model.User;&lt;br /&gt;
import org.avid.util.DbUtil;&lt;br /&gt;
&lt;br /&gt;
public class UserDao {&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private Connection connection;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public UserDao() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; connection = DbUtil.getConnection();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void addUser(User user) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PreparedStatement preparedStatement = connection&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .prepareStatement(&quot;insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? )&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Parameters start with 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(1, user.getFirstName());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(2, user.getLastName());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(4, user.getEmail());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.executeUpdate();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void deleteUser(int userId) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PreparedStatement preparedStatement = connection&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .prepareStatement(&quot;delete from users where userid=?&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Parameters start with 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setInt(1, userId);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.executeUpdate();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public void updateUser(User user) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PreparedStatement preparedStatement = connection&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .prepareStatement(&quot;update users set firstname=?, lastname=?, dob=?, email=?&quot; +&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;where userid=?&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // Parameters start with 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(1, user.getFirstName());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(2, user.getLastName());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setDate(3, new java.sql.Date(user.getDob().getTime()));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setString(4, user.getEmail());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setInt(5, user.getUserid());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.executeUpdate();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public List&amp;lt;User&amp;gt; getAllUsers() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; List&amp;lt;User&amp;gt; users = new ArrayList&amp;lt;User&amp;gt;();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Statement statement = connection.createStatement();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ResultSet rs = statement.executeQuery(&quot;select * from users&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; while (rs.next()) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; User user = new User();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setUserid(rs.getInt(&quot;userid&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setFirstName(rs.getString(&quot;firstname&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setLastName(rs.getString(&quot;lastname&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setDob(rs.getDate(&quot;dob&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setEmail(rs.getString(&quot;email&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; users.add(user);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return users;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public User getUserById(int userId) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; User user = new User();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PreparedStatement preparedStatement = connection.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; prepareStatement(&quot;select * from users where userid=?&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; preparedStatement.setInt(1, userId);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ResultSet rs = preparedStatement.executeQuery();&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (rs.next()) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setUserid(rs.getInt(&quot;userid&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setFirstName(rs.getString(&quot;firstname&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setLastName(rs.getString(&quot;lastname&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setDob(rs.getDate(&quot;dob&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setEmail(rs.getString(&quot;email&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (SQLException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return user;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;

&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating Contrrollers&lt;/h3&gt;
&lt;br /&gt;
Finally, create a new Servlet inside the org.avid.controller package and name it UserController.java&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
package org.avid.controller;&lt;br /&gt;
&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.text.ParseException;&lt;br /&gt;
import java.text.SimpleDateFormat;&lt;br /&gt;
import java.util.Date;&lt;br /&gt;
&lt;br /&gt;
import javax.servlet.RequestDispatcher;&lt;br /&gt;
import javax.servlet.ServletException;&lt;br /&gt;
import javax.servlet.http.HttpServlet;&lt;br /&gt;
import javax.servlet.http.HttpServletRequest;&lt;br /&gt;
import javax.servlet.http.HttpServletResponse;&lt;br /&gt;
&lt;br /&gt;
import org.avid.dao.UserDao;&lt;br /&gt;
import org.avid.model.User;&lt;br /&gt;
&lt;br /&gt;
public class UserController extends HttpServlet {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private static final long serialVersionUID = 1L;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private static String INSERT_OR_EDIT = &quot;/user.jsp&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private static String LIST_USER = &quot;/listUser.jsp&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; private UserDao dao;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; public UserController() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; super();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dao = new UserDao();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String forward=&quot;&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String action = request.getParameter(&quot;action&quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if (action.equalsIgnoreCase(&quot;delete&quot;)){&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int userId = Integer.parseInt(request.getParameter(&quot;userId&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dao.deleteUser(userId);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; forward = LIST_USER;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; request.setAttribute(&quot;users&quot;, dao.getAllUsers()); &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } else if (action.equalsIgnoreCase(&quot;edit&quot;)){&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; forward = INSERT_OR_EDIT;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int userId = Integer.parseInt(request.getParameter(&quot;userId&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; User user = dao.getUserById(userId);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; request.setAttribute(&quot;user&quot;, user);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } else if (action.equalsIgnoreCase(&quot;listUser&quot;)){&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; forward = LIST_USER;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; request.setAttribute(&quot;users&quot;, dao.getAllUsers());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } else {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; forward = INSERT_OR_EDIT;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RequestDispatcher view = request.getRequestDispatcher(forward);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; view.forward(request, response);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; User user = new User();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setFirstName(request.getParameter(&quot;firstName&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setLastName(request.getParameter(&quot;lastName&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; try {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Date dob = new SimpleDateFormat(&quot;MM/dd/yyyy&quot;).parse(request.getParameter(&quot;dob&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setDob(dob);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; } catch (ParseException e) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setEmail(request.getParameter(&quot;email&quot;));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; String userid = request.getParameter(&quot;userid&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(userid == null || userid.isEmpty())&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dao.addUser(user);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; user.setUserid(Integer.parseInt(userid));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dao.updateUser(user);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RequestDispatcher view = request.getRequestDispatcher(LIST_USER);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; request.setAttribute(&quot;users&quot;, dao.getAllUsers());&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; view.forward(request, response);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Creating View&lt;/h3&gt;
&lt;br /&gt;
Now, it’s time for us to create the jsp, the view for our application. Under the WebContent folder, create a jsp file, name it index.jsp&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=EUC-KR&quot; pageEncoding=&quot;EUC-KR&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=EUC-KR&quot;&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Insert title here&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;jsp:forward page=&quot;/UserController?action=listUser&quot; /&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
This jsp serves as the entry point for our application. In this case, it will redirect the request to our servlet to list all the users in the database.&lt;br /&gt;
&lt;br /&gt;
Next, create the jsp to list all the users in the WebContent folder. Name it listUser.jsp&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
&amp;lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=EUC-KR&quot; pageEncoding=&quot;EUC-KR&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=EUC-KR&quot;&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Show All Users&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;table border=1&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;thead&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th&amp;gt;User Id&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th&amp;gt;First Name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th&amp;gt;Last Name&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th&amp;gt;DOB&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th&amp;gt;Email&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;th colspan=2&amp;gt;Action&amp;lt;/th&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/thead&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;tbody&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;c:forEach items=&quot;${users}&quot; var=&quot;user&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;tr&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;c:out value=&quot;${user.userid}&quot; /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;c:out value=&quot;${user.firstName}&quot; /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;c:out value=&quot;${user.lastName}&quot; /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;fmt:formatDate pattern=&quot;yyyy-MMM-dd&quot; value=&quot;${user.dob}&quot; /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;c:out value=&quot;${user.email}&quot; /&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;a href=&quot;UserController?action=edit&amp;amp;userId=&amp;lt;c:out value=&quot;${user.userid}&quot;/&amp;gt;&quot;&amp;gt;Update&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;td&amp;gt;&amp;lt;a href=&quot;UserController?action=delete&amp;amp;userId=&amp;lt;c:out value=&quot;${user.userid}&quot;/&amp;gt;&quot;&amp;gt;Delete&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/tr&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/c:forEach&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/tbody&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/table&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;p&amp;gt;&amp;lt;a href=&quot;UserController?action=insert&quot;&amp;gt;Add User&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
In this jsp, we use JSTL to connect between the jsp and the servlet. We should refrain from using scriplet inside the jsp because it will make the jsp more difficult to maintain. Not to mention it will make the jsp looks ugly.&lt;br /&gt;
&lt;br /&gt;
Next, create a new jsp in WebContent folder and name it user.jsp&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
&amp;lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=EUC-KR&quot; pageEncoding=&quot;EUC-KR&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/fmt&quot; prefix=&quot;fmt&quot;%&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=EUC-KR&quot;&amp;gt;&lt;br /&gt;
&amp;lt;link type=&quot;text/css&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; href=&quot;css/ui-lightness/jquery-ui-1.8.18.custom.css&quot; rel=&quot;stylesheet&quot; /&amp;gt;&lt;br /&gt;
&amp;lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.7.1.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-ui-1.8.18.custom.min.js&quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Add new user&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;script&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(function() {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $(&#39;input[name=dob]&#39;).datepicker();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; });&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;form method=&quot;POST&quot; action=&#39;UserController&#39; name=&quot;frmAddUser&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; User ID : &amp;lt;input type=&quot;text&quot; readonly=&quot;readonly&quot; name=&quot;userid&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value=&quot;&amp;lt;c:out value=&quot;${user.userid}&quot; /&amp;gt;&quot; /&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; First Name : &amp;lt;input&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type=&quot;text&quot; name=&quot;firstName&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value=&quot;&amp;lt;c:out value=&quot;${user.firstName}&quot; /&amp;gt;&quot; /&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Last Name : &amp;lt;input&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type=&quot;text&quot; name=&quot;lastName&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value=&quot;&amp;lt;c:out value=&quot;${user.lastName}&quot; /&amp;gt;&quot; /&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DOB : &amp;lt;input&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type=&quot;text&quot; name=&quot;dob&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value=&quot;&amp;lt;fmt:formatDate pattern=&quot;MM/dd/yyyy&quot; value=&quot;${user.dob}&quot; /&amp;gt;&quot; /&amp;gt; &amp;lt;br /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Email : &amp;lt;input type=&quot;text&quot; name=&quot;email&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; value=&quot;&amp;lt;c:out value=&quot;${user.email}&quot; /&amp;gt;&quot; /&amp;gt; &amp;lt;br /&amp;gt; &amp;lt;input&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; type=&quot;submit&quot; value=&quot;Submit&quot; /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Deployment Descriptor&lt;/h3&gt;
&lt;br /&gt;
&lt;br /&gt;
Lastly, check the web.xml file located in WebContent—&amp;gt;WEB-INF folder in your project structure. Make sure it looks like this&lt;br /&gt;
&lt;pre class=&quot;xml&quot; name=&quot;code&quot;&gt;
&lt;br /&gt;
&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;&lt;br /&gt;
&amp;lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;display-name&amp;gt;SimpleJspServletDB&amp;lt;/display-name&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;welcome-file-list&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;welcome-file&amp;gt;index.jsp&amp;lt;/welcome-file&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;/welcome-file-list&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;servlet&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;description&amp;gt;&amp;lt;/description&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;display-name&amp;gt;UserController&amp;lt;/display-name&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;servlet-name&amp;gt;UserController&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;servlet-class&amp;gt;org.avid.controller.UserController&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;/servlet&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;servlet-mapping&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;servlet-name&amp;gt;UserController&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;url-pattern&amp;gt;/UserController&amp;lt;/url-pattern&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;/servlet-mapping&amp;gt;&lt;br /&gt;
&amp;lt;/web-app&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
That is it. Right click the project name and run it using Run As–&amp;gt;Run on server option.&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/08/step-by-step-guide-developing-mvc.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsMjfYyzFseehnV5vETPVOvBTtqsZF-r5iHz2JhepGweiO_vXfglHrWNApWOjhpqfpHz0NPNpwADUz_RvEa4YWftly8AXzQ8Zil8Jh_hhVFEKcwH_wpS-JlXGhTKXS80xKhw3SMlhvaJqX/s72-c/mysql-java-jdbc.jpg" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-4498905099858930544</guid><pubDate>Sat, 18 Aug 2012 22:01:00 +0000</pubDate><atom:updated>2012-09-04T00:07:24.824+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Email</category><category domain="http://www.blogger.com/atom/ns#">JavaMail</category><title>JavaMail API Part 4 - Using secured protocol</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
There are many security measures we can implement into the system. We will take a special case of gmail which uses STARTTLS.&amp;nbsp;STARTTLS is an extension to plain text communication protocols, which offers a way to upgrade a plain text connection to an encrypted (TLS or SSL) connection instead of using a separate port for encrypted communication.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih7zkPnRax5QDIaBWVkaiQwU2_XD9xbbYCRVJonz3ZIzOaHVHjwtX7ub-0bGbVyd_97fle8pJqiGLlV7E2vv0-TeFzHg8MEVUDHbH9li2DqJJtmcubbuAQL355K6SMp88Rmy9Qe8wGJen0/s1600/smtp-02.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;265&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih7zkPnRax5QDIaBWVkaiQwU2_XD9xbbYCRVJonz3ZIzOaHVHjwtX7ub-0bGbVyd_97fle8pJqiGLlV7E2vv0-TeFzHg8MEVUDHbH9li2DqJJtmcubbuAQL355K6SMp88Rmy9Qe8wGJen0/s400/smtp-02.png&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Here is the code for sending email using host which implements STARTTLS.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;package com.test;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;


public class SendMail {

   public static void main(String[] args) {

      String host = &quot;smtp.gmail.com&quot;;

      String from = &quot;sender@gmail.com&quot;;

      String pass = &quot;sender&#39;s password&quot;;

      Properties props = System.getProperties();

      props.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;); // added this line

      props.put(&quot;mail.smtp.host&quot;, host);

      props.put(&quot;mail.smtp.user&quot;, from);

      props.put(&quot;mail.smtp.password&quot;, pass);

      props.put(&quot;mail.smtp.port&quot;, &quot;587&quot;);

      props.put(&quot;mail.smtp.auth&quot;, &quot;true&quot;);

      String[] to = {&quot;recipient1@abc.com, recipient2@abc.com&quot;};

      String fileAttachment = &quot;F:\\emoticons\\yawn.gif&quot;;

      Session session = Session.getDefaultInstance(props, null);

      MimeMessage message = new MimeMessage(session);

      try {

         message.setFrom(new InternetAddress(from));

         InternetAddress[] toAddress = new InternetAddress[to.length];

         // To get the array of addresses

         for( int i=0; i &amp;lt; to.length; i++ ) { // changed from a while loop

            toAddress[i] = new InternetAddress(to[i]);

         }

         for( int i=0; i &amp;lt; toAddress.length; i++) { // changed from a while loop

            message.addRecipient(Message.RecipientType.TO, toAddress[i]);

         }

         message.setSubject(&quot;sending in a group&quot;);

         // create the message part&amp;nbsp;

         MimeBodyPart messageBodyPart = new MimeBodyPart();

         //fill message

         messageBodyPart.setText(&quot;Hi&quot;);

         Multipart multipart = new MimeMultipart();

         multipart.addBodyPart(messageBodyPart);

         // Part two is attachment

         messageBodyPart = new MimeBodyPart();

         DataSource source = new FileDataSource(fileAttachment);

         messageBodyPart.setDataHandler(new DataHandler(source));

         messageBodyPart.setFileName(fileAttachment);

         multipart.addBodyPart(messageBodyPart);

         // Put parts in message

         message.setContent(multipart);

         Transport transport = session.getTransport(&quot;smtp&quot;);

         transport.connect(host, from, pass);

         transport.sendMessage(message, message.getAllRecipients());

         transport.close();

      } catch (AddressException e) {

         e.printStackTrace();

      } catch (MessagingException e) {

         e.printStackTrace();

      }
   }
}

&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/08/javamail-api-part-4-using-secured.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih7zkPnRax5QDIaBWVkaiQwU2_XD9xbbYCRVJonz3ZIzOaHVHjwtX7ub-0bGbVyd_97fle8pJqiGLlV7E2vv0-TeFzHg8MEVUDHbH9li2DqJJtmcubbuAQL355K6SMp88Rmy9Qe8wGJen0/s72-c/smtp-02.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-1548054837193964032</guid><pubDate>Sat, 18 Aug 2012 21:01:00 +0000</pubDate><atom:updated>2012-09-04T00:28:06.518+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Email</category><category domain="http://www.blogger.com/atom/ns#">JavaMail</category><title>JavaMail API Part 3 - Send attachment in email</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRhRaXFGA1kb4nfLk2JRggq4ZWE0D_NxLC-JdJxPGb3Nm725aVxpYdBirKdenuT-65cVuiXuetcMmYcBMSfmqywsqIXg6DHK0rhU618FzavWWAgLY4gP0HHXqTqr2ZcZ9DO-tt9452EERi/s1600/mail_attachment.gif&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRhRaXFGA1kb4nfLk2JRggq4ZWE0D_NxLC-JdJxPGb3Nm725aVxpYdBirKdenuT-65cVuiXuetcMmYcBMSfmqywsqIXg6DHK0rhU618FzavWWAgLY4gP0HHXqTqr2ZcZ9DO-tt9452EERi/s1600/mail_attachment.gif&quot; /&gt;&lt;/a&gt;&lt;br /&gt;
Here is an example to send an email with attachment from your machine.&lt;br /&gt;
&lt;br /&gt;
We assume that mail server is running on localhost and is connected to internet to send email.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For prerequisites please refer to my &lt;a href=&quot;http://thejavatrail.blogspot.in/2012/08/sending-email-using-java-mail-api.html&quot; target=&quot;_blank&quot;&gt;previous&lt;/a&gt; article on JavaMail API.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;// File Name SendFileEmail.java



import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;



public class SendFileEmail

{

&amp;nbsp; &amp;nbsp;public static void main(String [] args)

&amp;nbsp; &amp;nbsp;{

&amp;nbsp; &amp;nbsp; 

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Recipient&#39;s email ID needs to be mentioned.

&amp;nbsp; &amp;nbsp; &amp;nbsp; String to = &quot;abcd@gmail.com&quot;;

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Sender&#39;s email ID needs to be mentioned

&amp;nbsp; &amp;nbsp; &amp;nbsp; String from = &quot;web@gmail.com&quot;;

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Assuming you are sending email from localhost

&amp;nbsp; &amp;nbsp; &amp;nbsp; String host = &quot;localhost&quot;;

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get system properties

&amp;nbsp; &amp;nbsp; &amp;nbsp; Properties properties = System.getProperties();

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Setup mail server

&amp;nbsp; &amp;nbsp; &amp;nbsp; properties.setProperty(&quot;mail.smtp.host&quot;, host);

&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get the default Session object.

&amp;nbsp; &amp;nbsp; &amp;nbsp; Session session = Session.getDefaultInstance(properties);

&amp;nbsp; &amp;nbsp; &amp;nbsp; try{
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a default MimeMessage object.

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MimeMessage message = new MimeMessage(session);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set From: header field of the header.

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setFrom(new InternetAddress(from));

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set To: header field of the header.

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.addRecipient(Message.RecipientType.TO,&amp;nbsp;new InternetAddress(to));

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set Subject: header field

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setSubject(&quot;This is the Subject Line!&quot;);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create the message part

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BodyPart messageBodyPart = new MimeBodyPart();

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Fill the message

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;messageBodyPart.setText(&quot;This is message body&quot;);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a multipar message

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Multipart multipart = new MimeMultipart();

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set text message part

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;multipart.addBodyPart(messageBodyPart);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Part two is attachment

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;messageBodyPart = new MimeBodyPart();

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;String filename = &quot;file.txt&quot;;

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DataSource source = new FileDataSource(filename);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;messageBodyPart.setDataHandler(new DataHandler(source));

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;messageBodyPart.setFileName(filename);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;multipart.addBodyPart(messageBodyPart);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Send the complete message parts

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setContent(multipart );

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Send message

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Transport.send(message);

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(&quot;Sent message successfully....&quot;);

&amp;nbsp; &amp;nbsp; &amp;nbsp; }catch (MessagingException mex) {

&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mex.printStackTrace();

&amp;nbsp; &amp;nbsp; &amp;nbsp; }

&amp;nbsp; &amp;nbsp;}

}


&lt;/pre&gt;
&lt;br /&gt;
Compile and run this program to send an HTML email:&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;

$ java SendFileEmail

Sent message successfully....



&lt;/pre&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
User Authentication&lt;/h3&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;If it is required to provide user ID and Password to the email server for authentication purpose then you can set these properties as follow&lt;/span&gt;s:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;props.setProperty(&quot;mail.user&quot;, &quot;myuser&quot;);

props.setProperty(&quot;mail.password&quot;, &quot;mypwd&quot;);



&lt;/pre&gt;
Rest of the email sending mechanism would remain as explained above.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/08/javamail-api-part-3-send-attachment-in.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRhRaXFGA1kb4nfLk2JRggq4ZWE0D_NxLC-JdJxPGb3Nm725aVxpYdBirKdenuT-65cVuiXuetcMmYcBMSfmqywsqIXg6DHK0rhU618FzavWWAgLY4gP0HHXqTqr2ZcZ9DO-tt9452EERi/s72-c/mail_attachment.gif" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-8958604445208456198</guid><pubDate>Sat, 18 Aug 2012 20:32:00 +0000</pubDate><atom:updated>2012-09-04T00:20:34.798+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Email</category><category domain="http://www.blogger.com/atom/ns#">JavaMail</category><title>JavaMail API Part 2 - Sending rich text in email</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
We send simple text in email in &lt;a href=&quot;http://thejavatrail.blogspot.in/2012/08/sending-email-using-java-mail-api.html&quot; target=&quot;_blank&quot;&gt;previous&lt;/a&gt; post in the series. In this article, we will send rich text. For rich text, MIME type is &quot;text/html&quot;. The actual mail content is written in HTML. While sending email, HTML contents will be set and content type is set to &quot;text/html&quot;.&lt;br /&gt;
&lt;br /&gt;
We assume that mail server is running on localhost and is connected to internet to send email.&lt;br /&gt;
&lt;br /&gt;
For prerequisites please refer to my &lt;a href=&quot;http://thejavatrail.blogspot.in/2012/08/sending-email-using-java-mail-api.html&quot; target=&quot;_blank&quot;&gt;previous&lt;/a&gt;&amp;nbsp;article on JavaMail API.&lt;br /&gt;
&lt;br /&gt;

&lt;br /&gt;
Using this example, you can send as big as HTML content you like.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
// File Name SendHTMLEmail.java&lt;br /&gt;
&lt;br /&gt;
import java.util.*;&lt;br /&gt;
import javax.mail.*;&lt;br /&gt;
import javax.mail.internet.*;&lt;br /&gt;
import javax.activation.*;&lt;br /&gt;
&lt;br /&gt;
public class SendHTMLEmail&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;public static void main(String [] args)&lt;br /&gt;
&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Recipient&#39;s email ID needs to be mentioned.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String to = &quot;abcd@gmail.com&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Sender&#39;s email ID needs to be mentioned&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String from = &quot;web@gmail.com&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Assuming you are sending email from localhost&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String host = &quot;localhost&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get system properties&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Properties properties = System.getProperties();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Setup mail server&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; properties.setProperty(&quot;mail.smtp.host&quot;, host);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get the default Session object.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Session session = Session.getDefaultInstance(properties);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; try{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a default MimeMessage object.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MimeMessage message = new MimeMessage(session);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set From: header field of the header.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setFrom(new InternetAddress(from));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set To: header field of the header.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set Subject: header field&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setSubject(&quot;This is the Subject Line!&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Send the actual HTML message, as big as you like&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setContent(&quot;&amp;lt;h1&amp;gt;This is actual message&amp;lt;/h1&amp;gt;&quot;,&amp;nbsp;&quot;text/html&quot; );&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Send message&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Transport.send(message);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(&quot;Sent message successfully....&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; }catch (MessagingException mex) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mex.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;
Compile and run this program to send an HTML email:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
$ java SendHTMLEmail&lt;br /&gt;
Sent message successfully....&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/08/javamail-api-part-2-sending-rich-text.html</link><author>noreply@blogger.com (Jay Rajani)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-6247101270631041564</guid><pubDate>Sat, 18 Aug 2012 18:05:00 +0000</pubDate><atom:updated>2012-09-04T00:24:15.791+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Email</category><category domain="http://www.blogger.com/atom/ns#">JavaMail</category><title>JavaMail API Part 1 - Sending simple text email</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
There are many applications where system has to send an email to the user.&lt;br /&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6cHJtq6y5UvQNOFLQ_35pU-5C8rei82VwjmM-zcP0-ESCL4lS_TKER25_N66YU3EFPcpCv0LRZvs5WryOtsrG9SVBcqLuWgELSSXC7qDeB_5KTENZCBYRYY3fu3PALlYt6ttoGZ2NaDry/s1600/email.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: right; float: right; margin-bottom: 1em; margin-left: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6cHJtq6y5UvQNOFLQ_35pU-5C8rei82VwjmM-zcP0-ESCL4lS_TKER25_N66YU3EFPcpCv0LRZvs5WryOtsrG9SVBcqLuWgELSSXC7qDeB_5KTENZCBYRYY3fu3PALlYt6ttoGZ2NaDry/s1600/email.jpg&quot; /&gt;&lt;/a&gt;
&lt;li&gt;Notification&lt;/li&gt;
&lt;li&gt;Validation&lt;/li&gt;
&lt;li&gt;Reporting&lt;/li&gt;
&lt;li&gt;Alarming etc&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
Sending email though look simple but it has many things to be considered like&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Whether email is a rich text or simple text&lt;/li&gt;
&lt;li&gt;Whether email has attachment&lt;/li&gt;
&lt;li&gt;Email using authentication&lt;/li&gt;
&lt;li&gt;Emailing using secured protocols and mechanisms&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
Let&#39;s first take a simple case of sending a simple text email. We can write a simple standalone java application to send email.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;Prerequisites&lt;/b&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;We will need&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;b&gt;JavaMail API&lt;/b&gt; and &lt;b&gt;Java Activation Framework (JAF)&lt;/b&gt;.&amp;nbsp;You can download latest version&amp;nbsp;from Oracle website.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://www.oracle.com/technetwork/java/javamail/index.html&quot; target=&quot;_blank&quot;&gt;JavaMail API&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.oracle.com/technetwork/java/jaf11-139815.html&quot; target=&quot;_blank&quot;&gt;JAF&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Download and unzip these files, in the newly created top level directories you will find a number of jar files for both the applications. You need to add &lt;b&gt;mail.jar&lt;/b&gt; and &lt;b&gt;activation.jar&lt;/b&gt; files in your CLASSPATH.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
We will also need mail server to send an email. There are many open source mail server we can use or we can use inbuilt mail server from operating system. Let&#39;s assume that mail server is running on localhost.&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
Sending Text Email&lt;/h3&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: justify;&quot;&gt;
Following is the code.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
// File Name SendMail.java
&lt;br /&gt;
import java.util.*;&lt;br /&gt;
import javax.mail.*;&lt;br /&gt;
import javax.mail.internet.*;&lt;br /&gt;
import javax.activation.*;&lt;br /&gt;
&lt;br /&gt;
public class SendMail&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;public static void main(String [] args)&lt;br /&gt;
&amp;nbsp; &amp;nbsp;{
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Recipient&#39;s email ID needs to be mentioned.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String to = &quot;abcd@gmail.com&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Sender&#39;s email ID needs to be mentioned&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String from = &quot;web@gmail.com&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Assuming you are sending email from localhost&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; String host = &quot;localhost&quot;;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get system properties&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Properties properties = System.getProperties();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Setup mail server&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; properties.setProperty(&quot;mail.smtp.host&quot;, host);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; // Get the default Session object.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Session session = Session.getDefaultInstance(properties);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; try{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Create a default MimeMessage object.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MimeMessage message = new MimeMessage(session);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set From: header field of the header.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setFrom(new InternetAddress(from));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set To: header field of the header.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.addRecipient(Message.RecipientType.TO,&amp;nbsp;new InternetAddress(to));&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Set Subject: header field&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setSubject(&quot;This is the Subject Line!&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Now set the actual message&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;message.setText(&quot;This is actual message&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Send message&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Transport.send(message);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(&quot;Sent message successfully....&quot;);&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; }catch (MessagingException mex) {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;mex.printStackTrace();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;br /&gt;
&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;/pre&gt;
&lt;br /&gt;
Compile and run this program to send a simple email:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;java&quot; name=&quot;code&quot;&gt;
$ java SendMail&lt;br /&gt;
Sent message successfully....&lt;br /&gt;
&lt;br /&gt;
&lt;/pre&gt;
&lt;br /&gt;
&lt;h3&gt;
Sending Email to multiple recipient&lt;/h3&gt;
&lt;br /&gt;
If you want to send an email to multiple recipients then following methods would be used to specify multiple email IDs:&lt;br /&gt;
&lt;br /&gt;
void addRecipients(Message.RecipientType type, Address [] addresses)&amp;nbsp;MessagingException&lt;br /&gt;
&lt;br /&gt;
Here is the description of the parameters:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;type: &lt;/b&gt;This would be set to TO, CC or BCC. Here CC represents Carbon Copy and BCC represents Black Carbon Copy. Example Message.RecipientType.TO&lt;/li&gt;
&lt;li&gt;&lt;b&gt;addresses:&lt;/b&gt; This is the array of email ID. You would need to use InternetAddress() method while specifying email IDs&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/08/sending-email-using-java-mail-api.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi6cHJtq6y5UvQNOFLQ_35pU-5C8rei82VwjmM-zcP0-ESCL4lS_TKER25_N66YU3EFPcpCv0LRZvs5WryOtsrG9SVBcqLuWgELSSXC7qDeB_5KTENZCBYRYY3fu3PALlYt6ttoGZ2NaDry/s72-c/email.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-7101417695463409960</guid><pubDate>Sun, 29 Jul 2012 15:47:00 +0000</pubDate><atom:updated>2012-08-19T02:33:26.949+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Design Patterns</category><title>J2EE Design Patterns and their relationships</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRIPxdaDuodxRuje9JWowb7OmPD1IQpsT9bk1hRLMFSB-flX3Zi-RJCLyh5oT7Y5lv7WAIVE1IvoaQKxKwCY6c35WMOc-Al8qz7wABoPtfq4KUXsNYaYeaSX9JaWewbYhX_njrJDykzaA/s1600/cjp2catalog1.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRIPxdaDuodxRuje9JWowb7OmPD1IQpsT9bk1hRLMFSB-flX3Zi-RJCLyh5oT7Y5lv7WAIVE1IvoaQKxKwCY6c35WMOc-Al8qz7wABoPtfq4KUXsNYaYeaSX9JaWewbYhX_njrJDykzaA/s1600/cjp2catalog1.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</description><link>http://thejavatrail.blogspot.com/2012/07/j2ee-design-patterns-and-their.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjgRIPxdaDuodxRuje9JWowb7OmPD1IQpsT9bk1hRLMFSB-flX3Zi-RJCLyh5oT7Y5lv7WAIVE1IvoaQKxKwCY6c35WMOc-Al8qz7wABoPtfq4KUXsNYaYeaSX9JaWewbYhX_njrJDykzaA/s72-c/cjp2catalog1.gif" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-3973636516408417817</guid><pubDate>Thu, 19 Jul 2012 06:46:00 +0000</pubDate><atom:updated>2012-07-19T13:17:39.598+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">installation</category><category domain="http://www.blogger.com/atom/ns#">linux</category><category domain="http://www.blogger.com/atom/ns#">mysql</category><title>Installing mysql on Red Hat</title><description>&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;To install mysql on linux, we need RPM files. You can download MySQL-5.5.20-1.linux2.6.i386.tar from mysql download page to understand and try steps from this article.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;For standard installation, one needs to install two RPMs: Server and Client. Server RPM contains mysql database, services, daemons etc whereas Client RPM contains admin tools, querying tools etc.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;In MySQL-5.5.20-1.linux2.6.i386.tar file, you will get following required RPMs&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;MySQL-server-5.5.20-1.linux2.6.i386.rpm&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;MySQL-client-5.5.20-1.linux2.6.i386.rpm&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;In my machine, I have downloaded RPMs on in /bin folder. Now go to shell and change directory to bin.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# rpm -ivf MySQL-server-5.5.20-1.linux2.6.i386.rpm&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;This will install server RMPs without setting password for root user causing anonymous access. To change the password you will need mysqladmin. It is bundled in client RPM.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# rpm -ivf MySQL-client-5.5.20-1.linux2.6.i386.rpm&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;After successful installation run following command to change root password&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# /usr/bin/mysqladmin -u root password &#39;new-password&#39;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Now your mysql is ready to run. But the moment you try to connect it will give error like &quot;&lt;your ip=&quot;&quot;&gt; is not allowed to connect to mysql service&quot;&lt;/your&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;The issue is caused because of default privileges given to root user. You need connect privilege to connect from a remote machine. You can grant all privileges to root user.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# su - mysql&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;This will open bash for mysql user created when you install server RPMs.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;-bash-3.2$ mysql -u root -p&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Enter password: &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Welcome to the MySQL monitor.  Commands end with ; or \g.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Your MySQL connection id is 8&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Server version: 5.5.20 MySQL Community Server (GPL)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Oracle is a registered trademark of Oracle Corporation and/or its&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;affiliates. Other names may be trademarks of their respective&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;owners.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Type &#39;help;&#39; or &#39;\h&#39; for help. Type &#39;\c&#39; to clear the current input statement.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;mysql&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;mysql&amp;gt; GRANT ALL ON *.* TO &#39;root&#39;@&#39;%&#39; IDENTIFIED BY &#39;&lt;password&gt;&#39;;&lt;/password&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Query OK, 0 rows affected (0.00 sec)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Now try connecting the mysql server, you should be able to connect.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;For unlucky people like me :), things do not start easily. When I tried installing mysql for the first time, mysqld service could not start. If the same thing happens to you, first check your installed packages using following command&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# rpm -qa | grep &#39;MySQL&#39;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;MySQL-server-5.5.20-1.linux2.6&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;MySQL-client-5.5.20-1.linux2.6&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;If you can see both RPM, you are okay with installation or nothing has been uninstalled. If it does not show you both, you need to install the required RPM.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;Second step is to find mysqld and provide privilege using&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# chmod 775 mysqld&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;and then&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# service mysqld start&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;You can also start it using &lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;[root@mysql bin]# /usr/bin/mysqld_safe &amp;amp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;That&#39;s it. There can be a lot of difference in issues faced while installing. Linux requires a thorogh understanding of how different flavour of linux works. But as programmers we can use google to stay away from that burden.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family:Georgia, serif;&quot;&gt;You can share your experiences here if you wish to.&lt;/span&gt;&lt;/div&gt;</description><link>http://thejavatrail.blogspot.com/2012/07/installing-mysql-on-red-hat.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-4267557993993698597</guid><pubDate>Sat, 30 Jun 2012 06:31:00 +0000</pubDate><atom:updated>2012-07-27T15:17:07.241+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Annotation</category><category domain="http://www.blogger.com/atom/ns#">Hibernate</category><category domain="http://www.blogger.com/atom/ns#">Validator</category><title>Writing centralized validation using Java Annotation Part 1</title><description>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;The validation component in any application is generally scattered on different layers, though it is not a good practice. On presentation tier we have JS validations and on JSP/ Servlet side we use some POJO stuff. Again if we reach at persistence layer we have hibernate validation or any other db validations. As it is described here and we can realize. Many times validations has been performed at each class level without any code reuse.&lt;br /&gt;&lt;br /&gt;The issue is in the architecture.  We need to design a clear cut architecture for validation component. We have to identify entities to be validated, kind of business rules to be applied etc. Some entity has to be validated differently for different groups. e.g. User entity - when we receive request we see for values other than id, but while inserting into db we validate id. Similarly multiple flows use the same entity differently. Hence centralizing validation component is not again a simple job.&lt;br /&gt;&lt;br /&gt;Here in this series, I will explain you how to achieve the goal at certain extent using Java annotation and hibernate validation. The mechanism may be different for different application.&lt;br /&gt;&lt;br /&gt;Let&#39;s start with a centralized validator class using annotation&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;import javax.validation.ConstraintViolation;&lt;br /&gt;import javax.validation.Validation;&lt;br /&gt;import javax.validation.Validator;&lt;br /&gt;import javax.validation.ValidatorFactory;&lt;br /&gt;&lt;br /&gt;// default validator is required as per JSR303. We have hibernate validator jar for the same.&lt;br /&gt;ValidatorFactory factory = Validation.buildDefaultValidatorFactory();&lt;br /&gt;Validator validator = factory.getValidator();&lt;br /&gt;&lt;br /&gt;Set&amp;lt;ConstraintViolation&amp;lt;Object&amp;gt;&amp;gt; constraintViolations = validator.validate(targetObject);&lt;br /&gt;&lt;br /&gt;if (constraintViolations.size() &amp;gt; 0) {&lt;br /&gt;for (ConstraintViolation&amp;lt;Object&amp;gt; constraintViolation : constraintViolations) {&lt;br /&gt;// Actions to be taken when validation fails&lt;br /&gt;throw new RuntimeException(constraintViolation.getMessage());&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;ValidatorFactory is a factory class found in the implementation jar. We hibernate validation. You can refer Java Validation API. It searches for classes which implements&lt;br /&gt;javax.validation.ConstraintValidator.&lt;br /&gt;Here is an example of such a validator which validates a POJO say it Task&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;package com.avid.validation.validator;&lt;br /&gt;&lt;br /&gt;import javax.validation.ConstraintValidator;&lt;br /&gt;import javax.validation.ConstraintValidatorContext;&lt;br /&gt;import com.avid.transfer.Task;&lt;br /&gt;import com.avid.validation.annotation.ValidateTask;&lt;br /&gt;&lt;br /&gt;public class TaskValidator implements ConstraintValidator&amp;lt;ValidateTask, Task&amp;gt; {&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public void initialize(ValidateTask validateTask) {}&lt;br /&gt;&lt;br /&gt;@Override&lt;br /&gt;public boolean isValid(Task task, ConstraintValidatorContext validationContext) {&lt;br /&gt;if(task == null || task.getId() == -1) {&lt;br /&gt;validationContext.disableDefaultConstraintViolation();&lt;br /&gt;validationContext.buildConstraintViolationWithTemplate(&quot;No task selected.&quot;).addConstraintViolation();&lt;br /&gt;return false;&lt;br /&gt;}&lt;br /&gt;return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;div&gt;&lt;span style=&quot; ;font-size:100%;&quot;&gt;When we call validator.validate(targetObject), javax.validation.Validator first finds suitable validator for object passed in the argument using its class name. It calles initialize method to initialize properties and resources. At last it will call isValid method.&lt;/span&gt;&lt;/div&gt;You must be wondering about ValidateTask. It is an annotation used to specify where to perform the validation. Following is the code for ValidateTask&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;@Constraint(validatedBy=TaskValidator.class)&lt;br /&gt;public @interface ValidateTask {&lt;br /&gt;&lt;span class=&quot;Apple-tab-span&quot;  style=&quot; white-space: pre;font-size:small;&quot;&gt;&lt;i&gt;&lt;br /&gt;// All three properties are required by validation framework&lt;br /&gt;String message() default &quot;{TaskValidator}&quot;;&lt;br /&gt;Class&amp;lt;?&amp;gt;[] groups() default {};&lt;br /&gt;Class&amp;lt;? extends Payload&amp;gt;[] payload() default {};&lt;br /&gt;}&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/pre&gt;&lt;i&gt;You can use it at class level or instance level. For this example I have used it at class level for simplicity like&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt;@ValidateTask&lt;br /&gt;public class Task extends BaseVO {&lt;br /&gt;private int id;&lt;br /&gt;private String taskName;&lt;br /&gt;&lt;br /&gt;...........&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Now we are done with centralized validator for Task. We can extend it to any number of entities. To use the validator in client, now you need only one line&lt;br /&gt;&lt;br /&gt;&lt;pre name=&quot;code&quot; class=&quot;java&quot;&gt; DefaultValidator.validate(new Task())&lt;br /&gt;&lt;/pre&gt;That&#39;s it. Now you can modify at one place and can impact entire application without the hassle of finding validation code and fixing it at multiple places.&lt;br /&gt;&lt;br /&gt;In next article I will show you how to add Regex validator and db validator in the same component to make it robust.&lt;/i&gt;&lt;/div&gt;</description><link>http://thejavatrail.blogspot.com/2012/06/writing-centralized-validation-using.html</link><author>noreply@blogger.com (Jay Rajani)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-3058787506096788562</guid><pubDate>Wed, 22 Jul 2009 12:01:00 +0000</pubDate><atom:updated>2009-09-03T15:23:45.210+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Framework</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Using hibernate and struts together</title><description>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;This article is to use hibernate with struts. As when we use hibernate, we have to set values of all fields externally. If we use struts with hibernate then all the values are set by struts. So it helps decreasing the number of lines of code. As well struts also helps in organizing the application&lt;br /&gt;&lt;br /&gt;(1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Configure the struts configuration in web.xml. If you don&#39;t know how to do so then first read a little about struts. At: Struts Action Example&lt;br /&gt;&lt;br /&gt;(2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In struts-config.xml define the action and form. &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.g.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;!-- ========== Form Bean Definitions ================= --&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;form-beans&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;form-bean name=&quot;User&quot; type=&quot;com.persistence.UserVO&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/form-beans&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;!-- ========== Action Mapping Definitions ============ --&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;action-mappings&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;action path=&quot;/user&quot; type=&quot;com.control.UserController&quot; name=&quot;User&quot; input=&quot;/home.jsp&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;forward name=&quot;success&quot; path=&quot;/home.jsp&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;forward name=&quot;failure&quot; path=&quot;/room.jsp&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/action&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/action-mappings&amp;#62;&lt;br /&gt;&lt;br /&gt;(3)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now create bean class as per the requirement the database as well HTML form. E.g.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;import org.apache.struts.action.ActionForm;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public class UserVO extends ActionForm {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private int id;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String userName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String userPassword;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String userFirstName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String userLastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String userEmail;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// ----- getter and setter methods for all fields&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;But keep one thing clear that the fields of bean class must have same name as the fields of HTML form you submit from. Here I created this class as my HTML form is as like below.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;form action=&quot;user.do&quot; method=&quot;post&quot;&amp;#62;&amp;#60;br /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name:&amp;#60;input type=&quot;text&quot; name=&quot;username&quot; /&amp;#62; &amp;#60;br /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Password:&amp;#60;input type=&quot;password&quot; name=&quot;userPassword&quot; /&amp;#62; &amp;#60;br /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;First name:&amp;#60;input type=&quot;text&quot; name=&quot;userFirstName&quot; /&amp;#62; &amp;#60;br /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Last name:&amp;#60;input type=&quot;text&quot; name=&quot;userLastName&quot; /&amp;#62; &amp;#60;br /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;E-mail:&amp;#60;input type=&quot;text&quot; name=&quot;userEmail&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;input type=&quot;submit&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/form&amp;#62;&lt;br /&gt;&lt;br /&gt;(4)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now configure hibernate configuration files. First of all create hibernate.cfg.xml file as per your requirements. If you don&#39;t know how to create then please read about hibernate first. Hibernate tutorial&lt;br /&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Then after create *.hbn.xml file to map bean class with database table (here * in the name means you can give name as per your choice). As below &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;hibernate-mapping&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;class name=&quot;com.persistence.UserVO&quot; table=&quot;contact&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;id column=&quot;USER_ID&quot; name=&quot;id&quot; type=&quot;int&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;generator class=&quot;native&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/id&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property column=&quot;USER_NAME&quot; name=&quot;userName&quot; type=&quot;java.lang.String&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property column=&quot;USER_PASSWORD&quot; name=&quot;userPassword&quot; type=&quot;java.lang.String&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property column=&quot;USER_FIRST_NAME&quot; name=&quot;userFirstName&quot; type=&quot;java.lang.String&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property column=&quot;USER_LAST_NAME&quot; name=&quot;userLastName&quot; type=&quot;java.lang.String&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property column=&quot;USER_EMAIL&quot; name=&quot;userEmail&quot; type=&quot;java.lang.String&quot; /&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/class&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/hibernate-mapping&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PLEASE NOTE:&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;This is main part where you have to take enough care. In class tag value of attribute name must be equal to the value of attribute type in tag form-bean.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;As well in id and property tags column name must contain the name of column of your table and name tag must contain the value you define in bean class.&lt;br /&gt;&lt;br /&gt;(5)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now define the class extending Action class of struts&#39; action class as below.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public class UserController extends Action {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UserVO user = (UserVO)form;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Session session = sessionFactory.getCurrentSession();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Transaction t = session.beginTransaction();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;session.saveOrUpdate(user);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;t.commit();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return mapping.findForward(&quot;success&quot;);  &lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} catch (Exception e) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return mapping.findForward(&quot;failure&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}</description><link>http://thejavatrail.blogspot.com/2009/07/using-hibernate-and-struts-together.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-5406239311730261437</guid><pubDate>Mon, 25 May 2009 12:15:00 +0000</pubDate><atom:updated>2009-09-03T15:23:45.210+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Framework</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Struts – Action – Example</title><description>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;In my earlier post (&lt;a href=&quot;http://thejavatrail.blogspot.com/2009/02/what-is-struts-struts-is-java-mvc.html&quot;&gt;Struts - What &amp; Why?&lt;/a&gt;) I have explained the basic architecture of struts base application. Now I gonna explain you how to create application through struts.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;There is a predefined structure to implement struts in our web application.&lt;br /&gt;&lt;br /&gt;(1)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;First of all create web.xml. Declare a servlet as below.&lt;br /&gt;e.g.&lt;br /&gt;&amp;#60;servlet&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-name&amp;#62;action&amp;#60;/servlet-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-class&amp;#62;org.apache.struts.action.ActionServlet&amp;#60;/servlet-class&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-name&amp;#62;application&amp;#60;/param-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-value&amp;#62;ApplicationResources&amp;#60;/param-value&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-name&amp;#62;config&amp;#60;/param-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-value&amp;#62;/WEB-INF/struts-config.xml&amp;#60;/param-value&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;load-on-startup&amp;#62;2&amp;#60;/load-on-startup&amp;#62;&lt;br /&gt;&amp;#60;/servlet&amp;#62;&lt;br /&gt;&amp;#60;!-- Standard Action Servlet Mapping --&amp;#62;&lt;br /&gt;&amp;#60;servlet-mapping&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-name&amp;#62;action&amp;#60;/servlet-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;url-pattern&amp;#62;*.do&amp;#60;/url-pattern&amp;#62;&lt;br /&gt;&amp;#60;/servlet-mapping&amp;#62;&lt;br /&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;We are not declaring any controller servlet here. As we are going to use struts. In struts controller servlet are being declared in struts-comfig.xml. &lt;br /&gt;&lt;br /&gt;(2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Create one file struts-config.xml. place this file in WEB-INF directory.&lt;br /&gt;e.g.&lt;br /&gt;&amp;#60;struts-config&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;!--  Form Bean Definitions  --&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;form-beans&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;form-bean name=&quot;submitForm&quot; type=&quot;com.forms.SubmitForm&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/form-beans&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;!--  Action Mapping Definitions  --&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;action-mappings&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;action path=&quot;/submit&quot; type=&quot;com.action.SubmitAction&quot; name=&quot;submitForm&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;forward name=&quot;success&quot; path=&quot;/index.jsp&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;forward name=&quot;failure&quot; path=&quot;/submit.jsp&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/action&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/action-mappings&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;message-resources parameter=&quot;ApplicationResources&quot;/&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/struts-config&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Here we declared one action named submitForm. SubmitForm defines path /submit so whenever server request for /submit.do submitForm action will be called.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;forward tag defines that where to forward request after its processing. Here we define submit.jsp for failure. So if request been failed to process then server will forward the request to submit.jsp. As well for success we define index.jsp.&lt;br /&gt;&lt;br /&gt;(3)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now define a class SubmitAction extending  org.apache.struts.action.Action class.&lt;br /&gt;As in servlet, we defines a method as doPost or doGet to process. Here define a method execute as in example.&lt;br /&gt;e.g.&lt;br /&gt;public class SubmitAction extends Action {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* here we gets object of ActionMapping, ActionForm, HttpServletRequest and HttpResponse.*/&lt;br /&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SubmitForm submitForm = (SubmitForm)form;&lt;br /&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/* if lastName field left blank then procees will be stoped and control will go to submit form again */&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(submitForm.getLastName()==null)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return mapping.findForward(&quot;failure&quot;);&lt;br /&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;String lastName = submitForm.getLastName();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;request.setAttribute(&quot;lastName&quot;, lastName.toUpperCase());&lt;br /&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return mapping.findForward(&quot;success&quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;br /&gt;} &lt;br /&gt;&lt;br /&gt;mapping : this object is used to forward request after processing as in example.&lt;br /&gt;form : explain later in this post.&lt;br /&gt;&lt;br /&gt;(4)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now just look at the struts-config.xml, there is tag defined by form-beans. This tag defines the form class. The form class is a simple javabean which contains the fields, we are going to pass through the form from jsp or html. Fields&#39; name must match with the names of the parameter of the request.&lt;br /&gt;e.g.&lt;br /&gt;public class SubmitForm extends ActionForm {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String lastName = &quot;&quot;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private String address = &quot;&quot;;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public String getLastName() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void setLastName(String lastName) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.lastName = lastName;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public String getAddress() {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return address;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void setAddress(String address) {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.address = address;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Here parameter passed through the /submit.do request must be lastName and address.&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Whenever one submit the form from submit.jsp all the fields of form class will be set with the values passed in the request. So the object we are getting in action class as argument have all the field filled by the user.&lt;br /&gt;&lt;br /&gt;(5)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Now for the testing create one jsp named submit.jsp.&lt;br /&gt;e.g.&lt;br /&gt;&amp;#60;form action=&quot;/submit&quot; method=&quot;post&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Last name: &amp;#60;input type=&quot;text&quot; name=&quot;lastName&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;address:&amp;#60;br/&amp;#62;&amp;#60;textarea name=&quot;address&quot; rows=&quot;2&quot; cols=&quot;30&quot;&amp;#62; address&amp;#60;/textarea&amp;#62; &lt;br /&gt;&amp;#60;/form&amp;#62;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;This is how the whole structure of a struts action implemented. This is a very basic functionality. This may be extend to a large application where more one controller are defined as well as forms.</description><link>http://thejavatrail.blogspot.com/2009/05/struts-action-example.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-9141420143298637641</guid><pubDate>Tue, 21 Apr 2009 05:31:00 +0000</pubDate><atom:updated>2009-09-03T15:24:23.194+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Servicemix</category><category domain="http://www.blogger.com/atom/ns#">SOA</category><title>Servicemix - HttpConnectorDemo – Example</title><description>This post explains how to create a servicemix httpconnector component using spring so if you are not using spring don’t waste your time.&lt;br /&gt;&lt;br /&gt;(1)  First of all create one configuration file containing component declaration. Put this file in your configuration folder (e.g. WEB-INF).&lt;br /&gt;E.g.&lt;br /&gt;&amp;#60;sm:activationspec componentname=&quot; HttpConnectorDemo&quot; service=&quot;foo:MobileHttpBinding&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;sm:component&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;bean class=&quot;com.component.pollar.MobileHttpBinding&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;property name=&quot;defaultInOut&quot; value=&quot;false&quot;&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/property&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/bean&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/sm:component&amp;#62;&lt;br /&gt;&amp;#60;/sm:activationspec&amp;#62;&lt;br /&gt;&lt;br /&gt;(2) Map the configuration file from your web.xml file so that your component takes part in web application.&lt;br /&gt;E.g.&lt;br /&gt;&amp;#60;context-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-name&amp;#62;contextConfigLocation&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-value&amp;#62;/WEB-INF/applicationContext.xml&lt;br /&gt;&amp;#60;/param-value&amp;#62;&lt;br /&gt;&lt;br /&gt;Then after to load this component whenever container starts, add following snippet of code to your web.xml.&lt;br /&gt;&lt;br /&gt;&amp;#60;context-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-name&amp;#62;contextClass&amp;#60;/param-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-value&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;org.apache.xbean.spring.context.XmlWebApplicationContext&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/param-value&amp;#62;&lt;br /&gt;&amp;#60;/context-param&amp;#62;&lt;br /&gt;&amp;#60;listener&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;listener-class&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;org.springframework.web.context.ContextLoaderListener&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/listener-class&amp;#62;&lt;br /&gt;&amp;#60;/listener&amp;#62;&lt;br /&gt;&lt;br /&gt;(3)  Now make a class file containing implementation of HttpConnector class of servicemix. Add your actual logic to processInOnly method as we have defined that  &amp;#60;property name=&quot;defaultInOut&quot; value=&quot;false&quot;&amp;#62; so a call to this class will go to processInOnly method. Yet for safety define processInOut method and call processInOnly method from there.&lt;br /&gt;E.g.&lt;br /&gt;public class HttpConnectorDemo extends HttpConnector {&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void processInOnly(HttpServletRequest request, HttpServletResponse response){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// Your actual logic.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void processInOut(HttpServletRequest request, HttpServletResponse response){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;processInOnly(request, response);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;} &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;(4)  Now this class can act as a servlet. All we have to do is define a servlet in web.xml and map it to this class.&lt;br /&gt;E. g.&lt;br /&gt;&amp;#60;servlet&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-name&amp;#62;MobileServlet&amp;#60;/servlet-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-class&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;org.apache.servicemix.components.http.SpringBindingServlet&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/servlet-class&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-name&amp;#62;endpoint&amp;#60;/param-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;param-value&amp;#62;MobileHttpBinding&amp;#60;/param-value&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;/init-param&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;load-on-startup&amp;#62;1&amp;#60;/load-on-startup&amp;#62;&lt;br /&gt;&amp;#60;/servlet&amp;#62;&lt;br /&gt;&amp;#60;servlet-mapping&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;servlet-name&amp;#62;Mobile&amp;#60;servlet-name&amp;#62;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;#60;url-pattern&amp;#62;/form.do&amp;#60;/url-pattern&amp;#62;&lt;br /&gt;&amp;#60;/servlet-mapping&amp;#62;&lt;br /&gt;&lt;br /&gt;(5) Now the part left is most easy part just make a war file and deploy it in your server. Start your browser, hit the URL and see the picture, your job is done. Just include necessary jar files in lib folder so that server can load your component properly.&lt;br /&gt;&lt;br /&gt;Now start making components and Enjoy Servicemix ESB.</description><link>http://thejavatrail.blogspot.com/2009/04/servicemix-httpconnectordemo-example.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-9042546801445247847</guid><pubDate>Fri, 06 Mar 2009 17:10:00 +0000</pubDate><atom:updated>2009-04-20T16:22:14.463+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Java - Inner Classes - for SCJP</title><description>If anonymous class is created for interface, it extends Object class and implement that interface, if it is created for a class then it extends that class.&lt;br /&gt;Earlier, Non-static inner classes were not allowed to have static fields at all. This rule is now modified and the new rule says that:  &#39;The third paragraph of the section Members that can be marked static is amended to make an exception and allow inner classes to declare static FIINAL fields that are compile time constants as members.&#39;&lt;br /&gt;Ex.:&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;public class Outer&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;class Inner&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;static final  int k = 10;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;(Top level classes mean, classes defined in package scope and STATIC inner classes of classes defined in package scope) Consider the anonymous inner class for catching action events:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;ActionListener al = new ActionListener()&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;public void actionPerformed(ActionEvent e)&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;};&lt;/span&gt;&lt;br /&gt;Here, the anonymous class implicitly extends Object and implements the interface ActionListener. Explicit extends or implements clauses are not allowed for such classes. Other inner class (ie. non anonymous) can have them. Consider the following (although of no use) class:&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;public class TestClass&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;public TestClass(int i)  {    }&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;public void m1()&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;TestClass al = new TestClass(10)&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;public void actionPerformed(ActionEvent e)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;};&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;This illustrates 4 points:&lt;br /&gt;&lt;ol type=&quot;1&quot;&gt;&lt;li&gt;Instance Methods can also have inner classes. (But they cannot have static inner classes).&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Inner class can extend the outer class.&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Anonymous inner class can be created for classes. (Not just for interfaces). They implicitly extend the class.(Here, TestClass)&lt;/li&gt;&lt;br /&gt;&lt;li&gt;Anonymous inner class can have initialization parameter. (If the class they extend has a corresponding constrctor).&lt;/li&gt;&lt;br /&gt;&lt;/ol&gt;Non-static inner classes can contain final static fields (but not methods).&lt;br /&gt;Anonymous classes cannot have explict constructors, since they have no names.&lt;br /&gt;A static inner class is also known as A Top Level Nested class. So,there are two types of Top level classes. One, that is a standard class and other an inner class which is static.&lt;br /&gt;Eg.&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;public class A  //This is a standard Top Level class.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;class X&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;static final int j = 10;  //compiles fine!&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;}&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;public static class B //This is also a Top Level class (but nested!)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;}&lt;/span&gt;&lt;br /&gt;You can create objects of B with having objects of A. Eg. A.B b = new A.B();&lt;br /&gt;Members in outer instances are directly accessible using simple names. There is no restriction that member variables in inner classes must be final.&lt;br /&gt;Nested classes define distinct types from the enclosing class, and the instanceof operator does not take of the outer instance into consideration.&lt;br /&gt;&lt;br /&gt;Every non static inner class object has a reference to it&#39;s out class object which can be accessed by doing OuterClass.this. So the expression B.this.c will refer to B&#39;s c, which is &#39;a&#39;. Inside a non-static inner class, &#39;InnerClass.this&#39; is equivalent to &#39;this&#39;. so &#39;C.this.c&#39; refers to C&#39;s c which is &#39;c&#39;. The expression super.c will access the variable from A, the superclass of C which is &#39;d&#39;.&lt;br /&gt;&lt;br /&gt;Only classes declared as members of top-level classes can be declared static. Such a member is a top-level nested class if it is declared static, otherwise it is a non-static inner class.&lt;br /&gt;Package member classes, local classes(ie. classes declared in methods) and anonymous classes cannot be declared static.</description><link>http://thejavatrail.blogspot.com/2009/03/if-anonymous-class-is-created-for.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-3454710148137692373</guid><pubDate>Mon, 02 Mar 2009 12:47:00 +0000</pubDate><atom:updated>2009-09-03T15:24:55.256+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Framework</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><title>Prevent Duplicate Form Submission</title><description>&lt;b&gt;Duplicate form submissions can occur in many ways :&lt;/b&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Using Refresh button&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Using the browser back button to traverse back and resubmit form&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Using Browser history feature and re-submit form.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Malicious submissions to adversely impact the server or personal gains&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Clicking more than once on a transaction that take longer than usual&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Effect :&lt;/b&gt;&lt;br /&gt;&lt;p&gt;                Think about a online shopping site, You are buying from a online shopping store. You hava submitted the form by clicking submit button screen comes with &quot;Please wait....&quot; and stays for a long you worried and press submit again what happens the first click submitted first shopping and second click requests for the same order again. And you have to pay for same item twice.&lt;/p&gt;&lt;p&gt;               This is only the one case you may be pay twice, there are many others factors which can affects.&lt;/p&gt;&lt;b&gt;Solution :&lt;/b&gt;&lt;br /&gt;&lt;ol type=&quot;1&quot;&gt;&lt;li&gt;&lt;b&gt;Disable submit button on first time it is clicked.&lt;/b&gt;&lt;br /&gt;        This helps prevent duplicate form submission from user aggresion impatient. But not from the others mentioned above.&lt;br /&gt;E.g. &amp;lt;input type=&quot;submit&quot; name=&quot;submit&quot; onclick=&quot;this.disabled=&#39;true&#39;;/*your other function calling*/&quot;&amp;gt;&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Using Struts :&lt;/b&gt;&lt;/li&gt;&lt;li style=&quot;list-style-type: none;&quot;&gt;&lt;ol type=&quot;A&quot;&gt;&lt;li&gt;&lt;b&gt;By forwarding twice :&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;               &lt;/strong&gt; The easy solution to this problem is to use HTTP redirect after the form submission. Suppose that the CustomerForm submission results in showing a page called Success.jsp. When HTTP redirect is used, the URL in the URL bar becomes /App1/Success.jsp instead of /App1/submitCustomerForm.do. When the page  refreshed, it is the Success.jsp that is loaded again instead of  App1/submitCustomerForm.do. Hence the form is not submitted again. To use the HTTP redirect feature, the forward is set as follows:&lt;br /&gt;&lt;br /&gt;&amp;lt;forward name=”success” path=”/Success.jsp” redirect=”true” /&amp;gt;&lt;br /&gt;&lt;br /&gt;            However there is one catch. With the above setting, the actual JSP name is shown in the URL. Whenever the JSP name appears in the URL bar, it is a candidate for ForwardAction. Hence change the above forward to be as follows:&lt;br /&gt;&lt;br /&gt;&amp;lt;forward name=”success” path=”/GotoSuccess.do” redirect=”true” /&amp;gt;&lt;br /&gt;&lt;br /&gt;Where GotoSuccess.do is another action mapping using ForwardAction as follows:&lt;br /&gt;&lt;br /&gt;&amp;lt;action path=”/GotoSuccess” type=”org.apache.struts.actions.ForwardAction” parameter=”/Success.jsp” validate=”false” /&amp;gt;&lt;br /&gt;&lt;br /&gt;            Now, you have now addressed the duplicate submission due to accidental refreshing by the customer Problem : It does not prevent you from intentionally going back in the browser history and submitting the form again. Malicious users might attempt this if the form submissions benefit them or adversely impact the server.&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;b&gt;Synchronizer Token :&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;              Struts provide a token system which help solving this problem. Steps of implementation :&lt;br /&gt;&lt;ol type=&quot;1&quot;&gt;&lt;br /&gt;&lt;li&gt;add follwing line in the first(the page containing form you like to prevent from duplication) page or servlet,&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;&amp;lt;% String token = TokenProcessor.getInstance().generateToken(request); session.setAttribute(&quot;org.apache.struts.action.TOKEN&quot;&lt;br /&gt;, token); %&amp;gt;&lt;br /&gt;&lt;li&gt;make a hidden field in the form as follow,&lt;/li&gt;&lt;br /&gt;&amp;lt;input type=&quot;hidden&quot;&lt;br /&gt;name=&quot;&lt;%=.TOKEN_KEY%&gt;&quot;&lt;br /&gt;value=&quot;&lt;%=token%&gt;&quot;/&amp;gt;&lt;br /&gt;&lt;br /&gt;&lt;li&gt;Now in the Action class you created, change code as follow,&lt;br /&gt;&lt;/li&gt;&lt;br /&gt;.... if(isTokenValid(request)){&lt;br /&gt;// your code if form submitted first time&lt;br /&gt;} else {&lt;br /&gt;// your code if form is duplicate.&lt;br /&gt;}&lt;br /&gt;saveToken(request);&lt;br /&gt;.....&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/li&gt;&lt;/ol&gt;</description><link>http://thejavatrail.blogspot.com/2009/03/prevent-duplicate-form-submission.html</link><author>noreply@blogger.com (Hardik)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-6340127670273595163</guid><pubDate>Fri, 27 Feb 2009 06:35:00 +0000</pubDate><atom:updated>2009-09-03T15:25:09.913+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Framework</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Struts - What &amp; Why?</title><description>&lt;span style=&quot;font-weight: bold;&quot;&gt;What is Struts?&lt;/span&gt;&lt;br /&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Struts is a Java MVC framework for building web applications on the J2EE platform.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;How and where Struts fits in the big picture?&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Presentation Tier Strategies Technologies used for the presentation tier can be roughly classified into three categories:&lt;br /&gt; • Markup based Rendering (e.g. JSPs)&lt;br /&gt; • Template based Transformation (e.g. XSLT)&lt;br /&gt; • Rich content (e.g. Macromedia Flash)&lt;br /&gt;&lt;br /&gt; We will start by introducing the two modes of designing JSPs - Model 1 and Model 2 architectures in the next two sections and then arrive at Struts as an improvement over the Model 2 architecture.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Model 1 Architecture&lt;/span&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU8txOCIfuRpVMHlgtFiNgfn4pyK6k8yfKLCCvdxefXJ4qdmLeN7fHk-Hl-Tv8DX4zsLY5wNZ4hiRHYR82h01jroc8uorbM-_w-96HHdcPjGh_2IjCrYnBV4jQOhDXKj4V34gVL6OF0ls/s1600-h/model1_architecture.bmp&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 320px; height: 135px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU8txOCIfuRpVMHlgtFiNgfn4pyK6k8yfKLCCvdxefXJ4qdmLeN7fHk-Hl-Tv8DX4zsLY5wNZ4hiRHYR82h01jroc8uorbM-_w-96HHdcPjGh_2IjCrYnBV4jQOhDXKj4V34gVL6OF0ls/s320/model1_architecture.bmp&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5308533221277028914&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt; Let us illustrate the operation of Model 1 architecture with an example. Consider a HTML page with a hyperlink to a JSP. When user clicks on the hyperlink, the JSP is directly invoked. The servlet container parses the JSP and executes the resulting Java servlet. The JSP contains embedded code and tags to access the Model JavaBeans. The Model JavaBeans contains attributes for holding the HTTP request parameters from the query string. In addition it contains logic to connect to the middle tier or directly to the database using JDBC to get the additional data needed to display the page. The JSP is then rendered as HTML using the data in the Model JavaBeans and other Helper classes and tags.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Problems with Model 1 Architecture&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; • In Model 1 architecture, the presentation logic usually leads to a significant amount of  java code embedded in the JSP in the form of scriptlets. This is ugly and maintenance nightmare even for experienced Java developers.&lt;br /&gt;&lt;br /&gt; • Application control is decentralized in Model 1 architecture since the next page to be  displayed is determined by the logic embedded in the current page. Decentralized navigation control can cause headaches.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Model 2 Architecture - MVC&lt;/span&gt;&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgTek2joVDWBKFtM5qnp9Uv4CcIHMUqiTKd5Zz2G41dv5d04zNZsH4yTVfvliSNIHNctaN1srGkROOygm_pDcy36_XED4HU_Oe0ZYUFli9WhbBmD4D5lRs4PoG_i7RZdrRNru7uwlML18/s1600-h/model2_architecture.bmp&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 320px; height: 149px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgTek2joVDWBKFtM5qnp9Uv4CcIHMUqiTKd5Zz2G41dv5d04zNZsH4yTVfvliSNIHNctaN1srGkROOygm_pDcy36_XED4HU_Oe0ZYUFli9WhbBmD4D5lRs4PoG_i7RZdrRNru7uwlML18/s320/model2_architecture.bmp&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5308533410666256994&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt; The main difference between Model 1 and Model 2 is that in Model 2, a controller handles the user request instead of another JSP. The controller is implemented as a servlet.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Advantages of Model 2 Architecture&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; • Since there is no presentation logic in JSP, there are no scriptlets. This means lesser nightmares.&lt;br /&gt;&lt;br /&gt; • With MVC you can have as many controller servlets in your web application. In fact you can have one Controller Servlet per module.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Why do we need Struts? (Controller gone bad – Fat Controller)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt; If MVC is all that great, why do we need Struts after all? The answer lies in the difficulties associated in applying bare bone MVC to real world complexities. In medium to large applications, centralized control and processing logic in the servlet – the greatest plus of MVC is also its weakness. Consider a mediocre application with 15 JSPs. Assume that each page has five hyperlinks (or five form submissions). The total number of user requests to be handled in the application is 75. Since we are using MVC framework, a centralized controller servlet handles every user request. For each type of incoming request there is “if” block in the doGet method of the controller Servlet to process the request and dispatch to the next view. For this mediocre application of ours, the controller Servlet has 75 if locks. Even if you assume that each if block delegates the request handling to helper classes it is still no good. You can only imagine how bad it gets for a complex enterprise web application. So, we have a problem at hand. The  controller Servlet that started out as the greatest thing next to sliced bread has gone bad. It has put on a lot of weight to become a Fat Controller.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;First Look at struts&lt;/span&gt;&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxoaTgtQFjO1MjxXDXFwNbeUst0HVakw1Tcxz_zUgeOgS8ryTqjcbBs2qV_o-fafBDscuxF8xhN7hb0JK0cMNJ8OK7Bjhi1UsR3U6L6nEmxYMWMewFCbhKWk0juZP1R0bNgNXKzDbV1V8/s1600-h/struts_architecture.jpg&quot;&gt;&lt;img style=&quot;margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 386px; height: 282px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxoaTgtQFjO1MjxXDXFwNbeUst0HVakw1Tcxz_zUgeOgS8ryTqjcbBs2qV_o-fafBDscuxF8xhN7hb0JK0cMNJ8OK7Bjhi1UsR3U6L6nEmxYMWMewFCbhKWk0juZP1R0bNgNXKzDbV1V8/s320/struts_architecture.jpg&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5308532397351986914&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;br /&gt; In Struts, there is only one controller servlet for the entire web application. This controller servlet is called ActionServlet and resides in the package org.apache.struts.action. It intercepts every client request and populates an ActionForm from the HTTP request parameters. ActionForm is a normal JavaBeans class. It has several attributes corresponding to the HTTP request parameters and getter, setter methods for those attributes. You have to create your own ActionForm for every HTTP request handled through the Struts framework by extending the org.apache.struts.action.ActionForm class. Consider the following HTTP request for http://localhost:8080/App1/create.do?firstName=John&amp;amp;lastName=Doe. Suppose class MyForm extends the org.apache.struts.action.ActionForm class and contains two attributes – firstName and lastName. It also has getter and setter methods for these attributes. For the lack of better terminology, let us coin a term to describe the classes such as ActionForm – View Data Transfer Object. View Data Transfer Object is an object that holds the data from html page and transfers it around in the web tier framework and application classes.&lt;br /&gt;&lt;br /&gt; The ActionServlet then instantiates a Handler. The Handler class name is obtained from an XML file based on the URL path information. This XML file is referred to as Struts configuration file and by default named as struts-config.xml. The Handler is called Action in the Struts terminology. And you guessed it right! This class is created by extending the Action class in org.apache.struts.action package. The Action class is abstract and defines a single method called execute(). You override this method in your own Actions and invoke the business logic in this method. The execute() method returns the name of next view (JSP) to be shown to the user. The ActionServlet forwards to the selected view.&lt;/div&gt;</description><link>http://thejavatrail.blogspot.com/2009/02/what-is-struts-struts-is-java-mvc.html</link><author>noreply@blogger.com (Hardik)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU8txOCIfuRpVMHlgtFiNgfn4pyK6k8yfKLCCvdxefXJ4qdmLeN7fHk-Hl-Tv8DX4zsLY5wNZ4hiRHYR82h01jroc8uorbM-_w-96HHdcPjGh_2IjCrYnBV4jQOhDXKj4V34gVL6OF0ls/s72-c/model1_architecture.bmp" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-5218617899981782691</guid><pubDate>Fri, 13 Jun 2008 12:38:00 +0000</pubDate><atom:updated>2008-06-13T22:36:41.899+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Interoperability</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">JNI</category><title>Hello World With JNI</title><description>&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Java Native Interface (JNI) can be used to call native code from your Java application. I needed to call a function from a DLL file. Basically JNI addresses interoperability issues and lets your Java code access legacy system. Here I want to share basic “Hello World” application with you which I developed using JNI.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Starting with Java side, all you need to keep in mind while coding is that you have to declare signature of all legacy methods you need to access. All those methods must be declared with native modifier.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Secondly you need to load native library. That you can achieve using &lt;i style=&quot;&quot;&gt;System.loadLibrary()&lt;/i&gt;. It will try to load library file from basic operating system library location. e.g. WINDOWS\system32 in windows operating system. If you want to provide a specified location, you can use &lt;i style=&quot;&quot;&gt;System.load(String filePath) &lt;/i&gt;method.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;This is how I code &lt;i style=&quot;&quot;&gt;HelloWorld.java.&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;    &lt;p class=&quot;MsoNormal&quot; style=&quot;color: rgb(0, 0, 0);&quot;&gt;&lt;b&gt;&lt;span style=&quot;&quot;&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;br /&gt;package com.jay.jni;&lt;br /&gt;&lt;br /&gt;public class HelloWorld&lt;br /&gt;{&lt;br /&gt;       static&lt;br /&gt;       {&lt;br /&gt;         System.loadLibrary(&quot;TestDll&quot;);&lt;br /&gt;    }&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;br /&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    public static void main(String ar[])&lt;br /&gt;   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;{&lt;br /&gt;       &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;System.out.println(&quot;Hello world from Java&quot;);&lt;br /&gt;       &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;HelloWorld t=new HelloWorld();&lt;br /&gt;       &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;String strFromDLL = t.inDll();&lt;br /&gt;     &lt;br /&gt;       &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;System.out.println(&quot;&quot;+strFromDLL);&lt;br /&gt;   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;}&lt;br /&gt;   &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;br /&gt;    &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;public native String inDll();&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 0);font-size:85%;&quot; &gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Next step is to create a header file. For that you need to run following command&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;i style=&quot;&quot;&gt;javah -jni com.jay.jni.HelloWorld&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p  style=&quot;color: rgb(0, 0, 0); text-align: justify;font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;This will create a header file with name “&lt;i style=&quot;&quot;&gt;com_jay_jni_HelloWorld.h&lt;/i&gt;”. Generate header file will be as follow.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;i style=&quot;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;/* DO NOT EDIT THIS FILE - it is machine generated */&lt;br /&gt;#include &lt;jni.h&gt;&lt;br /&gt;/* Header for class com_jay_jni_HelloWorld */&lt;br /&gt;&lt;br /&gt;#ifndef _Included_com_jay_jni_HelloWorld&lt;br /&gt;#define _Included_com_jay_jni_HelloWorld&lt;br /&gt;#ifdef __cplusplus&lt;br /&gt;extern &quot;C&quot; {&lt;br /&gt;#endif&lt;br /&gt;/*&lt;br /&gt;* Class:     com_jay_jni_HelloWorld&lt;br /&gt;* Method:    inDll&lt;br /&gt;* Signature: ()Ljava/lang/String;&lt;br /&gt;*/&lt;br /&gt;JNIEXPORT jstring JNICALL Java_com_jay_jni_HelloWorld_inDll&lt;br /&gt; (JNIEnv *, jobject);&lt;br /&gt;&lt;br /&gt;#ifdef __cplusplus&lt;br /&gt;}&lt;br /&gt;#endif&lt;br /&gt;#endif&lt;/jni.h&gt;&lt;/span&gt;&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/p&gt;  &lt;p style=&quot;color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/p&gt;  &lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;b style=&quot;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;span style=&quot;font-family:verdana;&quot;&gt;Note the package name in header file. Most of the time people copies code from internet and tries to run program after modification in package and/or class name. You need to be very careful while dealing with JNI.&lt;/span&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Now comes native code portion. I created a MFC DLL project in VC++.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Once you create a simple DLL project, add three header files into your project.&lt;/span&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;i style=&quot;&quot;&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;i style=&quot;&quot;&gt;1.    com_jay_jni_HelloWorld.h&lt;br /&gt;2.    jni.h&lt;br /&gt;3.    jni_md.h&lt;br /&gt;&lt;br /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;div style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Second and third can be found in &lt;i style=&quot;&quot;&gt;&lt;java_home&gt;\include&lt;/java_home&gt;&lt;/i&gt; folder. Then you need to implement your native function.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;color: rgb(0, 0, 0);&quot;&gt;    &lt;/div&gt;&lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;For that I created a header file my application &lt;i style=&quot;&quot;&gt;TesetDLL &lt;/i&gt;with name &lt;i style=&quot;&quot;&gt;TestDLL.h.&lt;/i&gt; Then I added following code into in it at last.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;text-align: justify; color: rgb(0, 0, 0);&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;JNIEXPORT jstring JNICALL Java_com_jay_jni_HelloWorld_inDll (JNIEnv * env, jobject jobj)&lt;br /&gt;{&lt;br /&gt;   jstring js = (env)-&gt;NewStringUTF(&quot;Hello From DLL&quot;);&lt;br /&gt;   return js;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;That’s all; I build it and created a DLL. Now all I need to do is put my DLL in system library folder (as I used loadLibrary) and run my program.&lt;/span&gt;&lt;/p&gt;</description><link>http://thejavatrail.blogspot.com/2008/06/hello-world-with-jni.html</link><author>noreply@blogger.com (Jay Rajani)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-3595585352812650883.post-6175848350712781248</guid><pubDate>Thu, 12 Jun 2008 08:23:00 +0000</pubDate><atom:updated>2008-06-12T14:07:01.441+05:30</atom:updated><category domain="http://www.blogger.com/atom/ns#">Eclipse</category><category domain="http://www.blogger.com/atom/ns#">J2EE</category><title>Controlling J2EE Module Depedencies</title><description>&lt;div  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;J2EE Module Dependencies is a critical issue in an enterprise application. You need your one web module to access some resources while prohibiting others. Similar is the case with EJB modules.  &lt;/span&gt;&lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Let’s figure it out with Eclipse Web Tools Platform. I consider that you all have ability to create projects and modules in Eclipse; so skipping those steps. I have Eclipse 3.3 with WTP 2.0 M6.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;I have created project with following details.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;MyProject – &lt;st1:place st=&quot;on&quot;&gt;&lt;st1:city st=&quot;on&quot;&gt;Enterprise&lt;/st1:city&gt;&lt;/st1:place&gt; project&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;MyEJB_Module – Contains bean class&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;MyEJBClient_Module – Contains interfaces of EJB, VO, and BeanUtil.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;MyWeb_Modules – Holds JSPs, servlets and all presentation tier stuffs.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;MyJava_Module – Contains common classes like constants, utilities. &lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9P1sn4-dpxUjtsl9s3aqU14t2D5aA63R17ng5rTyMQcOrpG3l5PqRaP1cApWNxm_DmQH_f3znjzadxyXv5oQQZRJ7J9WMajmxtmEoNgKdJyq6K7Rh-vdGZyjxvGvsTJjt3yIaI0ndkFcj/s1600-h/1.JPG&quot;&gt;&lt;img style=&quot;margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9P1sn4-dpxUjtsl9s3aqU14t2D5aA63R17ng5rTyMQcOrpG3l5PqRaP1cApWNxm_DmQH_f3znjzadxyXv5oQQZRJ7J9WMajmxtmEoNgKdJyq6K7Rh-vdGZyjxvGvsTJjt3yIaI0ndkFcj/s400/1.JPG&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5210909873059317586&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Starting with MyProject, select properties from right click menu. In property dialog, select “J2EE Module Dependencies”. You will find all the modules. Select all as project is dependent on all modules.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi60RyysgcLc9JHHYTs5v7HOHs7KurelfefB7YEKUFU5rZRvo4oxMSn80OKS1y92CpAv__6krrQqfnaNJbbSLmBJpiEdi5mdjIBr9qOOxXzA9A0PditjW6liOd-3Z8R7t7L-5DmjB25GRql/s1600-h/2.JPG&quot;&gt;&lt;img style=&quot;margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi60RyysgcLc9JHHYTs5v7HOHs7KurelfefB7YEKUFU5rZRvo4oxMSn80OKS1y92CpAv__6krrQqfnaNJbbSLmBJpiEdi5mdjIBr9qOOxXzA9A0PditjW6liOd-3Z8R7t7L-5DmjB25GRql/s400/2.JPG&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5210909153165019714&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;Now you can configure dependency of individual module, let’s take an example of web module. In “J2EE Module Dependencies” window, you will find a radio button group which would has by default “Use EJB Client Jars” option selected. That radio button groups defines visibility of EJB jar. &lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvkYMEMAW2p7dbjYQauBr5v4i8thPI4Jo6DFXxYUKh36arJ_oZzPUy3WQ4ZXXudLYNcIEINBdbJvAO7dBmUg0LObNyHV9Qh8dCqKjxRbUp-fb82pLde2S5E_Kux1_QTybw44hj8ZwGONKg/s1600-h/3.JPG&quot;&gt;&lt;img style=&quot;margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhvkYMEMAW2p7dbjYQauBr5v4i8thPI4Jo6DFXxYUKh36arJ_oZzPUy3WQ4ZXXudLYNcIEINBdbJvAO7dBmUg0LObNyHV9Qh8dCqKjxRbUp-fb82pLde2S5E_Kux1_QTybw44hj8ZwGONKg/s400/3.JPG&quot; alt=&quot;&quot; id=&quot;BLOGGER_PHOTO_ID_5210909405404446114&quot; border=&quot;0&quot; /&gt;&lt;/a&gt;As clients of any EJB should be isolated from bean classes, it is preferable not to select other options where you can define dependencies with EJB jar file. After all, EJB clients should not have any kind of dependencies with EJB jar.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Now here if I select MyEJB_ModuleClient.jar only, MyWeb_Module won’t able to access classes defined in MyJava_Module until you redefine module dependencies.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;Similarity you can define dependencies with Utility project, specifically saying third party libraries. You want one module to access one jar and isolate others all you need to do is add library in main project (MyProject in my case) and then configure dependencies of other modules.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;This feature takes care of dependencies on behalf of you and let you concentrate on other tasks.&lt;/span&gt;&lt;/p&gt;&lt;div style=&quot;text-align: justify; font-family: verdana; color: rgb(0, 0, 0);&quot;&gt;  &lt;/div&gt;&lt;p  style=&quot;text-align: justify; color: rgb(0, 0, 0);font-family:verdana;&quot; class=&quot;MsoNormal&quot;&gt;&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;</description><link>http://thejavatrail.blogspot.com/2008/06/j2ee-module-dependencies-is-critical.html</link><author>noreply@blogger.com (Jay Rajani)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9P1sn4-dpxUjtsl9s3aqU14t2D5aA63R17ng5rTyMQcOrpG3l5PqRaP1cApWNxm_DmQH_f3znjzadxyXv5oQQZRJ7J9WMajmxtmEoNgKdJyq6K7Rh-vdGZyjxvGvsTJjt3yIaI0ndkFcj/s72-c/1.JPG" height="72" width="72"/><thr:total>0</thr:total></item></channel></rss>