Hello everybody!
When I first started to teach myself about Servlet and JSP(because these two are like JavaEE-101), I was looking for a reference book that can give me the logic and make me understand everything clearly. That book is "Head First Servlets and JSP". So I definitely recommend this book if you are new to JavaEE and looking for somewhere to start or you are preparing for an exam. I read this book now and then and tried to experiment all the small task the book gives to readers. One of those tasks was "Beer Suggestion Project" which is very basic yet again very suitable for beginners. Here in this post you can find that project(you can also find it in the book though :)).
My project structure was like below at the end.
I started with index.html that can be seen below. There is a form that we use to take user choices and submit it via post method. The parameters we collect from the user will be submitted to "BeerSelect.do" which we will define in web.xml. So the container will know which servlet to go. "BeerSelect.do" will be displayed in the url instead of the servlet name.
|
index.html |
There are some configurations in web.xml. We specify welcome files and servlet mappings here. these configurations will help the container know which file is welcome file and which servlet requested.
|
web.xml |
The servlet will look like below. In the doPost method(as the form in index.html used post method), we get the parameter and send this parameter to the class' method. It then retrieves the result as a list. After that we can just send the result to the result page.
|
BeerSelectServlet |
The class can be seen below. There is a basic if else statement that returns different beer brands according to the incoming parameter.
|
BeerExpert |
result.jsp gets the list of results and display them using an iterator.
|
result.jsp |
Running the application, you will get the pages like below.
|
index.html |
|
result.jsp |
Hope to see you in the next blog post!
Selen
No comments:
Post a Comment