1,表单页面 shangpin.html
1 <!DOCTYPE html
2 [/span>html
3 [/span>head
4 [/span>meta charset="UTF-8"
5 [/span>title
6
7 [/span>body
8
9 商品信息[/span>br
10
11 [/span>form action="Shp" method="post"
12
13 商品名称:[/span>input type="text" name="name"
14 [/span>br
15 商品类别:[/span>input type="radio" name="kind" value="食品" checked
16 [/span>input type="radio" name="kind" value="办公用品"
17 [/span>input type="radio" name="kind" value="服装"
18 [/span>br
19 商品简介:
20 [/span>br
21 [/span>textarea rows="10" cols="20" name="intr"
22 [/span>br
23 [/span>input type="submit" value="提交"
24
25
26
27
28
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 //代码效果参考:http://www.lyjsj.net.cn/wz/art_23192.html
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 //代码效果参考:http://www.lyjsj.net.cn/wz/art_23190.html
constructor stub21 }
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 }