Spring Framework Flashcards
Can static use this?
No, because cannot use this keyword as there is no instance for ‘this’ to refer to
How do you get apache on Java?
You pick preferences, go to server runtime environments, apache Tomcat add, 8.5, and then click the apache folder you want to select
Walkthrough the process of making a tomcat container
1) Download Apache Tomcat
2) Create a new folder name servlets and have STS use it as its working directory
3) Add apache tomcat as one of our run time environments
a) spring tool suite -> preferences
b) click on preferences, go to server - runtime environ e ments -> add
c) select apache tomcat v8.5 click next
d) change the tomcat installation directory to the folder downloaded from our very first step and click Finish
e) on the left bottom side of STS, in the Servers tab, we are gonig to create a new server using Apache Tomcat 8.5 if you cannot fnid the Servers tab, you can go to Window -> Show View -> Servers to add it to your STS dashboard view
How do you create a new servlet project
Right click on the Package explorer -> New -> Dynamic Web Project
After you have created a class, what do the doGet and doPost do?
Go to java resources, right click on src -> new -> servlet
1) next have your package be com.codingdojo.web
Breakdown the code that STS creates when creating a servlet
@WebServlet("/Home") : This annotataes this servlet to respond to this specific URL pattern public class Home extends HttpServlet {: our class extends from the HttpServlet built in java class
private static final long serialVersionUID = 1L: identifier for our class
doGet(HttpServletRequest request, HttpServletResponse response) method: This method handles the GET requests coming to this specific url.
HttpServletRequest object that contains the request the client has made on the servlet
HttpServletResponse object that contains the response the servlet sends to the client
doPost(HttpSerletRequest request, HttpServletResponse response) : This method handles the POST request coming to this specific URL
What is Tomcat?
Tomcat is the container that has all the servlets, JSPs, and Java Beans.
Explain the nuance of the multiple Java class and the main method in Tomcat
An application can have multiple Java classes. However, there can be only noe main method within that program. When we create web applications using servlets, we will never have a class that has a main method because they are being controled by another Java Application, the Web Container.
What makes our ‘Home’ class a servlet?
It extends the HttpServlet class, which in itself, implements the basic Servlet interface.
In the HttpServlet class, there is a service() method. THIS METHOD is what calls methods such as doGet() or doPost() from the servlets.
How does a container handle a request?
Get request:
1) User visits the ‘/HelloWorld/Home’ URL
2) The container receives the request and immediately creates two important objects for our servlets
- HttpServletRequest - Request Information
- HttpServletResponse - Response Information
3) The container finds the appropriate servlet, allocates or creates a thread for the request and passes the request and response objects to the servlet
4) The service() method calls the doGet() method from our Home servlet
5) The doGet() method generates a response from <h1> Hello World </h1>
6) The container generates an actual HTTP response from the response object, sends it back to the client and the thread completes
Where do we keep static files?
For Apache Tomcat, we will place all our static assets in our WebContent, and they will be available to serve from our root path. As long as they are not in the WEB-INF directory, they will be publicly available on our server
Best practice for JavaServlet Pages?
Better practice is to separate the presentation layer from the logic layer. We want to avoid writing our HTML code in our servlet methods. Instead, we willl be using JSPs to write our views.
What is the purpose of a JSP file?
JSP file is for developers to add Java code with HTML
Explain the Model 2 MVC architecture
This architecture utilizes our servlets to control the data flow (C), our JSP files to display our information (V), and Java Beans to model (M), or shape, how the information is stored
Note: In modern web developemtn, it is common that the Model is concerned with shaping (or modeling) our dta as it is returned from the database so that our view can present it appropriately.
What are the differences between Spring, Spring MVC, and Spring Boot
The Spring Framework is a large open source framework that consists of several modules. One of these modules is the Spring MVC framework, which allows us to build web applications usijng Java.
Spring Boot is a framework used to configure and set up Spring applications very easily. A spring project needs several dependencies installed and configured to work, but with Spring Boot, everything comes pre-configured so that develoeprs only have to worry about writing code ( and not XML). Spring Boot is a configuration framework that uses the SPring framework.
What is the request and response cycle
Like most web frameworks, Springs’ web moduel functions on a request basis.
What is maven?
Maven is a software project management and comprehension tool. With the help of Maven, Spring Boot will be able to configure and install dependencies, compile our Java code and run our class files.
What do you need before starting a Spring Boot project?
Spring Tool Suite, JDK and its environment variables already set up, Maven and its environment variables already set up
What is a controller?
In MVC, it’s like a traffic cop. Its job is to receive incoming requests and then route it to different parts of your application until a response can be served back to the client. They are classes that are made to accept requests and figure out what to do with it.
What are annotations?
Annotation are a form of metadata that can be added to your source code. There are annotations for calsses, methods, variables, parameters, and pacakges
@RequestMapping
For mapping web requests onto specific handler classses and/or methods.
@Controller
Indicates that an annotated class is a ‘Container’ (a web controller).
@ResponseBody
Indicates a return value should be bound to the web response body. All this means is that we want Spring boot to return data and not a view.
@RestController
Combines Controller and ResponseBody annotation. We use this annotation when we want our controller to respond with data
Why is Spring Boot so popular?
Using Spring Boot we do not need to code any configuration for the DispatcherServlet to determine which @Controller and @RequestMapping to target. This is part of why Spring Boot is so popular - it is preconfigured to scan the correct classes for our @RequestMapping annotations and create a URI Map to properly send, or dispath our requests to
Distinguish between the different word ‘model’ in context
Model in the MVC design patterns for web applications
Model object, which is available in Spring MVC’s controllers
Domain Model, a java bean that contains annotation to represent the application’s data
In Spring for the MVC definition, what does model do?
It is responsible for managing the application’s business logic and data.
However, in Spring MVC, the business logic is handled by the Service Layer and the data is handled by the Persistence Layer. The SL and PL make up for what is known as the M in MVC in today’s web applications
What is the persistence layer?
The layer is in charge of managing the application’s data. PL is made up of Domain models and Respositories. We will use Repositories to access our database via an object relational mapper (ORM).
What is the service layer?
It is made up of classes that implement the business logic of our application. It will call on the respositories to execute some sort of transaction according to the request from the user.