提交表单信息

来源:互联网 发布:清华学霸 知乎 编辑:程序博客网 时间:2024/05/21 06:45

一、GET实现搜索引擎

    1、概述
        以GET方式提交数据时,浏览器把表单内容组织成一个查询字符串(Query String),各变量之间以”&”链接,然后以Servlet路径加问号“?”加查询字符串的形式获取服务器内容。
        Servlet中HttpServletRequest对象通过方法getParameter(“a”)可以获取到aValue,getParameter(“b”)获取到bValue,getQueryString()获取到字符串”a=aValue&b=bValue”(必要的时候先使用setCharacterEncoding(encoding)设置实际编码方式)。
        GET方式提交表单内容时所有被提交的内容都将显示在浏览器地址栏中。因此提交敏感信息(如密码、银行账号等)时不能使用GET方式;提交过长的内容时候也不能使用GET方式。
        不经过FORM提交数据而直接以输入网址,或者单击链接的方式访问Servlet也被看做是GET方式提交数据。

    2、实例
        String type = request.getParameter(“type”);
        String word = request.getParameter(“word”);

    3、搜索中文
        想在该搜索引擎中搜索中文,需要修改TOMCAT目录下\conf\server.xml里设定的默认GET编码方式,否则会出现乱码。下面为server.xml需要修改的部分。粗体部分为添加的代码。如果不指定UTF-8方式编码,TOMCAT将使用ISO-8859-1编码。
        <Connector port="8080" protocol="HTTP/1.1"
            connectionTimeout="20000"
            redirectPort="8443"  URIEncoding=”UTF-8”/>

二、POST提交个人信息
    同GET方式一样,Servlet可以通过HttpServletRequest对象的getParameter(String param)方法获取param对应的参数值。
    不同的是,由于POST方式不会使用“?”以及“&”符号来组织一个QueryString,因此POST时getQueryString()将返回null。
    如果同一个名称的参数有多个值,则以字符串数组的形式提交给Servlet,Servlet可以通过HttpServletRequest的getParameterValues(Sting param)取得这个字符串数组。

三、上传文件客户端
        上传文件需要设置FORM的enctype属性为multipart/form-data。
        上传文件要使用文件域(<imput type=”file”/>)

四、上传文件服务器端

    1、概述
        SmartUpload与Apach Commons Fileupload
        Commons-fileupload.jar

    2、实例
        DiskFileUpload diskFileUpload = new DiskFileUpload();
        List<FileItem> list = diskFileUpload.parseRequest(request);
        for(FileItem fileItem : list){
            if(fileItem.isFormField()){
            // 如果是文本域
                if("description1".equals(fileItem.getFieldName())){
                    out.println("遍历到 description1 ... <br/>");
                    description1 = new String(fileItem.getString().getBytes(), "UTF-8");
                }
            }
            else{
            // 否则,为文件域
                if("file1".equals(fileItem.getFieldName())){
                    File remoteFile = new File(new String(fileItem.getName().getBytes(), "UTF-8"));
                    out.println("遍历到 file1 ... <br/>");
                    out.println("客户端文件位置为: " + remoteFile.getAbsolutePath() +"<br/>");
                    file1 = new File(this.getServletContext().getRealPath("attachment"), remoteFile.getName());
                    file1.getParentFile().mkdirs();
                    file1.createNewFile();
                    InputStream ins = fileItem.getInputStream();
                    OutputStream ous = new FileOutputStream(file1);
                    try{
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while((len=ins.read(buffer)) > -1)
                        ous.write(buffer, 0, len);
                        out.println("已保存文件" + file1.getAbsolutePath() + "<br/>");
                    }finally{
                    ous.close();
                    ins.close();
                }
            }
        }