You are on page 1of 47

MAVEN:

—-----
In general, while preparing projects , Java
developers have to perform the following activities .
1.Preparing Project Structure
2.Downloading Dependencies[Jars] and attach all the
dependencies to the project.
3.Compile the Code
4.Unit Testing
5.Generating Test reports
6.Start the server
7.Deploying web application
8.Generating Documentation
9.Undeploying web application
10. Stop the server
—--------
—---------

If we perform all the above project development


activities manually by the developers , Developers
may get a burden , it may take a lot of time ,it may
impact the development process in the form of more
development time, more development cost,....

To overcome the above problem we need to have an


alternative , where all the above tasks must be
performed automatically by using some third party
tools called “Build Tools”.
EX: Ant, MAVEN, GRADLE,...
Where ANT build tool is outdated, not supported by
the at present tools and technologies.

Where MAVEN and GRADLE are used in the applications


development at present.

MAVEN is a “Yiddish” Word, it is a German Language


word, its meaning is “Accumulator of Knowledge”.

MAVEN is able to perform all the above development


activities more perfectly than ANT.

MAVEN follows “Convention Over Configuration”


principle, where all the default Configurations are
provided by the MAVEN to the projects , where
developers are required to thing about their actual
application logic instead of the project
configuration

MAVEN Architecture:
Where a plugin is an action or development activity , when
we select a plugin in Maven , automatically MAVEN software
will perform the following actions.

1. MAVEN will listen to the plugin that we selected.


2. MAVEN will get all the configuration details from
pom.xml w.r.t the plugin what we selected.
3. MAVEN will perform the respective action over the
project.
4. The result of the MAVEN plugin will be displayed on
MAVEN Console.

EX: If we select a test plugin in MAVEN, then MAVEN will get


the test configurations from a pom.xml file like JUNIT
configurations, Java configurations,... MAVEN will execute
the test cases over the project and the result of test cases
will be provided on console.
pom.xml[Project Object Model]
—--------------------------------
pom file is called Project Configuration file, it is able to
provide all the maven configuration details to the project.

In general, in applications POM file is able to provide the


following configuration details.
1. Project Description
2. Repositories
3. Dependency Management
4. Build Configurations
5. Profiles configurations
6. Project Inheritance
—-------
—------
—------
Project Description:
—-------------------
It is able to provide some description about the project
which includes project name, maven model version, project
version, type of packaging,......

To provide project description we will use the following


tags in the pom.xml file.

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>appName</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Logical Name of the project</name>
<description> description about the
project</description>
<packaging>jar/war/pom</packaging>
</project>

<project> tag is a root tag in pom.xml file, it is able to


represent all the configuration of the present project.

Where <modelVersion> tag is able to specify the code that


represents the MAVEN version. Code 4.0.0 is able to support
the MAVEN 2.x version and above.

Where <groupId> tag is able to provide an unique identity


for the project, that is the reverse of the company domain
name.
EX: com.durgasoft

Where <artifactId> is able to provide an application name.

Where <version> tag is able to provide project version


number.

Where <name> tag is able to provide the logical name of the


project.

Where <description> tag is able to provide some description


about the project.

Where <packaging> tag is able to provide application package


type like jar or war or,.... To submit to the server admins.
Repositories:
—-------------
IN general, MAVEN is providing directory structures to the
applications depending on our selection.

MAVEN is providing all the dependent jar files to the


applications as per our selection.

In the above context, MAVEN is getting all the dependents


from a location called “Repository”.

In general, there are three types of Repositories.


1. Local Repository
2. Central Repository
3. Remote Repository

Local Repository:
It is a location in our computer,it will be created when we
execute the first maven application after installation of
the maven.

In general, MAVEN will create a local repository in our


system at the following location.

“C:\Users\{User}\.m2\repository”

Central Repository:
It is a location in the web[Website] maintained by Apache
Software Foundations and some other third party
organizations collaboration, it is a frequently used
repository in Maven projects, it is able to provide almost
all the dependencies which are free sources.

MAVEN uses the following url as default central repository.


“https://repo.maven.apache.org/maven2”.

As per the requirement , we can use our own repositories as


a central repository , but we must configure that in our
pom.xml file.

<project>
—----
<repositories>
<repository>
<central>
<id>central</id>

<url>https://repo.nexus.repository/maven</url>
</central>
</repository>
</repositories>
—-----
</project>

Remote Repository:
It is a location in our company , it is able to provide our
project specific dependencies.

If we want to use Remote Repositories we must configure that


repositories in the pom.xml file.

<project>
—----
<repositories>
<repository>
<remoteRepository>
<id>remote</id>
<url>https://com.durgasoft.libraries</url>
</remoteRepository>
</repository>
</repositories>
—-----
</project>

When we execute the Maven project, MAVEN Software will


perform the following actions.

1. MAVEN software will go to the pom.xml file and search


for the dependencies configuration.
2. If Maven Software identifies any dependency
configuration then MAVEN software will read dependencies
from the pom.xml file.
3. MAVEN software will search for the dependencies at local
Repository,if the dependencies are available at local
repository then MAVEN software will attach the
dependencies to the project.
4. IF the required dependencies are not available at Local
Repository then MAVEN software will search for them in
the Central Repository.
5. If the required dependencies are available at the
central repository then MAVEN software will load all
these dependencies to the Local Repository then It will
attach the dependencies to the project.
6. If the required dependencies are not available at
central repository then MAVEN software will search for
the Remote repository configurations in pom.xml file, if
no remote repository configurations in pom.xml file then
Maven will stop searching dependencies and generate
Build fail status.
7. If the required Remote Repository configuration exists
in the pom.xml file then MAVEn software will search for
the required dependencies at Remote repository.
8. If the required dependencies are available in Remote
Repository then MAVEN will load these dependencies to
the Local Repository then attach these dependencies to
the project.
9. Even at the Remote Repository if the dependencies are
not available then MAVEN stop searching for dependencies
and generate Build Fail status.

Dependency Management:
—------------------------
Dependency is a jar file which is required in our
application.

If we want to get any dependency from Maven , first we have


to configure those dependencies in the pom.xml file.

To provide dependencies configuration in the pom.xml file we


have to use the following tags.
<project>
—----
<dependencies>
<dependency>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
<scope></scope>
<dependency>
</dependencies>
—----
</project>

Where <groupId> tag is able to provide dependency group


names.
Where <artifactId> tag is able to provide dependency names.
Where <version> tag is able to provide
Where <version> tag is able to provide a Dependency version.
Where <scope> is able to provide dependency scope.

EX:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>run</scope>
</dependency>

If we provide the above dependency in the pom.xml file then


MAVEN will search for the dependency in the repository with
the name mysql-connector-java-8.0.30.jar under the group
mysql at the following repository location.
https://repo.maven.apache.org/maven2
https://repo.maven.apache.org/maven2
|---> mysql
|---mysqlconnector-java
|---8.0.30
|--mysql-connector-java-8.0.30.jar

Dependencies Scope:
—--------------------
In general, In the projects we need dependencies for the
following activities of the project.
1. Compilation of the project
2. Testing the Project
3. Running the project

In dependencies configuration , we have to specify scope


value in order to define the availability of the present
dependency to the above three activities.

MAVEN has provided the following scopes for the


dependencies.

1. Compile
2. Provided
3. Runtime
4. Test
5. System
6. Import
Compile:
It is able to provide the dependency to all the phases of
the project like Compilation, Testing and Runtime.
EX:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
<scope>compile</scope>
</dependency>

Provided:
It is able to provide the dependency for compilation and
testing but not for runtime.
EX:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>

Runtime:
It is able to provide the dependency at runtime and at
testing.
EX:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
Test:
It is able to provide the dependency for the project
testing.
EX:

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>

System:
It will not provide dependency by searching in the
repositories, it will provide the dependency from the sun
directory structures in our project.

It is an outdated scope, not supported in the present


applications.

import:
It is a deprecated scope , not utilized in the present
applications, it will be used to override the pom
configurations by importing super pom configurations.

Transitive Dependencies:
—-------------------------
One dependency is dependent on another dependency, this
process is called “Transitive Dependency”, MAVEN is able to
get all the dependencies in transitive dependency mode only.
Build Configurations:
—--------------------
The main purpose of Build configuration is to configure
plugins.

MAVEN has provided a number of plugins to perform project


activities.

1. Compile: To compile the java resources in projects


2. Run: To run java application
3. Test: to test java application
4. Install: to generate our own artifact.
5. Clean: to delete all the dependencies from the project
6. testReposts: To generate test reports.
7. Document: To generate project documentation
—-----
—------

If we want to provide plugin configuration then we have to


use the following tags.

1. <project>
2. ...
3. <build>
4. <pluginManagement>
5. <plugins>
6. <plugin>
7. <groupId>org.apache.maven.plugins</groupId>
8. <artifactId>maven-compiler-plugin</artifactId>
9. <version>3.11.0</version>
10. <configuration>
11. <source>1.17</source>
<target>1.17</target>
12. </configuration>
13. </plugin>
14. </plugins>
15. </pluginManagement>
16. </build>
17. …
18. </project>

In Maven we are able to compile versions by using the


following properties.
<project>
—---
<properties>
<maven.compiler.source>1.17</maven.compiler.source>
<maven.compiler.target>1.17</maven.compiler.target>
</properties>
—---
</project>

Profiles configurations:
—-------------------------
In general, in any Project , there are three main lifecycle
stages.
1. Development
2. Testing Mode
3. Production Mode

In general, we will provide different configurations for


each and every above Lifecycle stages in a project, in this
context if we provide the configurations on the basis of
Project Lifecycle mode manually then it may be a burden to
the developers , there is a chance to miss some of the
configurations in manual approach.

To overcome the above problems we need an alternative where


all the project configurations must be provided
automatically depending on the status of the project, to
achieve this requirement we have to use Profile Management
in MAVEN.

If we want to provide profiles for the project we have to


use the following tags in the pom.xml file.

1. <profiles>
2. <profile>
3. <id>profileName</id>
4. <activation>
5. <activeByDefault>true</activeByDefault>
6. </activation>
<properties>
—---------
—---------
</properties>
7. ...
8. </profile>
9. </profiles>

EX:
—--
<project>
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
—----Development configurations—----
<properties>
</profile>
<profile>
<id>test</id>
<properties>
—----Test configurations—----
<properties>
</profile>
<profile>
<id>production</id>
<properties>
—----Production configurations—----
<properties>
</profile>
</profiles>
</project>

Project Inheritance:
—---------------------
In Java applications, we are able to define inheritance
relations between classes in order to reuse one class
properties and behaviors in another class in order to
improve Code Reusability.

SImilarly, It is possible to define inheritance


relationships between pom files in order to reuse project
configuration details.

If we want to inherit one pom file configuration to another


pom file configuration then we have to use the following
steps.
1. Prepare Parent pom
2. Prepare child pom in the project

IN the parent pom we are able to provide the common


configurations of all the projects like JDK versions, Source
Code Repositories, constants, profiles configurations,......

To make a pom as parent pom we have to provide “pom” as


value to <packaging> tag in <project> tag.

EX:
Parent pom
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.durgasoft</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
—----
—----
</project>

Child pom:
<project>
—-
<parent>
<groupId>com.durgasoft</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0</version>
</parent>
—--
</project>
IN MAVEN, for each and every pom there is a common and
default super pom, which contains all the minimum
configurations of the projects, which includes default
repositories, ……

In general, super pom exists in the jar file


maven-model-builder-version.jar file at the following
location.

“org\apache\maven\model\pom.xml”.

If we want to get super pom details on the console there we


have to use the following maven command.

mvn help:effective-pom

ArcheTypes:
—-----------
Archetype is Project type in order to provide the required
directory structure and the required setups.

If we want to get all the archetypes which are supported by


MAVEN we have to use the following command.

mvn archetype:generate

MAVEN Installation and MAVEN Setups:


—--------------------------------------
1. Download MAVEN
2. Install MAVEN
3. Setup MAVEN in System
Steps to prepare First Maven application:
—------------------------------------------
1. Prepare a directory or folder for all Maven
applications.
2. Open command prompt and go to the location where we want
to generate a maven application.
3. Create a Quickstart Maven project by using the following
steps.
a. Use the following command on command prompt.
mvn archetype:generate
b. Provide project number 2011
c. Select a number .
Choose
org.apache.maven.archetypes:maven-archetype-quickst
art version:
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
7: 1.3
8: 1.4
Choose a number: 8: 8
d. Provide group Id : com.durgasoft
e. Provide artifactId: helloapp
f. Provide version : 1.0-SNAPSHOT
g. Provide package : com.durgasoft
h. Conform the above data
Confirm properties configuration:
groupId: com.durgasoft
artifactId: helloapp
version: 1.0-SNAPSHOT
package: com.durgasoft
Y: : Y

4. Write login in App.java or in AppTest.java:


App.java is normal java program
AppTest.java is JUnit test case.

App.java
package com.durgasoft;

public class App


{
public static void main( String[] args )
{
System.out.println( "Hello User!, This is from
App.java" );
}
}

AppTest.java
package com.durgasoft;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
public class AppTest
{
@Test
public void shouldAnswerWithTrue()
{
System.out.println("Hello User!, This is from
JUNIT AppTest.java");
assertTrue( true );
}
}
To run AppTest.java file use the following command on
command prompt

mvn test

To run App.java we have to use the following two approaches.


1. Go to “D:\Spring830\MAVEN
PROJECTS\helloapp\target\classes” on command prompt and
use the following command.

java com.durgasoft.App

2. Create a jar file and execute it.


a. Open the pom.xml file and provide <packaging> tag
under <project> tag.
EX:
<project>
<groupId>com.durgasoft</groupId>
<artifactId>helloapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
—----
</project>
b. Create jar file by using the following command on
command prompt:
mvn package

It will create a jar file with the name


helloapp-1.0-SNAPSHOT.jar under “D:\Spring830\MAVEN
PROJECTS\helloapp\target” location.

c. Set classpath to the above jar file.


D:\Spring830\MAVEN PROJECTS\helloapp>set
classpath=D:\Spring830\MAVEN
PROJECTS\helloapp\target\helloapp-1.0-SNAPSHOT.jar;

d. Execute jar file:


D:\Spring830\MAVEN PROJECTS\helloapp>java
com.durgasoft.App
Hello User!, This is from App.java

JDBC Applications with MAVEN:


—-----------------------------
1. Create Maven Quickstart project
2. Open pom.xml file and provide Driver dependency.
3. Provide Jdbc Application logic in the main class and
main() method.
4. Execute the Jdbc application.

If we want to use MySQL Database for Jdbc applications then


we have to get MySQL Driver dependency from the internet and
we have to add Mysql dependency to the pom.xml file.

<!--
https://mvnrepository.com/artifact/mysql/mysql-connector-jav
a -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>

EX:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.durgasoft</groupId>
<artifactId>app20</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.source
Encoding>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>

</dependencies>
</project>

Main.java
package com.durgasoft;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Main {
public static void main(String[] args) {
try(
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/
durgadb", "root", "root");
Statement statement =
connection.createStatement();
ResultSet resultSet =
statement.executeQuery("select * from emp1");
) {

System.out.println("ENO\tENAME\tESAL\tEADDR");

System.out.println("-----------------------------");
while(resultSet.next()){

System.out.print(resultSet.getInt("ENO")+"\t");

System.out.print(resultSet.getString("ENAME")+"\t\t");

System.out.print(resultSet.getFloat("ESAL")+"\t");

System.out.print(resultSet.getString("EADDR")+"\n");
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}

IN general, MAVEN is getting all the dependencies from Maven


Repository, where MAVEN Repositories are able to provide
only freeware softwares provided dependencies, Maven
Repositories are not able to provide licensed softwares
dependencies.
If we prepare JDBC application with MySQL database in Maven
then Maven is able to get MySql-Java-connector dependency
from Maven Repository as per the dependency configuration
which we provided in pom.xml file, because MySQL database is
a freeware.

Oracle Database is not freeware, its dependencies are not


provided by Maven repositories, if we want to use Oracle
database for Jdbc applications in Maven , we must create a
dependency for ojdbc11.jar file in the Local repository.

If we want to create our own dependency for ojdbc11.jar file


in the local repository then we have to use the following
command on command prompt.

D:\mavenapps\app01>mvn install:install-file -DgroupId=oracle


-DartifactId=oracle-ojdbc -Dpackaging=jar -Dversion=21-xe
-DgeneratePom=true
-Dfile=C:\Oracle21XE\dbhomeXE\jdbc\lib\ojdbc11.jar

If we execute the above command on command prompt then we


are able to get the pom file like below.

oracle-ojdbc-21-xe.pom
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>oracle</groupId>
<artifactId>oracle-ojdbc</artifactId>
<version>21-xe</version>
<description>POM was created from
install:install-file</description>
</project>

EX:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.durgasoft</groupId>
<artifactId>app20</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>

<project.build.sourceEncoding>UTF-8</project.build.source
Encoding>
</properties>
<dependencies>
<dependency>
<groupId>oracle</groupId>
<artifactId>oracle-ojdbc</artifactId>
<version>21-xe</version>
</dependency>
</dependencies>
</project>

Main.java
package com.durgasoft;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {


public static void main(String[] args) {
try(
Connection connection =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:
1521:xe", "system", "durga");
Statement statement =
connection.createStatement();
ResultSet resultSet =
statement.executeQuery("select * from emp1");
) {

System.out.println("ENO\tENAME\tESAL\tEADDR");

System.out.println("-----------------------------");
while(resultSet.next()){

System.out.print(resultSet.getInt("ENO")+"\t");

System.out.print(resultSet.getString("ENAME")+"\t\t");

System.out.print(resultSet.getFloat("ESAL")+"\t");

System.out.print(resultSet.getString("EADDR")+"\n");
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}

MAVEN for Web Applications:


—----------------------------
In general, to prepare and execute web applications we need
servers.

For web applications development, MAVEN has an internal


server in the form of Tomcat6 or Tomcat7, we can use MAVEN
provided internal Server for our web applications execution.

If we want to prepare web applications with MAVEN we have to


use the following steps.

1. Create a web project by using MAVEN web1.1 archetype.


2. Change Java version from 1.5 to 17 as per the
requirement

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>

3. Provide the Maven tomcat7 plugin in the pom.xml file:

<build>

<finalName>app03</finalName>

<plugins>
<plugin>

<groupId>org.apache.tomcat.maven</groupId>

<artifactId>tomcat7-maven-plugin</artifactId>

<version>2.0</version>

</plugin>

</plugins>

</build>

4. Provide Servlet API dependency in pom.xml

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>3.0-alpha-1</version>

<scope>provided</scope>

</dependency>

5. Prepare web resources like html files, Servlets,


Jsps,...
HelloServlet.java

package com.durgasoft.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html>");

out.println("<body>");

out.println("<h1>Hello User!, This is from


HelloApp-with-MAVEN</h1>");

out.println("</body></html>");

}
6. Run web application and access the web resources

a. Right click on application


b. Select Run as
c. Select Maven Build…
d. Provide the following goal.
tomcat:run -Dmaven.tomcat.port=1234
e. Click on apply and ok button
f. Open the browser use the following url.
http://localhost:1234/app03/hello

If we want to run the above servlet application by using our


explicit server like Tomcat 10.0 version then it is not
required to manage tomcat plugin pom.xml file and we must
provide jakarta servlet api dependency in pom.xml file.

EX:
loginform.html

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>
<h2 style="color: red;" align="center">Durga Software
Solutions</h2>

<h3 style="color: blue;" align="center">User Login


Form</h3>

<form method="POST" action="./login">

<table align="center">

<tr>

<td>User Name</td>

<td><input type="text" name="uname"></td>

</tr>

<tr>

<td>Password</td>

<td><input type="password"
name="upwd"></td>

</tr>

<tr>

<td><input type="submit"
value="Login"></td>

</tr>

</table>

</form>
</body>

</html>

Success.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"


"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;


charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<br><br><br>

<h1 style="color: red;" align="center">

Login Success

</h1>

<h3 align="center">

<a href="./loginform.html">|Login Form|</a>

</h3>

</body>
</html>

Failure.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"


"http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;


charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<br><br><br>

<h1 style="color: red;" align="center">

Login Failure

</h1>

<h3 align="center">

<a href="./loginform.html">|Login Form|</a>

</h3>

</body>

</html>

UserAction.java
package com.durgasoft.app04.action;

import java.sql.ResultSet;

import com.durgasoft.app04.factories.ConnectionFactory;

public class UserAction {

public String checkLogin(String uname, String upwd) {

String status = "";

try {

ResultSet resultSet =
ConnectionFactory.getConnection().

createStatement().

executeQuery("select * from reg_Users where UNAME =


'"+uname+"' and UPWD = '"+upwd+"'");

if(resultSet.next()) {

status = "success";

}else {

status = "failure";

} catch (Exception e) {
e.printStackTrace();

return status;

LoginServlet.java

package com.durgasoft.app04.servlet;

import jakarta.servlet.RequestDispatcher;

import jakarta.servlet.ServletException;

import jakarta.servlet.annotation.WebServlet;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

import com.durgasoft.app04.action.UserAction;

@WebServlet(urlPatterns = {"/login"}, loadOnStartup = 1)

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

UserAction userAction = null;


@Override

public void init() throws ServletException {

userAction = new UserAction();

protected void doPost(HttpServletRequest request,


HttpServletResponse response) throws ServletException,
IOException {

String uname = request.getParameter("uname");

String upwd = request.getParameter("upwd");

String status = userAction.checkLogin(uname, upwd);

RequestDispatcher requestDispatcher = null;

if(status.equalsIgnoreCase("success")) {

requestDispatcher =
request.getRequestDispatcher("/success.html");

requestDispatcher.forward(request, response);

}else {

requestDispatcher =
request.getRequestDispatcher("/failure.html");
requestDispatcher.forward(request, response);

ConnectionFactory.java

package com.durgasoft.app04.factories;

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.util.Properties;

public class ConnectionFactory {

private static Connection connection = null;

static {

try {

Class.forName("com.mysql.cj.jdbc.Driver");

Properties properties = new Properties();

FileInputStream fileInputStream = new


FileInputStream("D:\\Spring830\\SPRING-CORE-MAVEN-APPS\\app0
4\\src\\main\\resources\\db.properties");
properties.load(fileInputStream);

connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/dur
gadb", properties);

} catch (Exception e) {

e.printStackTrace();

public static Connection getConnection() {

return connection;

db.properties

user = root

password = root

Web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">

<display-name>Archetype Created Web


Application</display-name>

<welcome-file-list>
<welcome-file>loginform.html</welcome-file>

</welcome-file-list>

</web-app>

Pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.durgasoft</groupId>

<artifactId>app04</artifactId>

<packaging>war</packaging>

<version>0.0.1-SNAPSHOT</version>

<name>app04 Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<maven.compiler.source>17</maven.compiler.source>

<maven.compiler.target>17</maven.compiler.target>

</properties>

<dependencies>
<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<!--
https://mvnrepository.com/artifact/jakarta.servlet/jakarta.s
ervlet-api -->

<dependency>

<groupId>jakarta.servlet</groupId>

<artifactId>jakarta.servlet-api</artifactId>

<version>5.0.0-M2</version>

<scope>provided</scope>

</dependency>

<!--
https://mvnrepository.com/artifact/mysql/mysql-connector-jav
a -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>

</dependency>

</dependencies>

<build>

<finalName>app04</finalName>

</build>

</project>

MAVEN for Spring Project:


—-------------------------
1. Create a quickstart project.
2. Provide Spring dependencies and Java properties in
pom.xml
3. Prepare Spring application
4. Execute Application

EX:
Hello.java

package com.durgasoft.app05.beans;

public class Hello {

private String name;

public void setName(String name) {

this.name = name;

}
public String getName() {

return name;

public String sayHello() {

return "Hello "+name;

App.java

package com.durgasoft.app05;

import org.springframework.context.ApplicationContext;

import
org.springframework.context.support.ClassPathXmlApplicationC
ontext;

import com.durgasoft.app05.beans.Hello;

public class App {

public static void main(String[] args) {

ApplicationContext applicationContext = new


ClassPathXmlApplicationContext("SpringConfig.xml");

Hello hello = (Hello)


applicationContext.getBean("hello");

System.out.println(hello.sayHello());
}

SpringConfig.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/be
ans

https://www.springframework.org/schema/beans/spring-beans.xs
d">

<bean id="hello" class="com.durgasoft.app05.beans.Hello">

<property name="name" value="Durga"/>

</bean>

</beans>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.durgasoft</groupId>
<artifactId>app05</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>app05</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEnc
oding>

<maven.compiler.source>17</maven.compiler.source>

<maven.compiler.target>17</maven.compiler.target>

</properties>

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>

<version>5.3.25</version>

</dependency>

<!--
https://mvnrepository.com/artifact/commons-logging/commons-l
ogging -->

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.2</version>

</dependency>

</dependencies>

</project>

You might also like