1,表单页面 shangpin.html
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>商品信息</title> 6 </head> 7 <body> 8 9 商品信息<br> 10 11 <form action="Shp" method="post"> 12 13 商品名称:<input type="text" name="name" > 14 <br> 15 商品类别:<input type="radio" name="kind" value="食品" checked>食品 16 <input type="radio" name="kind" value="办公用品">办公用品 17 <input type="radio" name="kind" value="服装">服装 18 <br> 19 商品简介: 20 <br> 21 <textarea rows="10" cols="20" name="intr"></textarea> 22 <br> 23 <input type="submit" value="提交"> 24 25 </form> 26 27 </body> 28 </html>
2.接收处理servlet: shp.java
1 package com.hanqi; 2 3 import java.io.IOException; 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 /** 10 * Servlet implementation class Shp 11 */ 12 public class Shp extends HttpServlet { 13 private static final long serialVersionUID = 1L; 14 15 /** 16 * @see HttpServlet#HttpServlet() 17 */ 18 public Shp() { 19 super(); 20 // TODO Auto-generated constructor stub 21 } 22 23 /** 24 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 25 */ 26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 27 // TODO Auto-generated method stub 28 //指定字符集 29 response.setCharacterEncoding("GBK"); 30 request.setCharacterEncoding("utf-8"); 31 //获取表单提交信息 32 String name = request.getParameter("name"); 33 String kind = request.getParameter("kind"); 34 String intr = request.getParameter("intr"); 35 36 // //Request字符集转换 37 // String zname = new String(name.getBytes("iso-8859-1"),"utf-8"); 38 // String zkind = new String(kind.getBytes("iso-8859-1"),"utf-8"); 39 // String zintr = new String(intr.getBytes("iso-8859-1"),"utf-8"); 40 // 41 response.getWriter().append("商品名称:" + name + "; 商品类别:" + kind + "; 商品简介:" + intr); 42 43 //response.getWriter().append("Served at: ").append(request.getContextPath()); 44 } 45 46 /** 47 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 48 */ 49 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 50 // TODO Auto-generated method stub 51 doGet(request, response); 52 // response.setCharacterEncoding("gbk"); 53 // request.setCharacterEncoding("utf-8"); 54 // //获取表单提交信息 55 // String name = request.getParameter("name"); 56 // String kind = request.getParameter("kind"); 57 // String intr = request.getParameter("intr"); 58 // 59 // response.getWriter().append("商品名称:" + name + "; 商品类别:" + kind + "; 商品简介:" + intr); 60 61 } 62 63 }