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

  • 相关阅读:
    C#扩展方法
    asp.net mvc获取http body中的json
    ASP.NET MVC 获取表单数据
    @Html.DropDownList()的四种用法及自定义DropDownList扩展

    MVC5+EF6入门完整教程6:Partial View
    Day3.13组件切换
    Day3.12组件中的data和methods
    Day3.11定义私有组件
    Day3.10组件定义方式三
  • 原文地址:https://www.cnblogs.com/jakeasd/p/5633230.html
Copyright © 2011-2022 走看看