1.在tomcat的webapps新建beerV1,目录结构如下
(class文件是编译后生成的)
2.form.html
<html>
<body>
<h1 align="center">Beer Selection Page</h1>
<form method="POST" action="SelectBeer.do">
Select beer characteristics<p>
Color:
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<br><br>
<center>
<input type="SUBMIT">
</center>
</form>
</body>
</html>
3.BeerExpert.java
package model;
public class BeerExpert{
public String getBrands(String color){
if(color.equals("light"))
return "qingdao";
else if (color.equals("amber")) {
return "yinmai";
}else if (color.equals("brown")) {
return "xuehua";
}else if (color.equals("dark")) {
return "4";
}else return "no";
}
}
4.BeerSelect.java
package web;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BeerExpert;
public class BeerSelect extends HttpServlet{
public void doPost(HttpServletRequest reque,HttpServletResponse response) throws IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Beer Selection Advice<br>");
String c = reque.getParameter("color");
BeerExpert beerExpert = new BeerExpert();
String brand = beerExpert.getBrands(c);
out.println("<br>Got beer brand" + brand);
}
}
5.对两个java文件进行编译
jar包位置,model包(BeerExpert所在包)的位置,编译后的class文件存放路径,BeerSelect(要编译的文件)位置