I had Java 6 and Tomcat 7 already installed on my (Windows 7) machine, what I missed was just the IDE itself.
Installing Eclipse
I went to the Eclipse Web Tools Platform downloads page and I got a recent build (your choice, be daring or more conservative as you like). I downloaded the zipped file, put it somewhere on my machine, unzip it, and specify in the eclipse.ini file how to reach the java virtual machine, specifying the -vm option:
-vm C:\Program Files (x86)\Java\jdk1.6.0_20\bin\javaw.exeThen I started Eclipse, and I went on for creating a new project. The shortcut to do that is Ctrl-N. What I wanted to create was a "Dynamic Web Project", you'll find it in the Web folder.
I gave a fancy name to the project, specified the target runtime accordingly to my container (Apache Tomcat 7) and clicked the Finish button. After that I saw in the project explorer a folder for my new web app.
Model
The beer selection is done by a POJO so I created a Java class in the project Java Resources folder:
package ch03;
public class BeerExpert {
public String getBrand(String color, String body) {
StringBuilder brand = new StringBuilder("Our");
if(body.equals("light"))
brand.append(" Wishwash ");
else if(body.equals("medium"))
brand.append(" GoodOl' ");
else
brand.append(" McCoy ");
brand.append(color);
return brand.toString();
}
}ControlTime to create the servlet BeerSelector. You can do that in a number of way, for instance you can right click on the Deployment Descriptor (DD), then in the "New..." menu item you'll find a "Servlet" item to click and start the wizard process to generate a servlet.
I specified the Java package (ch03) and the class name (BeerSelector), then clicked on next, to use a URL mapping, "/BeerSelector.do", different from the default one. I confirmed, and let the wizard doing its job.
The doGet() asks the BeerExpert for a beer name and then forward to a JSP page for displaying the result. The doPost() simply calls doGet():
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String color = request.getParameter("color");
String body = request.getParameter("body");
String beer = new BeerExpert().getBrand(color, body);
request.setAttribute("beer", beer);
RequestDispatcher view = request.getRequestDispatcher("beerSelection.jsp");
view.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}ViewI created a couple of pages in the current WebContent folder.
The user access to the Beer Selector via an HTML page that sports a form like this:
<h1 align="center">Beer Selection Page</h1>
<form method="POST" action="./BeerSelector.do">
<p>Select beer characteristics ...</p>
<p>Color:
<select name="color">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select></p>
<p>Body:
<select name="body">
<option value="light">light</option>
<option value="medium">medium</option>
<option value="heavy">heavy</option>
</select></p>
<p><input type="SUBMIT"></p>
</form>And the feedback is shown in beerSelection.jsp, where the main point in it, is represented by this line:Our beer recommendation: <%= request.getAttribute("beer") %>
No comments:
Post a Comment