JSP-About the MVC model of Servlet, redirect forward, and scope

MVC model

Abbreviation for Model, View, Controller model, which is in charge of "processing", "result display", and "supervision" respectively. The controller receives the user's request, the controller asks the model to process the data, the model returns the processed data to the controller, the data is passed to the view, the view is shaped and returned to the user as a response, and so on. become.

In the Servlet / JSP environment, the controller is often performed by the Servlet class, the view is performed by JSP, and the model is performed by a normal Java class. One of the advantages of this is that it can be shared and developed without knowledge that spans multiple fields.

01.png

Forwards and redirects

There are two ways to transition pages from a Servlet class to a JSP or other Servlet class: forward and redirect. Forwarding is fast instead of being able to move only within the same application, and the address bar of the browser retains the information of the first request destination. On the other hand, redirects are slow instead of being able to access files and other applications (other sites), and the information in the address bar of the browser is also changed.

02.png

Method for processing

forward

redirect

Location of jsp files

The jsp file saved under Webcontent / WEB-INF / cannot be opened by receiving instructions from the browser, so it can be used when you do not want to link directly. Also, due to the nature that it cannot be opened directly from the browser, it can be displayed from the forward but cannot be redirected.

scope

Even if you use forward or redirect, you cannot save or send data as it is. By storing data in an area called a scope, data can be shared between Servlets and JSPs.

Conditions for sharing

Only instances can be saved -It must be possible to serialize by implementing java.io.Serializable ・ The class belongs to the package -Have a default constructor (constructor with no arguments) in public -Fields should be encapsulated and instead have getters / setters that follow naming conventions Is set as a rule for sharing.

Range to share

The scope is divided into four according to the storage range, and they are called "page scope", "request scope", "session scope", and "application scope", respectively.

Page scope

The range is only within that page. I will omit this time.

Request scope

The range is from the request to the response. It can be used for Web applications such as displaying calculation results based on the input data.

Session scope

The range is between sessions. Necessary when using the value temporarily entered in the registration form etc. several times.

Application scope

Used when handling data shared / referenced by the entire user, such as voting buttons. Discarded when the application is closed.

How to save / retrieve

It can be saved and retrieved by a method with a common name and argument of a class that differs for each scope.

** Save method **

** Get method **

For request scope

Class to use: *** ServletRequest *** class

In JSP, there are some variables ** that can be used as variables without explicitly declaring them as implicit objects, and the "current request" referenced by * ServletRequest * type is *** request ***. Can be treated implicitly.

For session scope

Class to use: *** HttpSession *** class

You need to retrieve an instance of the "session you are using" both when saving and retrieving data. It can be retrieved with * HttpServletRequest # getSession () *, and it is handled by putting it in a * HttpSession * type reference variable. There is also an implicit object here, and "currently in use session" can be implicitly handled by *** session ***.

Precautions when using session scope

In order to manage the session scope of multiple people at the same time, it is necessary to share the session ID between the client side and the server side, and link the scope and the session ID on the server side. In that case, as many scopes as there are clients will be prepared, and memory puncture may occur. Therefore, it is necessary to appropriately destroy the contents of the scopes and sessions. One example is session timeout, which discards sessions that have not been used for a certain period of time as garbage collection targets.

In addition, in order to make it easier to withstand a short period of request concentration, a method that voluntarily discards them is prepared and needs to be used appropriately by the developer.

For application scope

Class to use: *** ServletContext *** class

As with session scope, you need to get an instance of the "application you are using". The implicit object of this is treated with the variable name of *** application ***. If you want to get it as a method, you can get it with * HttpServlet # getServletContext () *.

Precautions when using application scope

Data can be saved in the application scope as an area common to all clients. However, since the data remains on the server side only temporarily, the scope of the application that ended at the same time as the server is terminated is destroyed. If you want to handle the data even after restarting, you need to leave it in the form of a file.

Scope type Request scope Session scope Application scope
Scope range Until the request is responded Until the session is destroyed Until the application is closed
Usage class ServletRequest HttpSession ServletContext
Implicit object of class request session application
Main applications input form Login information Common data
Other cautions Cannot be used because the redirect returns a response once If data accumulates, it will put a load on the server, so discard it as appropriate. If you want to reuse the data, output it to a file etc.

Recommended Posts

JSP-About the MVC model of Servlet, redirect forward, and scope
Think about the combination of Servlet and Ajax
What is the Java Servlet / JSP MVC model?
About redirect and forward
[Rails] How to get the URL of the transition source and redirect
This and that of the JDK
Set the number of seconds for fast forward and rewind in ExoPlayer
About the operation of next () and nextLine ()
Item 57: Minimize the scope of local variables
About the mechanism of the Web and HTTP
A simple example of an MVC model