Servlet作业2-将表单提交的商品信息输出到页面中

1,表单页面 shangpin.html

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品信息</title>
</head>
<body> 商品信息<br> <form action="Shp" method="post"> 商品名称:<input type="text" name="name" >
<br>
商品类别:<input type="radio" name="kind" value="食品" checked>食品
<input type="radio" name="kind" value="办公用品">办公用品
<input type="radio" name="kind" value="服装">服装
<br>
商品简介:
<br>
<textarea rows="10" cols="20" name="intr"></textarea>
<br>
<input type="submit" value="提交"> </form> </body>
</html>

2.接收处理servlet: shp.java

 package com.hanqi;

 import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class Shp
*/
public class Shp extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#HttpServlet()
*/
public Shp() {
super();
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//指定字符集
response.setCharacterEncoding("GBK");
request.setCharacterEncoding("utf-8");
//获取表单提交信息
String name = request.getParameter("name");
String kind = request.getParameter("kind");
String intr = request.getParameter("intr"); // //Request字符集转换
// String zname = new String(name.getBytes("iso-8859-1"),"utf-8");
// String zkind = new String(kind.getBytes("iso-8859-1"),"utf-8");
// String zintr = new String(intr.getBytes("iso-8859-1"),"utf-8");
//
response.getWriter().append("商品名称:" + name + "; 商品类别:" + kind + "; 商品简介:" + intr); //response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
// response.setCharacterEncoding("gbk");
// request.setCharacterEncoding("utf-8");
// //获取表单提交信息
// String name = request.getParameter("name");
// String kind = request.getParameter("kind");
// String intr = request.getParameter("intr");
//
// response.getWriter().append("商品名称:" + name + "; 商品类别:" + kind + "; 商品简介:" + intr); } }
上一篇:css透明度的设置 (兼容所有浏览器)


下一篇:2017微软秋招A题