Top Xx Jump Mvc Interview Questions Together With Answers For Coffee Programmers

Advertisement

Masukkan script iklan 970x90px

Top Xx Jump Mvc Interview Questions Together With Answers For Coffee Programmers

Jumat, 27 Maret 2020

The Spring MVC framework is i of the most pop Java frameworks for developing web applications. If yous accept been working inwards Java together with the developing web-based application so at that topographic point is a adept endangerment that yous accept already used Spring MVC inwards your project. In the concluding decade, it has buy the farm the defacto framework for developing Java web application. Spring MVC is based on classic MVC (Model-View-Controller) blueprint pattern but it is much to a greater extent than than that. It leverages, Spring framework's forcefulness inwards price of dependency injection together with Inversion of control together with promotes loosely coupled architecture, similar to the Spring framework itself. Because of its immense popularity together with usefulness, most of the Java evolution job requires a good knowledge of Spring together with Spring MVC.

There is a lot of demand for adept Java developer with good knowledge and experience inwards Spring together with Spring MVC. One way to prepare yourself for such chore interviews is to hold off for adept interview questions.

These questions non alone assist yous to prepare good for interviews but besides assist yous to empathise the primal concepts improve together with encourage yous to acquire to a greater extent than past times exploring together with that's why I am ever in search of adept Spring MVC Interview questions.

Recently I was preparing for Spring Core Professional Certification when I come upwardly across some Spring certification guides from Pivotal. These guides contain some interesting questions on Spring MVC.

Even though these questions are provided only to give yous an thought almost the syllabus of Spring certification, I truly flora many of such questions accept already been asked to myself together with friends inwards diverse Spring chore interviews.

On asking to a twain of my friends, I thought to part answers to these questions here. So, if yous are preparing for either Spring Certification or Java Web Developer interview, yous volition uncovering this listing of Spring MVC Interview Questions real useful for your preparation.




20 Spring MVC Interview Questions for Java Programmers

Without farther Ado, hither is my listing of some of the often asked Spring MVC questions from Java Interviews, especially from the spider web evolution positions.


1. MVC is an abbreviation for a blueprint pattern. What does it represent together with what is the idea behind it? (answer)
Answer - MVC is an abbreviation for Model-View-Controller blueprint pattern. This pattern is based upon the separation-of-concerns design principle which promotes handling different functionality at different layer together with loose coupling betwixt layers.

In MVC pattern, Model contains the information which is rendered past times View together with Controler assist inwards asking processing together with routing.

Neither Model knows about View, nor View is subject upon Model, which agency the same model tin endure rendered past times dissimilar views e.g. JSP, FreeMarker or it tin endure fifty-fifty endure written equally JSON or XML inwards instance of RESTful Web Services. You tin acquire to a greater extent than almost MVC inwards my favorite course Spring Framework 5: Beginner to Guru. If yous are serious almost Spring, this is the course of written report yous should look.


 The Spring MVC framework is i of the most pop Java Top twenty Spring MVC Interview Questions together with Answers for Java Programmers



2. Do yous demand spring-mvc.jar inwards your classpath or is it component subdivision of spring-core? (answer)
The spring-mvc.jar is non component subdivision of spring-core, which agency if yous desire to usage Spring MVC framework inwards your Java project, yous must include spring-mvc.jar inwards your application's classpath. In Java spider web application, spring-mvc.jar is unremarkably placed within /WEB-INF/lib folder.


3. What is the DispatcherServlet together with what is it used for? (answer)
The DispatcherServlet is an implementation of Front Controller blueprint pattern which handles all incoming spider web asking to a Spring MVC application. H5N1 Front Controller pattern (see Enterprise application blueprint pattern) is a mutual pattern inwards spider web applications whose chore is to have all asking together with road it to dissimilar components of the application for actual processing.

In instance of Spring MVC, DispatcherServlet road spider web requests to Spring MVC controllers.

In Spring MVC, DispatcherServlet is used for finding the right Controler to procedure a request, which it does amongst the assist of handler mapping e.g. @RequestMapping annotation.

It is besides responsible for delegating logical persuasion cry to ViewResolver together with so sending the rendered response to the client.


4. Is the DispatcherServlet instantiated via an application context? (answer)
No, DispatcherServlet is instantiated past times Servlet containers like Tomcat or Jetty. You must define DispatcherServlet into the web.xml file equally shown below.

You tin run across that load-on-startup tag is 1 which agency DispatcherServlet is instantiated when yous deploy Spring MVC application to Tomcat or whatever other Servlet container. During instantiation, it looks for a file servlet-name-context.xml together with so initializes beans defined inwards this file.


5. What is the root application context inwards Spring MVC? How is it loaded? (answer)
In Spring MVC, the context loaded using ContextLoaderListener is called the "root" application context which belongs to the whole application spell the i initialized using DispatcherServlet is truly specific to that servlet.

Technically, Spring MVC allows multiple DispatcherServlet in a Spring MVC spider web application together with so multiple such contexts each specific for respective servlet but having the same root context may exist. You tin farther check answer)
The @Controller is a Spring MVC tone to define Controller but in reality, it's only a stereotype annotation. You tin fifty-fifty do a controller without @Controller past times annotating the Spring MVC Controller classes using @Component annotation. The existent chore of asking mapping to the handler method is done using @RequestMapping annotation.


7. What is the ContextLoaderListener together with what does it do? (answer)
The ContextLoaderListener is a listener which helps to bootstrap Spring MVC. As the name suggests it loads together with do ApplicationContext, so yous don't accept to write explicit code to do do it.

The application context is where Spring edible bean leaves. For a Web application, at that topographic point is is a subclass called WebAppliationContext.

The ContextLoaderListener besides necktie the lifecycle of the ApplicationContext to the lifecycle of the ServletContext. You tin acquire the ServletContext from WebApplicationContext using getServletContext() method.


8. What are yous going to do inwards the web.xml? Where do yous house it?
The ContextLoaderListener is configured inwards web.xml equally listener together with yous pose that within a tag equally shwon below:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

When the Spring MVC web application is deployed, Servlet container created an instance of ContextLoaderListener shape which loads the Spring's WebApplicationContext. You tin besides see Spring MVC For Beginners to acquire to a greater extent than almost ContextLoaderListener together with WebApplicationContext together with their role inwards Spring MVC.

 The Spring MVC framework is i of the most pop Java Top twenty Spring MVC Interview Questions together with Answers for Java Programmers



9. How is an incoming asking mapped to a controller together with mapped to a method? (answer)
Sometimes this inquiry is besides asked How does DispatcherServlet knows which Controller should procedure the request? Well, the answer lies in something called handler mappings.

Spring uses handler mappings to associate controllers amongst requests, 2 of the commonly used handler mappings are BeanNameUrlHandlerMapping together with SimpleUrlHandlerMapping.

In BeanNameUrlHandlerMapping, when the asking url matches the cry of the bean, the shape inwards the edible bean Definition is the controller that volition handgrip the request.

On the other hand, In SimpleUrlHandlerMapping, the mapping is to a greater extent than explicit. You tin specify the number of URLs and each URL tin endure explicitly associated amongst a controller.

Btw, if yous are using annotations to configure Spring MVC, which yous should so @RequestMapping annotations is used to map an incoming request to a controller and a handler method.

You tin besides configure @RequestMapping tone past times URI Path, past times query parameters, past times HTTP methods of a request and past times HTTP headers introduce inwards the request.


10. What is the @RequestParam used for? (answer)
The @RequestParam is a Spring MVC tone which is used to extract asking parameter or query parameters from URL inwards Controller's handler method equally shown below:

public String personDetail(@RequestParam("id") long id){
  ....
  homecoming "personDetails";
}

The @RequestParam tone besides supports information type conversion e.g. yous tin run across hither a String is converted to long automatically but it tin besides outcome inwards an exception if the query parameter is non introduce or inwards instance of a type mismatch. You tin besides brand the parameter optional past times using requried=false e.g. @RequestParam(value="id", required=false )


11. What are the differences betwixt @RequestParam together with @PathVariable? (answer)
Even though both @RequestParam together with @PathVariable annotations are used to extract some information from the URL, at that topographic point is a key divergence betwixt them.

The @RequestParam is used to extract query parameters e.g. anything after "?" inwards URL spell @PathVariable is used to extract the part of the URI itself. For example, if the given URL is http://localhost:8080/SpringMVC/books/3232233/?format=json

Then yous tin access the query parameter "format" using @RequestParam tone together with /books/{id} using @PathVariable, which volition give yous 3232233.

Here is some other illustration of @PathVariable,

@RequestMapping("/persons/{id}" )
public String personDetail (@PathVariable ("id" ) long id) {...}

This code tin extract individual id=123 from /persons/123. It is especially used inwards RESTful Web Services because their id is unremarkably component subdivision of URI or URL path.

 The Spring MVC framework is i of the most pop Java Top twenty Spring MVC Interview Questions together with Answers for Java Programmers



12. What are some of the valid homecoming types of a controller method? (answer)
There are many homecoming types are available for a controller method inwards Spring MVC which is annotated past times @RequestMapping within the controller. Some of the pop ones are:
  1. String
  2. void
  3. View
  4. ModelAndView (Class)
  5. Model (Interface)
  6. Map
  7. HttpEntity<?> or ResponseEntity<?>
  8. HttpHeaders


You tin run across the amount listing of valid homecoming types for a Spring MVC controller here. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-return-types

Every homecoming type has its specific use. For example, if yous are using String so it agency Controller only homecoming View Name together with this persuasion cry will resolve by ViewResolver.

If yous don't desire to homecoming whatever persuasion cry cite homecoming type void. If yous desire to laid persuasion cry equally good equally desire to mail some object usage ModelAndView equally a homecoming type.


13. What is a View together with what's the thought behind supporting dissimilar types of View? (answer)
H5N1 View is an interface inwards Spring MVC application whose implementations are responsible for rendering context and exposing the model. H5N1 unmarried persuasion exposes multiple model attributes. Views inwards Spring MVC tin endure beans also.

They are probable to endure instantiated equally beans past times a ViewResolver. As this interface is stateless, persuasion implementations should endure thread-safe. past times using ViewResolver, a logical cry of persuasion tin endure resolved into dissimilar types of View implementation e.g. JstlView for displaying JSP or other persuasion implementations for FreeMarker together with Velocity.

If yous are novel to Spring MVC together with non familiar amongst these basic classes, so I advise yous acquire Spring past times joining i of the free Spring courses I accept shared earlier.

 The Spring MVC framework is i of the most pop Java Top twenty Spring MVC Interview Questions together with Answers for Java Programmers



14. How is the right View chosen when it comes to the rendering phase? (answer)
The right View is chosen past times ViewResolver inwards Spring MVC. When Controller returns a logical persuasion cry to DispatcherServlet, it consults to ViewResolver to uncovering the right View.

The ViewResolver depending upon its implementation resolves the logical persuasion into a physical resources e.g. a JSP page or a FreeMarker template.

For example, InternalResourceViewResolver is a default persuasion resolvers which converts logical persuasion cry e.g. "hello" to "/WEB-INF/hello.jsp" using prefix together with suffix.


15. What is the Model? (answer)
Model is in i lawsuit again a reference to encapsulate information or output for rendering. Model is ever created together with passed to the persuasion inwards Spring MVC. If a mapped controller method has Model equally a method parameter, so a model instance is automatically injected past times Spring framework to that method.

Any attributes assault the injected model are preserved together with passed to the View. Here is an illustration of using Model inwards Spring MVC:

public String personDetail(Model model) {
...
model.addAttribute("name", "Joe");
...
}

16. Why do yous accept access to the model inwards your View? Where does it come upwardly from? (answer)
You demand to accept access tot he model inwards your View to homecoming the output. It's the model which contains the information to endure rendered. The Model comes with the Controller, which process their client asking together with encapsulate the output into a Model object.


17. What is the purpose of the session scope? (answer)
The purpose of the session range is to do an instance of the edible bean for an HTTP Session. This agency the same bean tin serve multiple requests if it is scoped inwards session. You tin define the scope of a Spring edible bean using range attribute or @Scope annotation in Spring MVC application.


18. What is the default range inwards the spider web context? (answer)
The singleton range is the default range for a Spring edible bean fifty-fifty inwards the spider web context. The other 3 Web context-aware scopes are a request, session, together with global-session, which are alone available inwards a spider web application aware ApplicationContext object. See Spring Master Class - Beginner to Expert to acquire to a greater extent than almost ApplicationContext inwards Spring.

 The Spring MVC framework is i of the most pop Java Top twenty Spring MVC Interview Questions together with Answers for Java Programmers


19. Why are controllers testable artifacts? (answer)
In Spring MVC Controllers are testable artifacts because they are non straight coupled amongst whatever View technology. They only homecoming a logical View name, which tin endure easily tested.


20. What does the InternalResourceViewResolver do? (answer)
In Spring MVC, H5N1 ViewResolveer returns View to handgrip to output rendering based on the Logical View Name (provided past times the controller) together with locale. This way controller is non coupled to specific persuasion applied scientific discipline e.g. JSP or FreeMarker it alone returns the logical persuasion name.

InternalResourceViewResolver is the default View resolver configured inwards Spring MVC together with DispatcherServlet uses it to uncovering the right view. InternalResourceViewResolver is used to homecoming JSPs (JstlView).

It Configures prefix together with suffix to logical persuasion cry which so results inwards a path to specific JSP equally shown below:
<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "prefix" value= "/WEB-INF/" />
<property cry ="suffix" value =".jsp" />
</bean>

So if Controller returns "hello" equally logical persuasion name, the InternalViewResolver volition homecoming /WEB-INF/hello.jsp together with DispatcherServlet will frontward the request to this JSP page for rendering.

That's all almost some of the frequently asked Spring MVC Interview Questions. If yous know answers to these questions agency yous accept adept noesis of Spring MVC framework, its different components e.g. DispatcherServlet, handler mappings, Controllers, Views together with Model together with tin explicate to anyone.

Sometime, yous may acquire questions from Spring centre together with Spring safety equally well, thus it's besides advisable to prepare for them. You tin uncovering some Spring Security Interview inquiry hither together with Some Core Spring questions here.


Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring Framework Interview Guide - 200+ Questions & Answers


Other Spring related articles yous may similar to explore this blog
  • 15 Spring Boot Interview Questions for Java Developers (questions)
  • 3 ways to acquire Spring Framework improve (article)
  • How Spring MVC industrial plant internally? (answer)
  • What is the usage of DispatcherServlet inwards Spring MVC? (answer)
  • How to enable Spring safety inwards Java application? (answer)
  • Does Spring certification assist inwards Job together with Career? (article)
  • How to prepare for Spring Certification? (guide)
  • 3 Best Practices Java Developers Can acquire from Spring (article)
  • Difference between @Autowired together with @Injection annotations inwards Spring? (answer)
  • 5 Spring together with Hibernate online courses for Java developers (list)
  • 5 Spring Boot courses for Java developers (courses)
  • 5 courses to acquire Microservices amongst Spring Boot together with Spring Cloud (courses)

Thanks for reading this article. If yous similar these questions together with my answers together with explanations so delight part amongst your friends together with colleagues, it does brand a difference. If yous accept whatever questions which are non answered inwards this list, experience costless to driblet a comment together with I'll endeavor my best to uncovering an respond for you.

P.S. - If yous desire to acquire how to railroad train RESTful Web Services using Spring Framework, cheque out Eugen Paraschiv's REST amongst Spring course. He has lately launched the certification version of the course, which is amount of exercises together with examples to farther cement the existent basis concepts yous volition acquire from the course.