1、输入商品信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="Shangpinxinxi" method="post"> <table> <tr align="center"><td>商品信息</td></tr> <tr><td>商品名称:<input type="text" name="ming"></td></tr> <tr><td>保质日期:<input type="text" name="yue"></td></tr> <tr><td>生产日期:<input type="text" name="date"></td></tr> <tr><td>出售价格:<input type="text" name="price"></td></tr> <tr algin="center"><td><input type="submit" value="提交信息"></td></tr> </table> </form> </body> </html>
2、输出商品信息
package lianxi; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Shangpinxinxi extends HttpServlet { private static final long serialVersionUID = 1L; public Shangpinxinxi() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); String nm=request.getParameter("ming"); String bz=request.getParameter("yue"); String p=request.getParameter("price"); String sc=request.getParameter("date"); if(nm!=""&&bz!=""&&sc!=""&&p!="") { response.getWriter().write("商品:"+nm+"<br>保质期:"+bz+"<br>生产日期:"+sc+"<br>价格:"+p); } else { response.getWriter().write("请输入完整地信息!<br>"); //3秒后,跳转到输入页面........... response.setHeader("refresh", "3;URL=shangpin.jsp"); } //response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
3、运行结果