0

I want to upload details of a hoarding in mysql database with an image using .jsp and servlet but when i call my servlet it gives a blank page..

My .jsp

<form action="HoardingProfile" method="post" enctype="multipart/form-data">
    <table style="height: 100%; width:100%;">
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Location</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" name="txtLocation"
                    placeholder="Location" required="yes">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Size</h3>
            </td>
            <td class="padd10" style="width: 50%;">
                <input class="tx bk" type="text" name="txtSize" placeholder="Size"
                    required="yes">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Owner Name</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" placeholder="Owner Name"
                    required="yes" name="txtName">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Contact No.</h3>
            </td>
            <td style="width: 50%;">
                <input type="text" class="tx bk" placeholder="Number"
                    required="yes" name="txtNumber">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Address</h3>
            </td>
            <td style="width: 50%;">
                <textarea class="tx bk2" placeholder="Address" required="yes"
                    name="txtAddress"></textarea>
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 15%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Picture</h3>
            </td>
            <td style="width: 50%;">
                <input type="file" name="file" id="file">
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 50%;">
                <h3>Google map</h3>
            </td>
            <td style="width: 50%;">
                <textarea class="tx bk2" placeholder="Google Map iframe"
                    name="txtMap"></textarea>
            </td>
        </tr>
        <tr style="height: 02%">
            <td>&nbsp;
            </td>
        </tr>
        <tr style="height: 10%;">
            <td class="tdleft" style="width: 80%;">
                <input class="btn" type="submit" name="Add" value="Save"
            </td>
            <td style="width: 20%;">
                <input class="btn" type="reset" value="Reset"
            </td>
        </tr>
    </table>
</form> 

and my servlet

 PrintWriter out = response.getWriter();
    try {
        // Apache Commons-Fileupload library classes
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu  = new ServletFileUpload(factory);

        if (! ServletFileUpload.isMultipartContent(request)) {
            System.out.println("Sorry. No file uploaded");
            return;
        }

        // parse request
        List<FileItem> items = sfu.parseRequest(request);
        out.print(items);
        FileItem  txtLocation = (FileItem) items.get(0);
        String location =  txtLocation.getString();
        out.println(location);
        FileItem txtSize = (FileItem) items.get(1);
        String   size =  txtSize.getString();
        out.println(size);
        FileItem txtName = (FileItem) items.get(2);
        String   name =  txtName.getString();
        out.println(name);
        FileItem txtNumber = (FileItem) items.get(3);
        String   number =  txtNumber.getString();
        out.println(number);
        FileItem txtAddress = (FileItem) items.get(4);
        String   address =  txtAddress.getString();
        out.println(address);
        // get uploaded file
        FileItem file = (FileItem) items.get(5);

        // Connect to Oracle
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/opm?zeroDateTimeBehavior=convertToNull","root","");
        con.setAutoCommit(false);

        PreparedStatement ps = con.prepareStatement("insert into hprofile(location,size,owner,address,phone,deleted,img) values(?,?,?,?,?,?,?)");
        ps.setString(1, location);
        ps.setString(2, size);
        ps.setString(3, name);
        ps.setString(4, number);
        ps.setString(5, address);
        // size must be converted to int otherwise it results in error
        ps.setBinaryStream(6, file.getInputStream(), (int) file.getSize());
        ps.executeUpdate();
        con.commit();
        con.close();
        out.println("Proto Added Successfully. <p> <a href='listphotos'>List Photos </a>");
    }
    catch(Exception ex) {
        out.println( "Error --> " + ex.getMessage());
    }
     finally {            
        out.close();
    }
2
  • Did you check your DB end with a query to see there is an insert has been made? Commented Apr 9, 2013 at 8:22
  • no insert made.. out.print(items) displays no item... might there be some problem..? Commented Apr 9, 2013 at 8:26

1 Answer 1

0

I found out that I made some mistakes while linking the libraries...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.