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
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
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.