Phân biệt giữa request.getParameter() và request.getAttribute() trong java?

Written by IT Phú Trần on 06/03/2017 in Câu hỏi phỏng vấn and LẬP TRÌNH JAVA and LẬP TRÌNH WEB VỚI JSP/SERVLET with 198 comments.

Sự khác biệt cơ bản giữa getAttribute() getParameter() trong java là kiểu dữ liệu trả về, với getAttribute() trả vềobject (đối tượng) và getParameter () trả về kiểu dữ liệu String. Đối với trường hợp cả hai cùng một name thì giá trị của nó sẽ dựa vào  kiểu dữ liệu để truy xuất.

Cách dùng Request.getParameter()

Request.getParameter() được sử dụng để trích xuất các thông số yêu cầu (tức là dữ liệu được gửi bằng cách thông qua dạng html). Khi sử dụng request.getParameter() luôn luôn trả về kiểu dữ liệu String.

Cách dùng Request.getAttribute()

Sử dụng request.getAttribute() để có được một object,sử dụng request.setAttribute() để truyền giá trị từ servlet sang jsp và để lấy giá trị đối tượng sử dụng request.getAttribute(). Bạn có thể thêm bất kỳ đối tượng bạn muốn như String,Integer…, các đối tượng Custom trong thực tếcủa dự án, bất kỳ đối tượng nào chúng ta mong muốn.Khi sử dụng request.setAttribute() thì chúng ta thường gữi dữ liệu sang sang file jsp (Theo mô hình MVC file jsp chính là V - View).Vì vậy chúng ta thường đi kèm với chuyển tiếp forward.

Ví dụ :

file first.jsp :

//file: first.jsp

 

<%@ page import=”java.util.*” import=”java.io.*”%>
<% request.setAttribute(“PAGE”, “first.jsp”);%>
<jsp:forward page=”/second.jsp”/>

 

và file second.jsp :

 

<%@ page import=”java.util.*” import=”java.io.*”%>
Lấy dữ liệu khi gữi từ file first.jsp : <%=request.getAttribute(“PAGE”)%><br>
Lấy dữ liệu từ người dùng : <%=request.getParameter(“CLIENT”)%>

 

Từ trình duyệt của bạn, chạy first.jsp?value=itphutran.com và thử truyền biến qua url bằng cách sử dụng ký tự ?value=itphutran.com.

Tại trang first.jsp lấy giá trị itphutran.com bằng cách :

<%=request.getParameter(“value”)%>

>> output: itphutran.com

198 Responses to “Phân biệt giữa request.getParameter() và request.getAttribute() trong java?

  1. Избавит от грибка всего за 20 дней и не допустит повторного заражения, благодаря комплексу из натуральных масел и лекарственных растений.
    Наш сайт: https://ljvgzmqd.morningeverning.com

  2. I do believe all of the ideas you’ve introduced to your post.
    They’re really convincing and will definitely work. Nonetheless,
    the posts are too short for newbies. May just you please extend them a little
    from subsequent time? Thank you for the post.

  3. Very great post. I simply stumbled upon your weblog and wished to mention that I’ve truly
    loved browsing your blog posts. After all I will be subscribing
    on your feed and I hope you write again soon!

  4. I simply want to mention I am new to blogging and site-building and honestly loved your web page. Very likely I’m likely to bookmark your blog post . You really come with outstanding articles and reviews. Appreciate it for sharing your webpage.

  5. A further issue is that video games are typically serious in nature with the most important focus on mastering rather than entertainment. Although, there is an entertainment element to keep your kids engaged, every single game is often designed to develop a specific expertise or programs, such as math or technology. Thanks for your write-up.

  6. I have realized some considerations through your site post. One other point I would like to mention is that there are various games on the market designed in particular for preschool age kids. They include pattern identification, colors, animals, and forms. These generally focus on familiarization as opposed to memorization. This makes little kids engaged without sensing like they are learning. Thanks

  7. Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  8. I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and amusing, and without a doubt, you have hit the nail on the head. The problem is something too few people are speaking intelligently about. Now i’m very happy I found this in my search for something regarding this.

  9. After I originally left a comment I seem to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get 4 emails with the exact same comment. Perhaps there is an easy method you can remove me from that service? Cheers.

  10. Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I will revisit once again since I book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to help other people.

  11. May I simply say what a comfort to uncover somebody who genuinely understands what they’re discussing online. You actually understand how to bring an issue to light and make it important. More and more people must look at this and understand this side of your story. I was surprised that you are not more popular because you most certainly possess the gift.

  12. Can I simply say what a relief to find somebody that genuinely knows what they’re talking about online. You definitely realize how to bring a problem to light and make it important. More people really need to read this and understand this side of the story. It’s surprising you are not more popular since you certainly have the gift.

  13. You have made some decent points there. I looked on the internet for more info about the issue and found most people will go along with your views on this site.

  14. An interesting discussion is worth comment. There’s no doubt that that you ought to write more about this topic, it might not be a taboo matter but generally people do not discuss such subjects. To the next! Best wishes.

  15. You have made some really good points there. I looked on the internet for more information about the issue and found most individuals will go along with your views on this website.

  16. Thanks for discussing your ideas. I would also like to convey that video games have been actually evolving. Modern tools and enhancements have made it simpler to create realistic and active games. These types of entertainment games were not really sensible when the real concept was first of all being tried. Just like other designs of technological know-how, video games too have had to develop by way of many decades. This is testimony for the fast continuing development of video games.

  17. You should be a part of a contest for one of the finest sites on the net. I’m going to highly recommend this blog!

  18. Hello there! This post could not be written much better! Reading through this post reminds me of my previous roommate! He continually kept preaching about this. I am going to forward this article to him. Fairly certain he’ll have a very good read. Thanks for sharing!

  19. Spot on with this write-up, I absolutely believe this amazing site needs far more attention. I’ll probably be back again to see more, thanks for the information!

  20. Hi there, There’s no doubt that your web site may be having internet browser compatibility problems. When I take a look at your website in Safari, it looks fine however, when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to give you a quick heads up! Besides that, excellent blog!

  21. This is the right website for everyone who would like to understand this topic. You know a whole lot its almost tough to argue with you (not that I personally will need to…HaHa). You definitely put a fresh spin on a subject which has been written about for a long time. Excellent stuff, just excellent.

  22. After I originally left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I recieve 4 emails with the exact same comment. There has to be a means you are able to remove me from that service? Thanks a lot.

  23. The very next time I read a blog, I hope that it won’t disappoint me as much as this particular one. After all, Yes, it was my choice to read, however I genuinely thought you would have something interesting to talk about. All I hear is a bunch of whining about something that you could possibly fix if you were not too busy seeking attention.

  24. Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be exciting to read through articles from other writers and practice a little something from other websites.

  25. Hi! I could have sworn I’ve been to your blog before but after looking at a few of the articles I realized it’s new to me. Anyways, I’m certainly delighted I came across it and I’ll be book-marking it and checking back often!

  26. Oh my goodness! Amazing article dude! Thank you so much, However I am going through troubles with your RSS. I don’t understand why I can’t join it. Is there anybody else having similar RSS problems? Anyone that knows the answer will you kindly respond? Thanx.

  27. I have to thank you for the efforts you have put in penning this website. I’m hoping to check out the same high-grade blog posts from you in the future as well. In fact, your creative writing abilities has motivated me to get my own website now 😉

  28. Hello there! This blog post could not be written much better! Going through this post reminds me of my previous roommate! He continually kept talking about this. I will send this information to him. Fairly certain he’ll have a great read. I appreciate you for sharing!

  29. I blog often and I really thank you for your information. This article has really peaked my interest. I will bookmark your blog and keep checking for new information about once a week. I subscribed to your RSS feed as well.

  30. Right here is the perfect site for anybody who hopes to understand this topic. You realize a whole lot its almost hard to argue with you (not that I really will need to…HaHa). You definitely put a new spin on a subject that has been discussed for years. Great stuff, just excellent.

  31. Next time I read a blog, Hopefully it won’t fail me just as much as this particular one. I mean, Yes, it was my choice to read through, nonetheless I really believed you’d have something interesting to talk about. All I hear is a bunch of complaining about something that you could fix if you weren’t too busy searching for attention.

  32. Hi there! This article could not be written any better! Looking at this post reminds me of my previous roommate! He always kept preaching about this. I’ll forward this post to him. Fairly certain he’s going to have a great read. Thank you for sharing!

  33. A motivating discussion is worth comment. I do believe that you should write more about this issue, it may not be a taboo subject but typically people don’t speak about such subjects. To the next! Kind regards!

  34. I’m extremely pleased to find this site. I need to to thank you for your time for this wonderful read!! I definitely appreciated every part of it and i also have you bookmarked to see new stuff on your website.

  35. I must thank you for the efforts you have put in penning this site. I really hope to view the same high-grade content from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own website now 😉

  36. Good post. I learn something new and challenging on blogs I stumbleupon on a daily basis. It’s always interesting to read through articles from other authors and practice a little something from their websites.

  37. Aw, this was an extremely nice post. Taking a few minutes and actual effort to create a good article… but what can I say… I hesitate a lot and don’t seem to get nearly anything done.

  38. Having read this I believed it was rather enlightening. I appreciate you finding the time and effort to put this informative article together. I once again find myself spending way too much time both reading and posting comments. But so what, it was still worth it.

  39. An impressive share! I’ve just forwarded this onto a coworker who had been conducting a little research on this. And he in fact bought me lunch because I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this topic here on your site.

  40. Your style is very unique compared to other people I have read stuff from. I appreciate you for posting when you’ve got the opportunity, Guess I’ll just bookmark this page.

  41. Having read this I believed it was rather enlightening. I appreciate you finding the time and energy to put this article together. I once again find myself personally spending way too much time both reading and posting comments. But so what, it was still worthwhile!

  42. This is a really good tip particularly to those fresh to the blogosphere. Short but very accurate information… Thanks for sharing this one. A must read article!

  43. A motivating discussion is definitely worth comment. I believe that you need to publish more about this subject, it may not be a taboo subject but generally folks don’t discuss such issues. To the next! All the best.

  44. It’s hard to come by educated people on this subject, however, you sound like you know what you’re talking about! Thanks

  45. I blog quite often and I genuinely thank you for your content. Your article has really peaked my interest. I will take a note of your website and keep checking for new information about once per week. I subscribed to your RSS feed too.

  46. Good post. I learn something new and challenging on websites I stumbleupon everyday. It will always be interesting to read content from other writers and use something from other websites.

  47. I needed to thank you for this wonderful read!! I definitely loved every bit of it. I have you book-marked to check out new stuff you post…

  48. What’s up, all is going nicely here and ofcourse
    every one is sharing facts, that’s truly fine,
    keep up writing.

  49. An outstanding share! I’ve just forwarded this onto a co-worker who was doing a little homework on this. And he in fact ordered me dinner because I stumbled upon it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanx for spending time to talk about this issue here on your internet site.

  50. I have been surfing online more than 3 hours today, yet
    I never found any interesting article like yours.

    It is pretty worth enough for me. In my opinion, if all website
    owners and bloggers made good content as you did, the net will be much more
    useful than ever before.

  51. Hi, i read your blog from time to time and i own a similar
    one and i was just curious if you get a lot of spam comments?
    If so how do you prevent it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any assistance is very much appreciated.

  52. Awesome blog! Do you have any tips and hints for aspiring writers?

    I’m hoping to start my own site soon but I’m a little lost on everything.

    Would you propose starting with a free platform
    like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed ..

    Any recommendations? Cheers!

  53. Having read this I believed it was very informative. I appreciate you spending
    some time and effort to put this content together. I
    once again find myself personally spending a lot of time both reading and commenting.
    But so what, it was still worthwhile!

  54. Hi there! Do you use Twitter? I’d like to follow you if that would be ok.

    I’m undoubtedly enjoying your blog and look
    forward to new updates.

  55. It’s a pity you don’t have a donate button! I’d most certainly donate
    to this superb blog! I guess for now i’ll settle for bookmarking and adding
    your RSS feed to my Google account. I look forward to new updates and will share this
    blog with my Facebook group. Talk soon!

  56. Excellent site you have here.. It’s hard to find high-quality writing like yours these days. I truly appreciate individuals like you! Take care!!

  57. Everyone loves what you guys are up too. Such clever work and exposure!
    Keep up the good works guys I’ve included you guys to my personal blogroll.

  58. If you are going for best contents like I do, simply pay
    a quick visit this site all the time because
    it provides quality contents, thanks

  59. Heya i’m for the primary time here. I came across this board and I to find It really helpful &
    it helped me out a lot. I’m hoping to present something back and aid
    others like you aided me.

  60. Hi there, I found your website via Google while looking for a related topic, your website came up, it seems to be
    great. I have bookmarked it in my google bookmarks.

    Hello there, simply become alert to your blog through Google, and located that it’s
    really informative. I’m going to be careful for brussels.
    I’ll be grateful when you proceed this in future.
    A lot of other folks shall be benefited from your writing. Cheers!

  61. Having read this I believed it was extremely enlightening. I appreciate you spending some time and effort to put this content together. I once again find myself spending a significant amount of time both reading and leaving comments. But so what, it was still worth it.

  62. Good post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be useful to read through articles from other authors and use something from their web sites.

  63. Hi! I could have sworn I’ve visited this site before but after looking at a few of the posts I realized it’s new to me. Anyhow, I’m certainly pleased I discovered it and I’ll be book-marking it and checking back regularly!

  64. I absolutely love your website.. Pleasant colors & theme. Did you create this website yourself? Please reply back as I’m hoping to create my own personal website and would like to know where you got this from or exactly what the theme is named. Thanks.

  65. Its like you read my mind! You seem to know so much about this, like
    you wrote the book in it or something. I think that you can do with some pics to drive the message home a bit,
    but other than that, this is great blog. A great read.
    I will certainly be back.

  66. I like the helpful information you provide for your articles.
    I will bookmark your blog and test again right here frequently.
    I’m reasonably certain I will learn plenty of new stuff
    right right here! Best of luck for the following!

  67. I am really inspired together with your writing skills as
    well as with the structure on your blog. Is this a paid theme or did you customize it
    your self? Anyway keep up the excellent quality writing,
    it is uncommon to see a nice weblog like this one nowadays..

  68. Spot on with this write-up, I absolutely feel this web site needs far more attention. I’ll probably be returning to read more, thanks for the information.

  69. Hi there, I think your web site may be having browser compatibility issues. When I look at your blog in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Apart from that, excellent site!

  70. I’m amazed, I have to admit. Rarely do I encounter a blog that’s both equally educative and engaging, and without a doubt,
    you have hit the nail on the head. The issue is something which not enough men and women are
    speaking intelligently about. I’m very happy that I found this during my hunt for something concerning this.

  71. I blog often and I truly appreciate your content. This great article has really peaked my interest. I’m going to bookmark your website and keep checking for new information about once per week. I opted in for your RSS feed as well.

  72. Thank you for sharing your thoughts. I truly appreciate your efforts and I
    will be waiting for your next write ups thanks once again.

  73. Excellent items from you, man. I have take note your stuff previous to and you’re simply too
    fantastic. I actually like what you have obtained right
    here, really like what you are stating and the way wherein you are saying it.
    You’re making it entertaining and you continue to care for
    to keep it sensible. I cant wait to read much more from
    you. This is actually a wonderful web site.

  74. Hi, I do believe this is an excellent web site. I stumbledupon it 😉 I
    may come back yet again since I saved as a favorite it.
    Money and freedom is the greatest way to change, may you be rich and continue to guide others.

  75. Do you mind if I quote a few of your posts as long as I provide credit and sources back to your blog?
    My blog site is in the exact same niche as yours and my users would really benefit from some of the information you present here.
    Please let me know if this okay with you. Thanks a lot!

  76. Hi, I believe your web site may be having internet browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however when opening in IE, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Other than that, wonderful site.

  77. I’m amazed, I have to admit. Rarely do I encounter a blog that’s both educative and engaging, and without a doubt, you’ve hit the nail on the head. The problem is something that too few men and women are speaking intelligently about. I’m very happy that I came across this during my search for something regarding this.

  78. Hi there! This blog post couldn’t be written any better!
    Going through this post reminds me of my previous roommate!
    He continually kept talking about this. I most certainly
    will forward this information to him. Pretty sure
    he’s going to have a good read. I appreciate you for sharing!

  79. I wanted to thank you for this wonderful read!! I certainly loved every bit of it. I’ve got you saved as a favorite to look at new stuff you post…

  80. After I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get four emails with the exact same comment. Perhaps there is a means you are able to remove me from that service? Many thanks.

  81. Having read this I thought it was really enlightening. I appreciate you spending some time and effort to put this informative article together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was still worth it!

  82. Aw, this was an exceptionally good post. Taking a few minutes and actual effort to produce a superb article… but what can I say… I procrastinate a whole lot and don’t manage to get anything done.

  83. Aw, this was an extremely nice post. Taking a few minutes and actual effort to create a good article… but what can I say… I hesitate a lot and don’t manage to get nearly anything done.

  84. You are so cool! I don’t suppose I’ve read through a single thing like this before. So nice to find another person with a few unique thoughts on this issue. Seriously.. thank you for starting this up. This website is something that is required on the internet, someone with some originality.

  85. The next time I read a blog, I hope that it doesn’t fail me as much as this particular one. After all, I know it was my choice to read, however I actually thought you would have something useful to talk about. All I hear is a bunch of complaining about something that you can fix if you were not too busy searching for attention.

  86. The next time I read a blog, I hope that it does not fail me just as much as this one. After all, I know it was my choice to read, nonetheless I genuinely believed you’d have something interesting to say. All I hear is a bunch of crying about something that you could fix if you were not too busy looking for attention.

  87. Hello there! This post couldn’t be written any better! Looking through this post reminds me of my previous roommate! He constantly kept preaching about this. I most certainly will forward this post to him. Pretty sure he will have a very good read. I appreciate you for sharing!

  88. Hi, I think your website could possibly be having web browser compatibility problems. When I look at your web site in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Besides that, excellent website.

  89. I wanted to thank you for this great read!! I absolutely loved every bit of it. I have you saved as a favorite to check out new things you post…

  90. You’re so awesome! I don’t think I have read through something like this before. So great to discover someone with original thoughts on this subject matter. Really.. thanks for starting this up. This website is one thing that is required on the internet, someone with a bit of originality.

  91. I’m amazed, I have to admit. Seldom do I encounter a blog that’s equally educative and amusing, and let me tell you, you’ve hit the nail on the head. The issue is something not enough folks are speaking intelligently about. Now i’m very happy I came across this during my search for something concerning this.

  92. You ought to take part in a contest for one of the greatest blogs online. I am going to recommend this blog!

  93. It’s hard to find experienced people about this topic, however, you seem like you know what you’re talking about! Thanks

  94. Oh my goodness! Impressive article dude! Thanks, However I am going through problems with your RSS. I don’t understand the reason why I am unable to join it. Is there anybody having similar RSS issues? Anyone that knows the answer will you kindly respond? Thanks!!

  95. I blog quite often and I seriously appreciate your content. This great article has really peaked my interest. I will bookmark your website and keep checking for new details about once a week. I opted in for your RSS feed as well.

  96. Good post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It will always be useful to read through articles from other authors and practice a little something from other sites.

  97. It’s hard to come by experienced people on this topic, however, you sound like you know what you’re talking about! Thanks

  98. Oh my goodness! Impressive article dude! Thanks, However I am experiencing troubles with your RSS. I don’t know why I cannot subscribe to it. Is there anybody else getting similar RSS issues? Anyone who knows the solution will you kindly respond? Thanx!!

  99. I blog often and I truly thank you for your content. The article has truly peaked my interest. I’m going to bookmark your blog and keep checking for new details about once per week. I subscribed to your Feed too.

  100. I’m very happy to find this great site. I wanted to thank you for ones time just for this fantastic read!! I definitely savored every little bit of it and I have you book-marked to see new things on your website.

  101. Aw, this was an incredibly nice post. Spending some time and actual effort to generate a really good article… but what can I say… I procrastinate a whole lot and don’t seem to get nearly anything done.

  102. I have to thank you for the efforts you have put in writing this site. I really hope to check out the same high-grade blog posts from you in the future as well. In truth, your creative writing abilities has inspired me to get my own website now 😉

  103. Hey, you used to write great, but the last several posts have been kinda boring… I miss your super writings. Past few posts are just a little bit out of track! come on!

  104. This is a good tip especially to those fresh to the blogosphere. Brief but very precise info… Many thanks for sharing this one. A must read article.

  105. May I simply just say what a comfort to find an individual who actually understands what they’re discussing over the internet. You certainly realize how to bring a problem to light and make it important. A lot more people ought to look at this and understand this side of your story. I was surprised that you’re not more popular given that you most certainly possess the gift.

  106. Howdy, There’s no doubt that your site could be having browser compatibility issues. When I look at your web site in Safari, it looks fine however when opening in IE, it has some overlapping issues. I merely wanted to provide you with a quick heads up! Besides that, fantastic site!

  107. Hi! I simply would like to give you a big thumbs up for your excellent info you have here on this post. I’ll be coming back to your web site for more soon.

  108. Google

    That will be the finish of this write-up. Here you will obtain some web sites that we think you’ll value, just click the links.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *