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.