zoukankan      html  css  js  c++  java
  • 编写一个Servlet,将表单提交的商品信息输出到页面中

    <%@ 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="xs2" method="post">
    <table align="center" width="400" height="200" border="1"cellspacing="0" cellpadding="0">
    <tr><td align="center" colspan="2">请输入商品信息</td></tr>
    <tr><td align="right">商品名</td><td><input type="text"   name="xm"></td></tr>
    <tr><td align="right">产地</td><td><input type="text"   name="xb"></td></tr>
    <tr><td align="right">价格</td><td><input type="text"   name="nl"></td></tr>
    <tr><td align="center" colspan="2"><input type="submit" value="提交"></td></tr>
    </table>
    </form>
    
    </body>
    package com.hanqi.web;
    
    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 xs2 extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        
        public xs2() {
            super();
            
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            response.setContentType("text/html");
            response.setCharacterEncoding("UTF-8");
            
            String xm=new String(request.getParameter("xm").getBytes("ISO-8859-1"),"UTF-8");;
            String xb= new String(request.getParameter("xb").getBytes("ISO-8859-1"),"UTF-8");
            String nl=request.getParameter("nl");
             if(xm!=null&&xb!=null&nl!=null)
                {
                    if(xm!=""&&xb!=""&nl!="")
                    {
                        response.getWriter().write("产品信息为<br>产品名:"+xm+"<br>产地:"+xb+"<br>价格:"+nl);
                    }
                    else
                    {
                        response.getWriter().write("以上信息不能为空");
                    }
                }
                else
                {
                    response.getWriter().write("请采用正确方式访问");
                }
        }
    
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            doGet(request, response);
        }
    
    }

  • 相关阅读:
    代码校验工具 SublimeLinter 的安装与使用
    java中写sql语句的小小细节
    搭建Hexo博客并部署到Github
    更改npm全局模块和cache默认安装位置
    笔记本连接老式显示器(VGA线+HDMI接口)
    用JSON-server模拟REST API
    使用 Feed43
    Coding.net+Myeclipse 2014 Git配置
    line-height 属性
    border-style 属性
  • 原文地址:https://www.cnblogs.com/jskbk/p/5633506.html
Copyright © 2011-2022 走看看