<form method="post"
action="SelectBeer.do"
>Select beer characteristics<p>
Color:
Action值为SelectBeer.do 对应的是一个映射,servlet映射在web.xml文件里.method为提交post
<servlet-mapping>
<servlet-name>Ch3 Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
Servlet-mapping 里面有一个servlet-name为Ch3 Beer 找到
<servlet>
<servlet-name>Ch3 Beer</servlet-name>
<servlet-class>com.gac.example.BeerSelect</servlet-class>
</servlet>
这个映射对应的类为BeerSelect
找到类
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("Beer Selection Advice<br>");
String c = req.getParameter("color");
out.println("<br> Got beer color "+c);
super.doPost(req, resp);
}
代码获取表单里的color的值 并且打印出来 这个方法是获取网页中的post请求。Post请求对应的方法是doPost 可以读取网页中的参数,设置值,改变网页。