08. Using Session

20 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
Already have an account? Log In

Transcript

In this session, let us learn how to handle session in a spring MVC application. When you want to carry data across multiple pages for a particular session, you need to handle it properly. That you are a model attribute you want to carry the model attribute across multiple pages for a particular person means you you need to add it to a session. When you are working in a day application session can be achieved our session can be handled with the help of HTTP session interface. In a spring MVC application, if you want to maintain the conversational state, you can do it in two ways. One day, if you think are direct session attributes, this is an annotation.

You are just going to specify the model attributes that has to be carried across multiple pages. So I've been a session attribute is used for handling facial Are you can use your good old HTTP session interface, the HTTP session object should be injected to the controller. That is you're just going to also HTTP session with elfa HTTP session you can carry that model attributes across multiple pages. Now, let us see how to handle session in a spring MVC application. Using a simple example. I will go back to eclipse I am an eclipse I have created a spring starter project with Weber's dependency.

I have done the initial setup. Let us first go through the flow and we will identify in which scenario we need to start with session or in which scenario we need to use this at the rate session attribute. Let me explain the flow. As usual. I have got the application class here. The model classes product, then I have got product controller, then three JSP pages whole success and add product form.

So first from the product controller, I go to Home dot JSP from home dot JSP. And looking in for ad for ads Prop, the method annotated with that form is calling our product form. Here also instead of request mapping what we can use aggregate mapping. Let me import this. Okay, now I need to go into our product from in our product form. As usual I'm having a form with form fields.

When I click Submit, it has to go to this add product action. I'll go to this product controller in the product controller I have got value add product method, request method dot post, in snap that what we can do now we can go in for post mapping Let me import this also I don't need this like and what is the model attribute name product only I have given the same name and I am returning success. What am I printing in success Let me see that in success. I am trying to print the product details point it is working fine. After that I want to edit the product details. When I want to edit the product details I need to check whether the value is getting populated in the form that's what we are going to see now let's move forward on this application select spring MVC session right click run as run on server.

Click Finish. The application has started I am in home dot JSP let me click Add product. Now I have got add product from the JSP. Now let me enter the details. TV console. 900 quantities 900 I'm not doing any validation I have got a four I need to filter values it will take me to success dot JSP let me click OK Here I have given it a sad customer I'll just go and change it add product form instead of add customer it has to be add product this is the problem of copy pasting right.

So here's Let me refresh our product there Got it? Can you see TV 990 Let me click Add products. Now I have got success dot JSP wherein I'm having the product name cost and quantity. Let me go to the success that JC see I am trying to print the products we have overridden to string so it is printing complete product details then I am trying to retrieve the product name using dollar product dot name. Now I want to edit the form. So here I have given a link href equal to edit for view and edit.

I need to see the form with the module so that I can modify it. So when I click Edit from where it has to go, it has to go to product controller here, I need to have one other method to call edit product from. So here it says Edit form, show edit form. And here is a product from Actually, I'm trying to keep the URL similar to the operation that I'm performing. So it is going to show me this edit product form. This is going to be similar to our product for Monday.

So I'll just copy this Ctrl V. Now I need to give it an edit product form. Click Ok. I will open the Edit product form now in the Edit product form here instead of add, let me change it to edit and what about the action? It has to be added product. Right now I will go back to products or troller similar to add product I need to have edit product also. So, I will add one more method this product and now I need to go into final dot JSP wherein I want to print the details after the product details are edited that page is similar to success dot JSP file just copy it and I will paste it here itself.

I will give the name of final dot JSP and Open final dot JSP I don't need this line right Ctrl D okay we are do navitus editors product details. Now, I have done a flow from success dot JSP I have a link when this link is clicked it has to take me to edit product form wherein I want the values to be populated when they are now the decision for the user to modify or change the product details. Once the product details such as when they click submit it has to be To this next page it till now whatever we are seeing we are not writing any code in the product controller here. Basically within this you are supposed to call the back end layer service layer service talking to do come back to the result we are not doing anything we are directly printing it once again we are working with hibernate you will see how actually the complete flow is from the controller it goes to the service it is coming back to the result and then going to the final dot JSP our success dot JSP like that.

Okay, now, let me run this application we'll see the flaw first. What is the problem? The application has started I am in home the JC let me click this link. I have bought that product from let me add the product details. Product button. Now I am in success dot JSP when it Click this link it will take me to edit product from one aidid now, I am in Edit product form dot JSP here what actually I'm expecting I am expecting the values to be populated in the form so, that it is easy for me to change the values or modify the value values, but in this case I am not getting it, how to get it using session attributes.

Now, let me just go back to this product controller I will explain the flow Now, let us see the flow in the product controller first I am an add products in our product I am trying I am using accurate model attribute products. So, what actually is happening spring is creating a product object setting all the values that are in the form fields to the instance variable that is it is calling the setter methods in the model class. Then it is our training it to add the rate model attribute it is something like model dot r attribute this product comma product. So, the dummy name over here is product you will think that dummy name you are retrieving it in success dot JSP that is why it is printing it properly what happens to the values the values are getting seized over here itself because using model you can carry it only to the immediate page.

Now, you are given a date from again it is going to the product controller there is no session set. So, from here you are trying to call edit product for similarly let me add it for cost and quantity. Okay I have added the details. Let me rerun the application. The application has started let me click this link. I have bought that product crop and enter the details.

Let me click Add products. When I click Add product, what actually is happening spring framework is creating a product objects taking All these values and setting it to the product objects and then it is giving a dummy name for the product object that product we are given at the rate model attribute product only No. So, that name only is given to us the dummy name, I will click Add product. Now, I am able to retrieve it in success dot JSP this value is available only till success dot JSP it will not be carried to any other page because it is not in this session I will click View one is it Am I getting any value already we are at $1 product dot name product data whatever but it is not shown over here why the reason is it is not set in a session. So, I will go back to the product controller.

Now what I have to do once the product is populated with the values it has to be set in a session so that it can be retrieved in all the pages. I want to retrieve it even in the Edit product form. Okay, so I am just go scroll up. And here I'm just going to give session attributes. Let us see what is happening now. within which I need to give it a name that I want to carry across multiple pages.

If you are having multiple model attributes that has to be carried across in that scenario, you need to give it in the form of a string array within curly braces you have to give the dummy name. Let me save this I will rerun the application I have brought the first page let me click the link. I will add the product details. Click Add product. I am getting an error. Let us see what is the error in the console.

I'll maximize console. I'll scroll up HTTP session required exception expected session attribute products. This is the message let us see what Actually is happening and minimize this, I will go to this product controller where exactly we are getting the error, Are we getting the error in this page? No, are we getting the error in this page No, our ad product form dot JSP is coming perfectly fine. Now, in this particular point we are getting an error exception basically an exception why in a fixed version is occurring, you are given our threat model attribute product. It means when you are using arsenate session attribute product and when you are trying to use this annotation this method is trying to retrieve a product from the session.

I hope you can understand because I've got a direct session attribute and I'm using our direct model attribute spring framework is trying to retrieve an object from this product and assign it to this variable. It is time to call get attribute method. What I am telling spring framework You set attribute create a product object and assign it to this product attributes, but what spring assigned to do it assigned to retrieve it from this session define to retrieve a product object from the session and assign it to this product with variable which is not possible why when you are using accurate product this object must be available, we have not created it, spring will not create it because you are using session attributes. If you don't have session attribute if you are trying to give a product object it will create it in this case it will not create So, explicitly you need to create a product object where even before going into the form you create the product object and keep it ready.

So, in this ad for what I will do, I will create a product object product product equal to new product. I am creating a product object and I am setting I am giving a dummy name for the for the product object as product how already no God model in parameter. So, using Model Model dot add attribute, the first name is the dominie or the attribute may product comma product. Now, I will tell you the flow what actually is happening when you are trying to call the fall yet a new product object is created by you and you have given the dummy name as product. This is stored in session. So, when this method is called that time it says the product object is created and it is stored in C sharp.

Now, when you're calling this add product method, spring will retrieve the object which is already present and start populating that object with the values I hope you can understand. Now, in this case, when you're trying to call accurate model attributes, your new object is not getting created. Spring is retrieving that object which is already created for you and then it is starting to populate that object with a new values is it clear because you have created a project sorry product, the product object is available spring retrieve that and fetch the value from the form. Now, I will save this I will rerun this application. So, when you are using session attributes the objects should be available already the attributes should be available already started and add the details let me click Add product. Now see it is working perfectly fine because spring is retrieving the already created objects and calling all the setter methods to set the form field values.

Now, I have got a product object which is populated with values and the dummy name is dollar product. Now, again you see what is happening Click View unaided. Now can you see it is coming with the calculated values how in product controller when you are going to call get it for it is coming to the stage Sameer you're calling edit product form in Edit product from the GSP you are trying to retrieve dollar products isn't available Of course it is available why because you have set it up session. So in all the methods after this method, this product object is available in this pay in this method it is an object with the values. In this method it is an object with values populated with values. From here you're going to success from success you're coming to this page.

Here again the product object is available with values okay because thing is already populated. That is why when you call edit product for the it is populated with values properly. Okay and now again when you come to this edit product what will happen you are having a product object with value. So, needless entering into this method. Now, the user has changed the quantity and the price. So, what spring will do spring is going to take the original object and take the modified values call the setter method, so, that the original object is edited properly.

If you try to print here you will be getting that modified object Okay, now, I will just go to the method here I'm here now, I will just change it to 670 and this also will change let me click Edit tronic. Now, can you see the values that we are changing is deflecting in final dot JSP Okay, now, I will go back to the product controller in the storage controller I will try printing the values Add product method also let me try to print the product defect details inside the show it for if you want to print the product details, again you need to use this model attribute at the rate model attribute product product if you give and if you try to print it you will be getting a product objects populated with the values from the Add product from okay. So, here let me just rerun the application understand this using average session attributes we are trying to maintain session for model attributes across multiple pages create multiple controllers actually, you need to go in for controllers for every specific model.

If it is product for product how one controller for you should have one controller, adding, deleting, updating, then editing everything has to be done in a single controller. So this is the right way of coding in spring. Let me click this link. The application has started and enter the details. If we click Add product and go to this console also because in the console, I have got the details. Can you see it coming with populated values, I'll click view and edit.

Now, I will change it to mobile and I will change the price to 1000 this 200 click Edit product. Now can you see this spring is looking for an existing object many are using at the rate session attributes so that it can populate it with values and what will happen if you try to print the product object is too overdone to string you will not get any value, but basically only after the side product for the value is getting populated. I hope it is clear. This is how you handle sessions in a spring MVC application. Whatever we have learnt till now everything is separated Like we have learned how to do validation how to work with forms, how to handle session going forward when we are working with Spring and Hibernate, I will just show you how to use all the concepts together. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.