Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the ServletContext in Rest web service

Tags:

java

jersey

I am implementing rest web service using Jersey. I need to have object of ServletContext to save the file in the application directory. Please help me to get the context.

I am calling this webservice from android device.

Thanks in advance.

@Path("notice")
public class NoticeResources {

    @Resource
    private ServletContext context;


    @POST
    @Path("uploadphoto")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces("text/plain")
    public String uploadNotices(@FormDataParam("file") InputStream uploadedInputStream) {

        File photoDirectory = new File("\\photo");

        // if the directory does not exist, create it
        if (!photoDirectory.exists()) {
            boolean result = photoDirectory.mkdir();  
            if(result){
                System.out.println("DIR created");
            }
        }

        String rootPath = photoDirectory.getAbsolutePath();

        String uploadedFileLocation = rootPath + "\\photo.jpg";
        // save it
        try {
            writeToFile(uploadedInputStream, uploadedFileLocation);
        } catch(Exception e) {
            return "no" + rootPath;
        }
        return "yes" + rootPath;
    }

    // save uploaded file to new location
    private void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation) throws Exception {
        OutputStream out = new FileOutputStream(new File(uploadedFileLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        out = new FileOutputStream(new File(uploadedFileLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            out.write(bytes, 0, read);
        }
        out.flush();
        out.close();
    }   
}
like image 604
N Sharma Avatar asked Jun 29 '13 10:06

N Sharma


People also ask

What is ServletContext interface?

Interface ServletContext. public interface ServletContext. Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine.

What is ServletContext object?

ServletContext is the object created by Servlet Container to share initial parameters or configuration information to the whole application.


2 Answers

Use @Context, here is Jersey documentation

@Context
private ServletContext context; 

UPDATED - you can also inject directly into methods if desired

public String uploadNotices(@Context ServletContext context, ...)
like image 195
ikumen Avatar answered Oct 08 '22 15:10

ikumen


use the annotation @context (Method level injection)

public Response getContext(@Context HttpServletRequest req, @Context HttpServletResponse res)throws Exception
{   
    System.out.println("Context Path is:"+req.getRequestURL().toString());
    result=req.getRequestURL().toString();
    return Response.status(200).entity(result).build();
}
like image 44
Sudeep Krishnan M Avatar answered Oct 08 '22 14:10

Sudeep Krishnan M