zoukankan      html  css  js  c++  java
  • 编写servlet,输出商品信息

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

  • 相关阅读:
    SpringSource发布Spring Data Redis 1.0.0
    C#实现的GDI+时钟
    敏捷团队应对打扰的七种方法
    JBoss发布Hibernate 4.0
    在 IE10 的 XHR 的麦子
    Spring AMQP 1.0 GA发布了
    对 64 个以上逻辑处理器使用任务管理器
    预览Visual Studio11: 敏捷的支持、团队协作以及代码克隆监测
    在 Windows 8 中支持传感器
    HTTP API可演进性最佳实践
  • 原文地址:https://www.cnblogs.com/jakeasd/p/5633230.html
Copyright © 2011-2022 走看看