Not getting Text Area value with file upload JSP and Servet 2.5

≡放荡痞女 提交于 2019-12-11 13:21:27

问题


I am trying to submit a form with text fields, text area, file field etc in a JSP form. I am using commons file upload for this form.

Here is my JSP form:

<form name="add_product_form" id="add_product_form" enctype="multipart/form-data" method="post" action="Product.Add">
    <div id="form-body">
        <div id="lebel">
            <font color="red">*</font>Product Name:
        </div>
        <div id="field">
            <input type="text" name="product_name" id="product_name" value="">
        </div>
        <div id="lebel">
             <font color="red">*</font>SKU No:
        </div>
        <div id="field">
            <input type="text" name="sku_no" id="sku_no" value="">
        </div>
        <div id="lebel">
             <font color="red">&nbsp;</font>In Date:
        </div>
        <div id="field">
            <input type="text" name="in_date" id="in_date" value="">
        </div>
        <div id="lebel">
            <font color="red">&nbsp;</font>Upload Image:
        </div>
        <div id="field">
            <input type="file" name="upload_image" id="upload_image" value="">
        </div>
        <div id="lebel">
            <font color="red">&nbsp;</font>Description:
        </div>
        <div id="field">
            <textarea name="description" id="description"></textarea>
        </div>
        <div id="lebel">
            &nbsp;
        </div>
        <div id="button_field">
            <input type="submit" name="add_product_button" id="add_product_button" value="Add  Product">
        </div>
    </div>
</form>

I am getting the value of the text fields using following methods.

List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator();

while ( i.hasNext () )
{
    FileItem fi = (FileItem)i.next();
    if ( !fi.isFormField () )
    {
        // Get the uploaded file parameters
        String fieldName = fi.getFieldName();
        String value = fi.getString();
        fileName = fi.getName();
        String contentType = fi.getContentType();
        boolean isInMemory = fi.isInMemory();
        long sizeInBytes = fi.getSize();
        // Write the file
        if( fileName.lastIndexOf("\\") >= 0 )
        {
            file = new File( filePath +
            fileName.substring( fileName.lastIndexOf("\\"))) ;
        }
        else
        {
            file = new File( filePath +
            fileName.substring(fileName.lastIndexOf("\\")+1)) ;
        }
        fi.write( file ) ;
    }
    else
    {
        String name = fi.getFieldName();
        String value = fi.getString();
        if( name.equals("product_name") )
        {
            productName = value;
        }
        else if( name.equals("sku_no") )
        {
            skuNo = value;
        }
        else if( name.equals("in_date") )
        {
            newDateString = value;
        }
        else if( name.equals("description") )
        {
            productDesc = value;
        }
    }
}

But I am not getting the value of "TextArea" I have used in my form with name "descripton".

Can anyone help me in getting the value of this text area when submitting the form.

Thanks


回答1:


there is some problem with it. you can give style to textbox and look it like textarea which will help you out




回答2:


Could not find the direct solution.

To implement this I used a hidden field and jquery.

On clicking the submit button, I smet the value of teh text area in the hidden field, then submit the form.

Here is the jquery code:

$('#add_product_button').click(function()
{
        var description = $("#description").val();
        $("#hidden_description").val(description);
        $("add_product_form").submit();
});



回答3:


I have encountered the same problem,and I have solved it!
Just put your 'textarea' tag before the 'input type="file" ....' tag,
and the Servlet can get the textarea value

I am not a English speaker and hope you can understand my solution :-)



来源:https://stackoverflow.com/questions/20327019/not-getting-text-area-value-with-file-upload-jsp-and-servet-2-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!