Spring Framework Flashcards

1
Q

Can static use this?

A

No, because cannot use this keyword as there is no instance for ‘this’ to refer to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you get apache on Java?

A

You pick preferences, go to server runtime environments, apache Tomcat add, 8.5, and then click the apache folder you want to select

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Walkthrough the process of making a tomcat container

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a new servlet project

A

Right click on the Package explorer -> New -> Dynamic Web Project

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

After you have created a class, what do the doGet and doPost do?

A

Go to java resources, right click on src -> new -> servlet

1) next have your package be com.codingdojo.web

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Breakdown the code that STS creates when creating a servlet

A
@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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Tomcat?

A

Tomcat is the container that has all the servlets, JSPs, and Java Beans.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the nuance of the multiple Java class and the main method in Tomcat

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What makes our ‘Home’ class a servlet?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does a container handle a request?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where do we keep static files?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Best practice for JavaServlet Pages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of a JSP file?

A

JSP file is for developers to add Java code with HTML

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Explain the Model 2 MVC architecture

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the differences between Spring, Spring MVC, and Spring Boot

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the request and response cycle

A

Like most web frameworks, Springs’ web moduel functions on a request basis.

17
Q

What is maven?

A

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.

18
Q

What do you need before starting a Spring Boot project?

A

Spring Tool Suite, JDK and its environment variables already set up, Maven and its environment variables already set up

19
Q

What is a controller?

A

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.

20
Q

What are annotations?

A

Annotation are a form of metadata that can be added to your source code. There are annotations for calsses, methods, variables, parameters, and pacakges

21
Q

@RequestMapping

A

For mapping web requests onto specific handler classses and/or methods.

22
Q

@Controller

A

Indicates that an annotated class is a ‘Container’ (a web controller).

23
Q

@ResponseBody

A

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.

24
Q

@RestController

A

Combines Controller and ResponseBody annotation. We use this annotation when we want our controller to respond with data

25
Q

Why is Spring Boot so popular?

A

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

26
Q

Distinguish between the different word ‘model’ in context

A

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

27
Q

In Spring for the MVC definition, what does model do?

A

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

28
Q

What is the persistence layer?

A

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).

29
Q

What is the service layer?

A

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.