Search code examples
javasoapfile-uploadjax-ws

How to upload an image to a mysql database with JAX-WS using a JSP client?


I am trying to upload an image to a mysql database using a SOAP web service developed with Java in GlassFish server. This web service is being consumed by a client in JSP. I've searched a lot, but couldn't find a proper answer.

Could anyone help me? Thanks in advance!


Solution

  • You have to create the client code to consume the web service using JAX-WS or another framework like CXF, Axis or Spring WS.The client code will be in the controller of your application. JSP will act as view to send the data to send to the service to the controller, then the controller will interact with the web service.

    Here's a skeleton of the JSP and the controller:

    <form action="${request.contextPath}/path/to/controller" method="POST" enctype="multipart/form-data">
        File to upload:
        <input type="file" name="fileData" />
        <br />
        <!-- probably more fields, depending on your requirements... -->
        <input type="submit" value="Upload file">
    </form>
    

    Controller code (since you don't specifi an specific framework to be used, I'm using plain Servlet):

    @WebServlet("/path/to/controller")
    public class FileUploadToWSServlet {
    
        @Override
        public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
            //consume the data from JSP
    
            //pass the data received from JSP
            //to send it to consume the JAX-WS service
        }
    }
    

    Trying to consume the web service directly from JSP is doable through scriptlets but its usage should be avoided, so this approach is not recommended and is not part of my answer.