Nearby lessons

34 of 35

Spring - Integration with Servlets

📌 What You Will Learn
  • Understand Spring - Integration with Servlets
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - Integration with Servlets step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

a) Prepare html file or jsp file

  • select "src".
  • select "main".
  • Right Click on "webapps".
  • Select "New".
  • Select "Html File".
  • Provide File Name "loginform.html".
  • Click on "Next".
  • Click on "Finish".
  • Provide Html Code in loginform.html.
  • Save Html File.

EX: loginform.html

a) Prepare html file or jsp file

Example02
Loading editor…

a) Prepare html file or jsp file

Example03
Loading editor…

a) Prepare html file or jsp file

Example04
Loading editor…

b) Prepare Servlets

a. Project Name : loginapp

b. Folder Name: src/main/servlets

c. Click on "Finish" Button.

Example05
Loading editor…

b) Prepare Servlets

a) Package Name: com.durgasoft.servlets

b) Class Name : LoginServlet

Example06
Loading editor…

b) Prepare Servlets

EX: LoginServlet.java

Example07
Loading editor…

b) Prepare Servlets

Example08
Loading editor…

b) Prepare Servlets

Example09
Loading editor…

b) Prepare Servlets

Example10
Loading editor…

b) Prepare Servlets

ows ServletException, IOException {

Example11
Loading editor…

b) Prepare Servlets

Example12
Loading editor…

b) Prepare Servlets

Example13
Loading editor…

b) Prepare Servlets

web.xml

Example14
Loading editor…

b) Prepare Servlets

Example15
Loading editor…

b) Prepare Servlets

Example16
Loading editor…

c)Prepare Service Class As per the requirement

i) Package Name : com.durgasoft.service

ii) Class Name : UserService

Example17
Loading editor…

c)Prepare Service Class As per the requirement

EX:UserService.java

Example18
Loading editor…

c)Prepare Service Class As per the requirement

Example19
Loading editor…

c)Prepare Service Class As per the requirement

Example20
Loading editor…

c)Prepare Service Class As per the requirement

", "durga");

Example21
Loading editor…

c)Prepare Service Class As per the requirement

wd = '"+upwd+"'");

Example22
Loading editor…

c)Prepare Service Class As per the requirement

7.Past the URL in Browser Addressbar.

http://localhost:9944/loginapp/loginform.html

  • Provide data and click on "Login" button and get response from LoginServlet.

Spring Core Module Application in MAVEN

Steps to Prepare Spring Core Module Application in MAVEN:

  • Create Maven Quick Start Project.
  • Provide Configurations in pom.xml file.
  • Prepare Spring Resources like Beans, Configuration Files, Test and Cases...
  • Run Spring Application
  • Create Maven Quick Start Project.
  • Open Eclipse IDE
  • Right Click on "Project Explorer"
  • Select "New"
  • Select "Others"
  • Search for "Maven".
  • Select for "Maven Project".
  • Click on "Next" button.
  • Click on "Next" button.
  • Provide the following details.

a. groupId: com.durgasoft

b. artifactId: springapp1

c. version: 0.0.1-SNAPSHOT

d. package: com.durgasoft

  • Click on "Finish" Button.
  • Provide Configurations in pom.xml file.
  • Open pom.xml file File and provide java8 plugin configuration:

EX:

Example23
Loading editor…

c)Prepare Service Class As per the requirement

EX:

Example24
Loading editor…

c)Prepare Service Class As per the requirement

Example25
Loading editor…

c)Prepare Service Class As per the requirement

Example26
Loading editor…

c)Prepare Service Class As per the requirement

Note: In the above dependencies, just add spring-core and spring-context dependencies in

pom.xml file then automatically all other dependencies are also be loaded.

EX: pom.xml

Example27
Loading editor…

c)Prepare Service Class As per the requirement

XMLSchema-instance"

  • xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m

aven-4.0.0.xsd">

Example28
Loading editor…

c)Prepare Service Class As per the requirement

Example29
Loading editor…

c)Prepare Service Class As per the requirement

Example30
Loading editor…

c)Prepare Service Class As per the requirement

Example31
Loading editor…

c)Prepare Service Class As per the requirement

Example32
Loading editor…

c)Prepare Service Class As per the requirement

Example33
Loading editor…

c)Prepare Service Class As per the requirement

Example34
Loading editor…

c)Prepare Service Class As per the requirement

Example35
Loading editor…

c)Prepare Service Class As per the requirement

Example36
Loading editor…

a) Prepare Bean class

  • Right Click on "src/java/main".
  • Select "New"
  • Select "Class".
  • Provide the following details

Package: com.durgasoft.beans

Class : HelloBean

5 .Click on "Finish" button.

  • Provide properties and methods in Bean class.

EX: HelloBean.java

a) Prepare Bean class

Example38
Loading editor…

a) Prepare Bean class

Example39
Loading editor…

b) Prepare Spring Configuration File

  • Right Click on "src\java\main".
  • Select "Package".
  • Provide package name "com.durgasoft.resources".
  • Click on "Finish" button.
  • Right Click on "com.durgasoft.resources" package.
  • Select "New".
  • Select "Other".
  • Search "XML" and select "XML File".
  • Click on "Next" button.
  • Provide file Name: applicationContext.xml
  • Click on "Next" button.
  • Click on "Finish" Button.

EX: applicationContext.xml

b) Prepare Spring Configuration File

Example41
Loading editor…

c) Prepare Test Application

  • Right Click on "src/java/main".
  • Select "New"
  • Select "Class".
  • Provide the following details

Package: com.durgasoft.test

Class : TestClient

  • Click on "Finish" button.
  • Provide application Logic in TestClient class.

EX:TestClient.java

c) Prepare Test Application

Example43
Loading editor…

c) Prepare Test Application

Example44
Loading editor…

c) Prepare Test Application

Example45
Loading editor…

c) Prepare Test Application

sources/applicationContext.xml");

Example46
Loading editor…

c) Prepare Test Application

d)Access TestClient Application from JUnit test[src\test\main\TestApp.java]:

EX:TestApp.java

Example47
Loading editor…

c) Prepare Test Application

Example48
Loading editor…

c) Prepare Test Application

Example49
Loading editor…

c) Prepare Test Application

Example50
Loading editor…

c) Prepare Test Application

LOG4J

COURSE MATERIAL

BY

SPRING LOG4J MODULE INDEX

  • INTRODUCTION...................................................................Page 745
  • Log4j......................................................................................Page 748
  • Appenders.............................................................................Page 755
  • Layouts..................................................................................Page 761

INTRODUCTION

In Real time applications development, we may get no of problems or bugs or exceptions while

executing or testing the applications, Ti identify the problems and their locations then we have to

trace the applications flow of execution.

To trace applications flow of execution we will use "System.out.println(--)" at basic level.

EX:

Example51
Loading editor…

c) Prepare Test Application

If we execute the above application then we are able to get the following output on console or

command prompt.

Before deposit() method call

logic for deposit

After deposit() method call

Before withdraw() method call

logic for withdraw

After withdraw() method call

Before transfer() method call

logic for transfer

After transfer() method call

If we use System.out.println() method in applications to trace flow of execution then we are able to

get the following problems.

Example52
Loading editor…

c) Prepare Test Application

output systems like file systems[html files, xml files, text files...], databases, network,....

Example53
Loading editor…

c) Prepare Test Application

expensive and time consuming.

  • In Applications, it is not suggestible to write too many no of System.out.println() methods in

applications, because, it will reduce application performance.

Example54
Loading editor…

c) Prepare Test Application

production environment , because, if we use System.out.println() method in server side

applications then it will display messages in servers console only, it will not be avaiable to

users.

Example55
Loading editor…

c) Prepare Test Application

show differences in Error messages, warning messaages, normal information,....

Example56
Loading editor…

c) Prepare Test Application

enterprise applications.

To overcome all the problems while tracing applications we have to use Logging.

Logging : It is the process of writing log messages during the execution of a program to a central

place. This logging allows you to report and persist error and warning messages as well as info

messages so that the messages can later be retrieved and analyzed.

In general, in java applications, Logging is required

  • To understand flow of execution in applications
  • To manage exception messages when Exceptions are occurred in java applications
  • To manage the event - notification messages in file systems....

Logging Framework: It is a set of classes and interfaces or a product to perform Logging in Java

applications.

EX:

Example57
Loading editor…

c) Prepare Test Application

Advantages of Logging Frameworks:

  • Problem diagnosis: In ingeneral, in application development, we are able to detect the

problems which are occurred in compilation time or in execution, but, some hidden problems are

existed inside the applications, which may not come in out side direcly, but, they will give effect to

our application execution, in this situation, if we use good Logging Framework then it is possible to

detect the hidden problems quickly and we can fix them easily.

  • Quick Debugging: Once if we diagnose the problem and if we identify the location of the

problem by using logging framework then it is very simple to fix that problem and it simplifies

Debugging and it able to reduce overall Debugging time.

  • Easy Maintenance: If we provide good Logging Framework to perform logging in our

applications then it will provide somde description or information about our application which is

usefull to manage our applications easily.

  • History: In general, all Logging Frameworks are tracing the applications flow of execution and

they are able to store tracing details in a file system, this logging details are usefull to analyze the

problems and to solve the problems.

  • Cost and Time Saving: Logging Frameworks will simplifies debugging, easy maintenance ,

persisting application information,..... , these qualities of Logging Framework saves time and cost

of the application.

Drawbacks with Logging Frameworks:

  • Logging Frameworks needs to write some extra code in our java applications, it will

increase overhead to the application.

  • Logging Frameworks will provide some extra code in our applications, it will increase

application execution time at runtime, it will reduce application performance.

  • Logging Framework will provide some extra code in application, so that, it will increase

application length.

  • Poor logging strategies will increase confusion to the developers.

Log4j

Log4J is a reliable, fast and flexible framework written in JAVA to perform logging in Java

applications and it is provided by Apache Software Foundations.

Example58
Loading editor…
📝 Key Takeaways
  • Key ideas of Spring - Integration with Servlets explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.