Getting started with JavaEE

Before we Start!

Theory to be understood

Why J2EE ?

Before we go and have fun implementing our first Java EE Project lets first know some theory about it

JavaEE was developed to overcome many problems that internet were facing to implement Business process in its starting stage:
  1. Statelessness of HTTP protocol: which made it hard to make dynamic web Application because no current state of HTTP was save on server.
  2. No Database (Model): there was no such technology which can be used to store data on server and manipulate whenever we want to overcome Statelessness of HTTP protocol.

3 tier Application:(The solution): Model View Controller (MVC)

to over come the problems mentioned above the 3 tier Application was developped which contains Model View and Controller as each tier.

  1. Model - Stores the data in relational schemas in organised manner so it can easily be access and modified as required.
  2. View - deals with the user interface and experience called front end its what you see in browser
  3. Controller - its business logic that is designed as a communication medium between model and view, it process the user request and provide respond and also communicates with database to store or get data from it.
3 tiers Application MVC structure
Please see below video to understand MVC easily by Hitesh Choudhary


How Business process is implemented using J2EE:

Java Enterprise is implemented in the Model View Controller software design pattern as Servlets in the Controller Layer, and JSP/ Java Faces in the View Layer.

We study the required business processes – such as Assigning a Student to a program group – and we write the algorithm of our Servlets (as well as our SQL statements) to implement those businesses in SQL and Java Logic

Unified Modeling Language (UML): 



Design Phases of J2EE Application

Above image shows the development process of J2EE application we cannot start developing the application. We need to design the visual plan from requirement using UML diagrams in design phase.


  • It is a visual language with symbols to represent classes and databases.
  • We use UML to design our algorithm before we start coding.
  • It gives you a visual framework to make good design decisions before you start writing code or designing databases.

The J2EE Application Concepts:

Servlets:

A Java servlet processes or stores a 
Java class in Java EE that conforms to the Java Servlet API, a standard for implementing Java classes that respond to requests. Servlets could in principle communicate over any client–server protocol, but they are most often used with the HTTP. Thus "servlet" is often used as shorthand for "HTTP servlet". Thus, a software developer may use a servlet to add dynamic content to a web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML and more commonly, JSON. Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL mapping.

Facelet:

The term Facelets refers to the view declaration language for JavaServer Faces technology. JavaServer Pages (JSP) technology, previously used as the presentation technology for JavaServer Faces, does not support all the new features available in JavaServer Faces in the Java EE 6 platform. JSP technology is considered to be a deprecated presentation technology for JavaServer Faces. Facelets is a part of the JavaServer Faces specification and also the preferred presentation technology for building JavaServer Faces technology-based applications.

EJBs:

EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE platform. J2EE platform has component based architecture to provide multi-tiered, distributed and highly transactional features to enterprise level applications. The Java EE model, and more specifically, the Sun Microsystems Java™ Servlet 2.3 Specification, defines a Web application directory structure that specifies the location of Web content files, class files, class paths, deployment descriptors, and supporting metadata. The Web project hierarchy mirrors that of the Web application created from a project. In the workbench, you can use the New Web Project wizard to create a new Web project. The main project folder contains all development objects related to a Web application. The Web content folder contains the elements of the project necessary to create a Web application. This folder structure maps to the Web application archive (WAR) structure defined by Sun Microsystems.

Dynamic Web projects and applications

There are two types of Web projects: dynamic and static. Dynamic web projects can contain dynamic Java EE resources such as servlets, JSP files, filters, and associated metadata, in addition to static resources such as images and HTML files. Static web projects only contains static resources. When you create Web projects, you can include cascading style sheets and JSP tag libraries (for dynamic Web projects), so that you can begin development with a richer set of project resources. Dynamic Web projects are always embedded in Enterprise Application projects. The wizard that you use to create a dynamic Web project will also create an Enterprise Application (EAR) project if it does not already exist.

Web Deployment Descriptor

The standard Web application deployment descriptor (the web.xml file).

JavaSource

Contains the project's Java source code for classes, beans, and servlets. When these resources are added to a Web project, they are automatically compiled and the generated files are added to the WEB-INF/classes directory. The contents of the source directory are not packaged in WAR files unless an option is specified when a WAR file is created. Note: Though the default name given to the folder is JavaSources, you can change the name by right clicking on the name in the Project Explorer and clicking on Refactor > Rename.

META-INF

This directory contains the MANIFEST.MF file, which is used to map class paths for dependent JAR files that exist in other projects in the same Enterprise Application project. An entry in this file will update the run-time project class path and Java build settings to include the referenced JAR files.

WEB-INF

Based on the Sun Microsystems Java Servlet 2.3 Specification, this directory contains the supporting Web resources for a Web application, including: the web.xml file the classes lib directories

/classes

This directory is for servlets, utility classes, and the Java compiler output directory. The classes in this directory are used by the application class loader to load the classes. Folders in this directory will map package and class names, as in: /WEB-INF/classes/com/mycorp/servlets/MyServlet.class. Do not place any .class files directly into this directory.

/lib The supporting JAR files that your Web application references.

J2EE Floder structure

Getting Hands Dirty

Pre-requisite:


  1. Netbeans: Download
  2. XAMPP: Download
  3. MySQL workbench: Download
  4. MySQL (OSX users only): Download select exact version of OS X

Lets make our first J2EE project:

- Install all the softwares listed in pre-requisite.
  • Note: Step 4 is only for Mac(OS X users):
  • Important: in step 4 After installation you will get temporary password please save it in text file
           looks like this:
             2018-11-25T01:48:14.669578Z 1 [Note] A temporary password is generated for root@localhost: <password> 
             If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference
             manual.
- Start XAMPP
  • Once you open XAMPP there are Configuration file and Logs files you can use configuration files to configure servers and to set user name and password for tomcat server in our case
  • Log files are used to see if something goes wrong , what's wrong?, what's causing error? etc...
  • You can also navigate to this files manually from C:/xampp/tomcat/conf folder
  • and press start Apache, Tomcat and MySQL servers

XAMPP windows


  - For Mac Users step to set MySQL root password from given temporary password which you save in text file while installing

Open terminal and run following commands
> touch .bash_profile
> open -t .bash_profile
> now text editor will be opened add new line
   export PATH=${PATH}:/usr/local/mysql/bin/
   Save and exit editor and terminal



Open terminal again and run following commands
> mysql -u root -p
    Enter password: <paste temporary you got at installation>
ALTER USER 'root'@'localhost' IDENTIFIED BY '';
> exit




this will set root password to blank

- Start MySQL Workbench

Goto new Connection

Enter connection name: MySQL
User name: root
Press test connection press ok when asked for password leaving password blank
Press OK



- Now open my SQL

  1. create new Database college_enrollment_system
  2. Add table students with coloums student_id, first_name, last_name.




- Now Open Netbeans:


  1. Goto services tab
  2. Expan MySQL server at local host
  3. you will see database we created names college_enrollment_system right click on it and connect
  4. Now goto Files tab and select new file Enter sql file in search bar enter file name and finish
- Now paste following code in SQL file

insert into students(student_id,first_name,last_name) values (735691,"Himanshu", "Patel");
insert into students(student_id,first_name,last_name) values (734342,"Shubham", "Mourya");
select * from students;

and run the file and see the result data will be inserted in the database



 Hope you got everything to know about the JavaEE




Comments