码迷,mamicode.com
首页 > 其他好文 > 详细

编写servlet,输出商品信息

时间:2016-07-01 16:09:05      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:

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、运行结果

技术分享

技术分享

 

编写servlet,输出商品信息

标签:

原文地址:http://www.cnblogs.com/jakeasd/p/5633230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!