利用servlet回实现动态页面的提交效果

利用servlet来实现动态页面的提交效果
在一个工程内新建一个html文件,设置好内容后,创建一个servlet文件,而html中的action必须和servlet保持一致;同时要注意乱码事件;

package webprojecthomework;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetGoodsServlet extends HttpServlet {

/**
* Constructor of the object.
*/
// public GetGoodsServlet() {
// super();
// }

/**
* Destruction of the servlet. <br>
*/
// public void destroy() {
// super.destroy();
// }

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
*            the request send by the client to the server
* @param response
*            the response send by the server to the client
* @throws ServletException
*             if an error occurred
* @throws IOException
*             if an error occurred
*/
@SuppressWarnings("null")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");//设置编码格式
response.setContentType("text/html;charset=UTF-8");
String goods = request.getParameter("goods");//获取文本框name值
String btn = request.getParameter("btn");
ArrayList<Goods> templist = new ArrayList<Goods>();//创建动态数组存放数据
for (int i = 0; i < GoodsData.goodslist.size(); i++) {
Goods tempgoods = GoodsData.goodslist.get(i);
String name = tempgoods.getName();
if (name.indexOf(goods) != -1) {
templist.add(tempgoods);
}
}
PrintWriter out = response.getWriter();
if (templist.size() == 0) {
out.println("没有符合要求的商品<br>");
} else {
out.println("<table border=1 width='600'>");
out.println("<tr>");
out.println("<td>编号</td>");
out.println("<td>名称</td>");
out.println("<td>品牌</td>");
out.println("<td>价格</td>");
out.println("<td>颜色</td>");
out.println("</tr>");
for (int i = 0; i < templist.size(); i++) {
Goods findgoods = templist.get(i);
out.println("<tr>");
out.println("<td>" + findgoods.getId() + "</td>");
out.println("<td>" + findgoods.getName() + "</td>");
out.println("<td>" + findgoods.getBrand() + "</td>");
out.println("<td>" + findgoods.getPrice() + "</td>");
out.println("<td>" + findgoods.getColor() + "</td>");
out.println("</tr>");
}
out.println("</table>");
}
out.println("</body>");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
*             if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}