AJ Common Answers
AJ Common Answers
Directives in JSP are instructions given to the JSP container that influence the processing of the
entire JSP page. They do not produce any output but control how the JSP is compiled and executed.
Types of Directives:
1. Page Directive:
o Used to define attributes related to the JSP page, such as error handling, session
usage, and content type.
o Example:
• Example:
• Use this when the included file's content does not change frequently.
3. Taglib Directive:
• Example:
Explanation:
Directives are critical for configuring the page's behavior, such as managing imports, including
reusable components, and using custom tags. They ensure the JSP is processed correctly by the
container.
Q2. Explain Types of Advices with suitable examples.
In Spring AOP (Aspect-Oriented Programming), advice refers to the action taken at a specific join
point. It is the logic that you apply to cross-cutting concerns like logging, security, or transaction
management.
Types of Advice:
1. Before Advice:
o Code:
@Before("execution(* com.example.service.*.*(..))")
2. After Advice:
• Executes after the method execution, regardless of its outcome.
• Example: Logging after a method completes.
• Code:
@After("execution(* com.example.service.*.*(..))")
public void logAfter(JoinPoint joinPoint) {
System.out.println("Executed After Method: " + joinPoint.getSignature());
}
3. After Returning Advice:
• Code:
• Code:
@AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing =
"exception")
5. Around Advice:
• Code:
@Around("execution(* com.example.service.*.*(..))")
return result;
Conclusion:
Advices in Spring AOP help modularize cross-cutting concerns, improving code reusability and
maintainability.
Q3. Explain Data Access operations with JdbcTemplate Class and RowMapper Interface.
JdbcTemplate Class:
The JdbcTemplate class in Spring simplifies database interactions by providing methods for executing
SQL queries, updates, and stored procedures without boilerplate code.
RowMapper Interface:
The RowMapper interface maps rows of a ResultSet to Java objects. It is used to extract data from
the database and map it to objects.
Features of JdbcTemplate:
this.jdbcTemplate = jdbcTemplate;
student.setId(rs.getInt("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
return student;
});
}
Advantages of JdbcTemplate:
Spring Boot is a framework built on top of the Spring Framework that simplifies application
development. It provides pre-configured settings for common features, reducing the need for
boilerplate code.
3. Embedded Servers: Built-in servers like Tomcat and Jetty for easy deployment.
A RESTful web service allows communication between systems using HTTP methods like GET, POST,
PUT, and DELETE.
4. Use annotations like @GetMapping, @PostMapping, etc., for handling HTTP requests.
Example:
@SpringBootApplication
SpringApplication.run(RestServiceApplication.class, args);
@RestController
@RequestMapping("/api")
@GetMapping("/greet")
Explanation:
Spring Boot simplifies RESTful service development by reducing configuration overhead, enabling
developers to focus on business logic.
Q5. Explain all standard actions in JSP and write JSP code using these standard actions for a Login
Page.
Standard actions in JSP are predefined tags used to control runtime behavior.
o Example:
• Example:
• Example:
• Example:
• Example:
</form>
Actions in JSP are XML-based tags that control the behavior of the JSP engine during request
processing.
1. <jsp:useBean>:
o Example:
2. <jsp:setProperty>:
o Example:
3. <jsp:getProperty>:
o Example:
4. <jsp:include>:
o Example:
Explanation:
Actions in JSP are powerful tools to manage JavaBeans and dynamically include or forward resources.
Q7. Explain Dependency Injection with Spring via Setter Injection with suitable example.
Dependency Injection is a design pattern where the dependencies (objects) required by a class are
provided by an external source rather than the class creating them.
Setter Injection:
In Setter Injection, dependencies are injected into a class via setter methods.
Example:
1. Student Class:
this.name = name;
</bean>
3. Main Application:
student.displayInfo();
2. To avoid ClassCastException.
list.add("Hello");
list.add("Hello");
Benefits of Generics:
1. Type-safe collections.
JSP follows a Model-View-Controller (MVC) architecture and operates in the following phases:
1. Translation Phase:
2. Compilation Phase:
3. Execution Phase:
o The compiled servlet is executed, and dynamic content is generated based on the
request.
1. JSP Page:
o The entry point for the request containing static content (HTML/CSS) and dynamic
content (Java code).
2. JSP Container:
o Converts the JSP into a servlet, manages lifecycle, and processes requests.
3. Servlet Engine:
4. Web Server:
Client (Browser)
HTTP Request
HTTP Response
Client (Browser)
Q10. Explain Spring AOP in detail.
1. Aspect:
2. Join Point:
o A specific point in the application where an action can be applied, such as method
execution or exception handling.
3. Advice:
4. Pointcut:
5. Weaving:
Benefits of AOP:
1. Spring Core:
o Provides the foundation for dependency injection and bean lifecycle management.
2. Spring AOP:
4. Spring Web:
5. Spring Boot:
-------------------------------------
| Spring Framework |
-------------------------------------
-------------------------------------
| Spring Boot |
-------------------------------------
Q12. What is Lambda Expression? Write code on it using Lambda Expression as an Object.
Lambda expressions are introduced in Java 8 to provide a concise way to express functional
interfaces. They simplify code by replacing anonymous class implementations.
@FunctionalInterface
interface Greeting {
greeting.sayHello("Alice");
}
Q13. Explain Wildcards and its types.
Wildcards in Generics:
Wildcards in Java Generics are used to represent unknown types. They provide flexibility in handling
multiple data types.
Types of Wildcards:
o Example:
o Example:
System.out.println(num);
o Example:
list.add(10);
}
Q14. What is Bean Autowiring and its types of mode?
Autowiring in Spring automatically injects dependencies into a bean by type, name, or constructor
without explicitly specifying them in the configuration.
1. No Autowiring (no):
2. By Type (autowire="byType"):
3. By Name (autowire="byName"):
4. Constructor (autowire="constructor"):
5. Autodetect:
Session management is the process of maintaining user state across multiple HTTP requests.
1. Cookies:
2. URL Rewriting:
4. HTTP Session:
<%
int count = 1;
if (cookie.getName().equals("counter")) {
count = Integer.parseInt(cookie.getValue()) + 1;
response.addCookie(newCookie);
%>
What is Collections?
The Collections framework in Java provides a standardized way to store and manipulate groups of
objects.
Examples:
1. Set:
set.add("A");
set.add("B");
2. Map:
map.put(1, "One");
3. HashMap:
hashMap.put(1, "Value");
4. List:
list.add("Element");
5. Vector:
vector.add(10);
Q17. Explain JSP Implicit Objects.
JSP implicit objects are pre-defined objects provided by the JSP container. These objects are
automatically available in JSP pages without requiring explicit declaration.
<%
%>
Q18. What is POJO Programming Model? Explain with a suitable example.
What is POJO?
POJO stands for Plain Old Java Object. It is a simple Java class with no special restrictions other than
following Java conventions.
Features of POJO:
Example of a POJO:
// Default Constructor
public Student() {}
// Parameterized Constructor
this.id = id;
this.name = name;
return id;
this.id = id;
}
public String getName() {
return name;
this.name = name;
Advantages of POJO: