Trong bài viết này chúng ta hãy tạo ra một ví dụ về servlet đơn giản bằng cách sử dụng Eclipse IDE. Để tạo ứng dụng servlet chúng ta cần phải sử dụng các bước sau:
Các bước tiến hành tạo một file servlet như sau:
Environment sử dụng :
- JDK
- Eclipse IDE
- Apache Tomcat Server
Mở IDE Eclipse say đó chọn File Menu -> New-> Chọn Dynamic Web Project
- Sau đó nhấn Enter và nhấn Next.
- Click vào Generate web.xml deployment descriptor sau đó chọn click Finish.
- Bây giờ trong dự án, nhấp vào Java Resources ->chọn src , nhấp chuột phải và chọn New -> Package và nhập tên là com.itphutran.servlets
Bây giờ hãy nhấp vào gói com.itphutran.servlets và chọn New -> chọn Servlet
- Nhập tên lớp là ServletDemo và nhấn Finish .
Thêm nội dung sau vào ServletDemo.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.itphutran; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletDemo */ public class ServletDemo extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletDemo() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } |
file web.xml mapping cho servlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>demoservlet</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletDemo</servlet-name> <servlet-class>com.itphutran.servlets.ServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletDemo</servlet-name> <url-pattern>/demo-servlet</url-pattern> </servlet-mapping> </web-app> |
Khi chạy một ứng dụng web với servlet thì phương thức doGet() sẽ được chạy đầu tiên do đó tôi sẽ code in ra câu chào tại method doGet() của servlet như sau:
1 2 3 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().print("Xin chào Servlet!!!"); } |
Bây giờ tiến hành chạy dự án web:
Tổng kết
Qua bài này, chúng ta đã cùng nhau tìm hiểu để tạo ra một file servlet trong Java web cũng như đã tiến hành chạy thành công! Chúc các bạn thực hiện thành công nhé!
Nice replies in return of this question with genuine arguments and
describing the whole thing on the topic of that.
This page truly has all of the information I wanted about this subject and didn’t know who to ask.
Keep on working, great job!
Please let me know if you’re looking for a article author for your blog.
You have some really great articles and I think I would
be a good asset. If you ever want to take some of the load off,
I’d love to write some articles for your blog in exchange
for a link back to mine. Please shoot me an e-mail
if interested. Thanks!
I know this web site gives quality dependent content and additional material, is there any
other web site which gives these kinds of things in quality?
Very shortly this web page will be famous amid all blog viewers, due to it’s nice articles or
reviews
I have to thank you for the efforts you have put in penning this site.
I’m hoping to check out the same high-grade blog posts by you in the future as well.
In truth, your creative writing abilities has encouraged me
to get my very own blog now 😉
I’m more than happy to uncover this page. I want to to thank you for your time for this particularly wonderful read!!
I definitely really liked every part of it and i also have you saved to fav to see new information on your web site.
Good info. Lucky me I discovered your site by chance (stumbleupon).
I’ve bookmarked it for later!
Heya i am for the primary time here. I found this board and I to find It really helpful & it helped me out much.
I hope to offer something back and help others
like you helped me.
I simply want to mention I’m very new to blogs and truly enjoyed your page. Likely I’m want to bookmark your website . You actually come with superb article content. Thanks a bunch for sharing your website.
Can I just now say what a relief to find one who in fact knows what theyre speaking about online. You definitely understand how to bring a problem to light making it essential. Workout . should see this and understand this side from the story. I cant think youre less well-known when you absolutely provide the gift.
The next time I read a blog, I hope that it won’t disappoint me as much as this particular one. I mean, Yes, it was my choice to read through, nonetheless I genuinely believed you’d have something interesting to say. All I hear is a bunch of whining about something that you could fix if you weren’t too busy seeking attention.
Way cool! Some very valid points! I appreciate you writing this post and the rest of the site is very good.
hello https://www.wooricasinokorea.com 우리카지노
Having read this I thought it was rather enlightening. I appreciate you finding the time and effort to put this short article together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worthwhile.
Hi there! I could have sworn I’ve visited your blog before but after going through many of the articles I realized it’s new to me. Anyways, I’m definitely pleased I came across it and I’ll be bookmarking it and checking back often!
Hi, I do believe your web site might be having web browser compatibility problems. Whenever I take a look at your web site in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I merely wanted to give you a quick heads up! Apart from that, excellent blog!
Good post. I learn something new and challenging on websites I stumbleupon everyday. It will always be useful to read articles from other authors and use a little something from their websites.
One other issue issue is that video games can be serious anyway with the principal focus on learning rather than leisure. Although, it comes with an entertainment feature to keep your young ones engaged, every single game is usually designed to work on a specific set of skills or program, such as mathematics or research. Thanks for your write-up.
Do you believe past life regression? Do you think is reincarnation real?
Good blog you’ve got here.. It’s difficult to find good quality writing like yours nowadays. I honestly appreciate people like you! Take care!!
This page definitely has all the information I needed about this subject and didn’t know who to ask.
This website was… how do I say it? Relevant!! Finally I have found something which helped me. Cheers!
That is a very good tip especially to those new to the blogosphere. Short but very accurate info… Many thanks for sharing this one. A must read article.
I believe this is among the such a lot vital info for me.
And i’m happy studying your article. But wanna statement on few common issues,
The website style is great, the articles is in point of fact nice :
D. Good activity, cheers
I was able to find good advice from your blog posts.
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
more https://www.wooricasinokorea.com 블랙잭사이트
I really like reading through an article that can make men and women think. Also, thank you for allowing for me to comment.
If you desire to increase your knowledge just keep visiting
this web site and be updated with the newest news update posted here.
Oh my goodness! Impressive article dude! Thanks, However I am going through difficulties with your RSS. I don’t know the reason why I cannot join it. Is there anyone else having the same RSS issues? Anybody who knows the answer can you kindly respond? Thanks.
It’s hard to find well-informed people on this subject, but you seem like you know what you’re talking about! Thanks
Hello friends, its great paragraph regarding cultureand fully
explained, keep it up all the time.
Aw, this was an incredibly nice post. Taking a few minutes and actual effort to create a really good article… but what can I say… I put things off a whole lot and never seem to get anything done.
Very good information. Lucky me I recently found your site by accident (stumbleupon). I’ve book marked it for later!
Everyone says they love nature until they realize how dangerous she can be.
You should be a part of a contest for one of the highest quality websites on the web. I am going to recommend this website!
visit my homepage https://www.wooricasinokorea.com 소라넷
Excellent write-up. I definitely love this website. Continue the good work!
FreeSpinner is here to assist you with providing all the blog posts that you need.
Hi, I do think this is a great site. I stumbledupon it 😉 I may come back once again since i have saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and continue to help others.
I blog frequently and I really thank you for your information. This article has really peaked my interest. I will book mark your site and keep checking for new details about once a week. I opted in for your Feed as well.
Hi there! Do you use Twitter? I’d like to follow you if that
would be ok. I’m definitely enjoying your blog and look forward to new posts.
I merely want to show you that I am new to putting up a blog and really cherished your post. More than likely I am inclined to remember your blog post . You definitely have outstanding article material. Be Thankful For it for giving out with us all of your web post
Having read this I thought it was rather informative. I appreciate you finding the time and effort to put this informative article together. I once again find myself personally spending way too much time both reading and leaving comments. But so what, it was still worthwhile!
You should be a part of a contest for one of the most useful sites on the web. I am going to highly recommend this web site!
You could definitely see your enthusiasm within the article you
write. The arena hopes for even more passionate writers like you who aren’t afraid to say how they believe.
At all times go after your heart.
Great post! We will be linking to this great content on our site. Keep up the good writing.
I gotta favorite this site it seems very helpful handy
This is a very good tip particularly to those new to the blogosphere. Brief but very precise info… Thanks for sharing this one. A must read post!
Pretty! This was an incredibly wonderful article. Thank you for providing these details.
I really like your writing style, excellent information, regards for putting up : D.
Some truly prime posts on this internet site , saved to bookmarks .
Saved as a favorite, I love your site.
May I simply say what a relief to uncover somebody who really knows what they’re discussing on the net. You certainly know how to bring an issue to light and make it important. More people should read this and understand this side of your story. I was surprised you aren’t more popular since you surely have the gift.
Google
One of our visitors recently suggested the following website.
Google
Wonderful story, reckoned we could combine some unrelated data, nevertheless actually really worth taking a search, whoa did one particular study about Mid East has got additional problerms as well.