Discuss all cases about attributes added in Request scope both in Spring MVC and HttpServlet API way.
Source code link: github.com/TinaXing2012/Spring
Project Name: ModelScopedAttributeXML
You can find my other related videos below:
XML slides: youtu.be/wbxuwvPetJc
XML Intellij Coding: youtu.be/sfOY0vdBzMg
Java configuration slides: youtu.be/RhjG4CarVG0
Java configuration Coding:youtu.be/KjQwrm4Jono
In this video I'm gonna talk about the attribute in request scope.
So I just have my project set up already if you still have doubt how to set up spring MVC project, you can look at my previous videos.
I will put a link in the description.
So you can just a copy and paste to watch it.
And so let's start I have a request cop.
Attribute controller here, I, add annotation controller to make sure spring gonna initialize for me.
Now I'm gonna have a method called gate.
Let's call request I, put a ballot I'm, not good at giving out with them.
In this case, get Maki.
Okay, when I make a call or when other application starts it's gonna go to this method.
And for this method, let me add a motor first, okay and I'm gonna return to request school.
A tribute tribute JSP page, I already have my JSP page ready, but actually there's nothing inside right.
So next step is how to put an advert in request scope.
If you watched my other videos, probably already know an attribute.
Okay, let's call channel name.
This channel is called nice xrg.
So when you in spring and we see when you write this piece of a statement, it's gonna add miss Xin Jie into the request cup, how to retrieve it is using the key channel name.
This is identical.
Not a technique is the same as you using h-e-b, 70 request.
Okay, request and are you using request that sit attribute.
And you give a key.
So here let me using alternate its you can see.
So this one this one is put an attribute author name.
And this is a value in to request scope.
This one is a spring MVC way to put attribute into request go.
And the behind the thing actually spring, every seat gonna call request dollar set attribute.
It doesn't have its own implementation or request.
Scope it's gonna using this one.
So now let me go to the JSP page to actually display the two names.
Okay, why is the modern spirit? The other one is using request scope to set an attribute, a request object.
So the first one is I using spring, spring, MVC way to 6h put in requests, scope by using modal.
Okay, then I'm gonna give the value here turn on them and a second away.
Yes using plan.
This is my plan, HTTP, servlet API to set attribute in request scope.
And this one I did a forgotten name.
Key is author name.
So not let me redo provide the application and and see if everything works and I have to switch back to switch to Chrome.
So I can show you I can show you the behavior in the Chrome.
Ok, go to the Chrome.
Now I'm gonna make a call to the request attributed controller, see they say the channel name.
This is the author name and it's, a retrieved attribute in the request got successfully.
So let me refresh go through the code with you again, when you add into the request scope.
And what when you return this just page it's gonna be a full word? Okay.
So forward happens on the server side, which means the URL doesn't change.
And the request attribute.
We are carried two days next request.
And which means the request cop is still available in this JSP page and I'm gonna make a slightly change like a string display requests, attribute.
And then this statement I'm gonna put it here.
And then here I add get mapping called play.
Now, I want to when I make a call to this.
Slash I want to this request that we are forward to this get mapping request how to do that return add the keyword forward.
And then slash is in this case, when I make a request to this, slash on the server side is gonna forward two days method, but they share the same request, a scope and the URL in the browser bar.
The you are the URL remains, no change.
So you ask your ceaseless, not a slash display.
And the in the GSP, you can still see the values because it's a forward.
Let me read the ploy and show you I got this idea from my students.
And they told me you don't need to read stellar server every time because you can't take a longer you just can redeploy the application so it's faster.
So now it's deployed successfully I'm gonna switch back to the chrome.
And here I'm gonna make a call again.
So I can do is another browser bar tab.
You can see you can still retrieve the edge beauty in the request scope and the browser pop.
The URL doesn't change because forward happened on the server side and the letting me change a little bit.
So you can see the request cop, doesn't occur it to the next request.
If I change a forward to redirect, okay, I change it to redirect that's.
The only thing I change and what's gonna happen is when you is redirect browser will make another request to slash display.
And when the browser make a new request, the request cop is the fresh one.
So you can no longer retrieve channel name and the author name in base request.
Let me redeploy, okay and it's done.
So I'm gonna switch to the browser and I'm gonna do is just click her enter.
You are seeing nothing being displayed here.
And if you take a look at the URL carefully, this URL, yes, changed because redirect it's gonna have a, to course, it's gonna make a second call to this URL.
And because you make a new call the it's gonna consider as a new request.
Okay, the request object.
And the response object cannot be the brand-new.
So the previous attribute are not carried to current a request that's.
The reason you are seeing nothing here, and you might see something stranger here you have a channel name attached as the curry string.
And this is something related to redirect.
Let me show you in the intelligent to explain.
So when you have a redirect and if you're using motor to add some attribute, okay, by the photo setting, if the value is a primitive type or it's, a string type, it's gonna automatically append as the query string.
So let me add another on for you add attribute.
And suppose here let Susan year.
And this is an integer right? And this is a preemie type, but if I create an object, let's, add one more.
Let me add a model.
Okay, package excise.
Go go letting me other model, let's call customer.
Okay, a customer private heat, ID, private string, which is an M custom.
Let me provide a caterer setters and I'm also provide constructed.
The first one is the photo construct the second one and makana provide a constructor with two fields.
And then I'm gonna go back to our controller here I will create a new customer.
Let me input, C, Us, T equals new customer 1 and name is called cleaner.
And then we using motor attached boot, see UST.
Okay, see UST.
Let me using customer and for the key.
So here, this channel name year customer of them are in request scope.
The difference is the value this channel name.
The value is a string type this year.
The value is the integer and the customer sorry.
The value is an object.
So when we use redirect, if with a value for the edge, put yes, a string or primitive type, then it's going to append in the redirect URL as a curse string by the if it's an object it's, not going to append, let me tree deploy and show you.
So you will see missing and the 2019, but you want to see this customer.
So the progress successfully letting me switch to the browser and I'm gonna refresh make a call to the slash and para see here you are see channel name and a year append as query string.
But that customer are not appended here.
And they say the default setting inside the when you have a redirect, okay, let me switch back.
Now here has another treat.
Another small like tip.
Suppose, I just give ABC here.
Okay, I just give a value what's.
A key for that.
Then you have to take a look at what's, the type of this value.
If this type is a string, then it's gonna have how to retrieve it it's gonna be string equals ABC.
So if I add a model that a dispute to be 12.0 and 12.0 is a double type and encourage string is gonna be look like this.
It goes 12.0.
So you can give explicitly to give a key, or you can just give the value.
If you give it a value, the key cannot be the type of this value and make sure the first letter is a lowercase.
Okay, because ABC type is a string right? And you have to make a straight Psilakis.
And this type is a double and make sure the key.
The first letter is a locus.
And a suppose you don't want to this kind of the heavier use also can to disable it how to disable it.
You have to do configuration, whereas the configuration go to the samih.
And then here has something called.
You cannot default mode.
Redirect let's using true.
Ok, then let me rerun redeploy step away or depress successfully.
Now let me switch back to the cron and I'm gonna open a new tab.
I'm gonna open a new tab then enter.
So you can see you have a display, but those color string like a channel name and year and not here anymore.
Hope you understand after watching this video about the attribute in request scope.
And in my next video, I'm gonna introduce the session, scope, if you are interesting and you can go and watch it.
Thank you for watching see you next time.
scope="request" : The object is accessible from any JSP page servicing the same HTTP request that is serviced by the JSP page that created the object. A request-scope object is stored in the implicit request object. The request scope ends at the conclusion of the HTTP request.What is request scoped? ›
If a component is marked with request scope, simultaneous requests each see a different instance of the component. This is true even when the same session sends two requests simultaneously; each request gets a pointer to a separate object.How to set application scope in Servlet? ›
The Context/Application object is available in a JSP page as an implicit object called application. In a servlet, you can the object by calling getServletContext(). or by explicitly calling getServletConfig(). getServletContext().What is the request scope of a Spring bean? ›
The request scope creates a bean instance for a single HTTP request, while the session scope creates a bean instance for an HTTP Session. The application scope creates the bean instance for the lifecycle of a ServletContext, and the websocket scope creates it for a particular WebSocket session.What is the scope attribute in JSP? ›
scope attribute identifies the lifecycle of the Action element. The id attribute and the scope attribute are directly related, as the scope attribute determines the lifespan of the object associated with the id.How to use scopes in API? ›
Ways to use API scopes. You can use API scopes in different ways: In an API where the calling application is a third-party, or external, application. In this case, the calling application will request authorization from the user to access the requested scopes, and the user will approve or deny the request.What is scope in API request? ›
The scope constrains the endpoints to which a client has access, and whether a client has read or write access to an endpoint. Scopes are defined in the Merchant Center or with the API Clients endpoint for a single project when creating an API Client. Once you create an API Client, you cannot redefine the scopes.What is scoped in Java? ›
Java scope defines where a certain variable or method is accessible in a program. Briefly: A variable declared in a method is visible from the beginning of the declaration to the end of the method (method scope).What is a request attribute? ›
They can only be accessed from within the function. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions. Local variables are created when a function starts, and deleted when the function is completed.
A bean's scope is set using the @Scope annotation. By default, the Spring framework creates exactly one instance for each bean declared in the IoC container. This instance is shared in the scope of the entire IoC container and is returned for all subsequent getBean() calls and bean references.What is the difference between application scoped and singleton? ›
A @Singleton bean has no client proxy and hence an instance is created eagerly when the bean is injected. By contrast, an instance of an @ApplicationScoped bean is created lazily, i.e. when a method is invoked upon an injected instance for the first time.What is session scope? ›
session. Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . global session. Scopes a single bean definition to the lifecycle of a global HTTP Session .What is the difference between singleton and request scope in Spring? ›
Singleton: Only one instance will be created for a single bean definition per Spring IoC container and the same object will be shared for each request made for that bean. Prototype: A new instance will be created for a single bean definition every time a request is made for that bean.How to read data from a request in JSP? ›
- getParameter(): You call request. ...
- getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example, checkbox.
- getParameterNames(): Call this method if you want a complete list of all parameters in the current request.
- getParameterValues(): This function is used if multiple values are returned as parameters from the page to the JSP page. ...
- getParameterNames(): This function is used to pull out all the values from the current request of the page.
- Cookie getCookies() – returns an array containing cookie objects that the client has sent.
- Enumeration getAttributeNames() – contains enumeration of names of attributes for request.
- Enumeration getHeaderNames() – contains enumeration of names of header .
How can one page be redirected to another page in JSP? For redirecting a page in JSP, we are calling response. sendRedirect(). By using this method, the server returns the response to the client, from where the next request comes, and it displays that URL.