Access / Read resource file from REST/SERVELET

Posted: September 29, 2014 in web

String path = request.getServletContext().getRealPath(PDF_PATH);

It will search from standalone/deployments of your server. I’m using jboss so I will have this valuse below.


Dont forget to add the HttpServletRequest attribute.


public class DocumentRestService {

private static final String PDF_PATH = "/resources/template/sample.pdf";

 public Response getDocument(@PathParam("carId") String carId, @Context HttpServletRequest request)
 throws IOException, DocumentException {

String path = request.getServletContext().getRealPath(PDF_PATH);

PdfReader reader = new PdfReader(path);

ByteArrayOutputStream out = new ByteArrayOutputStream();

PdfStamper pdfStamper = new PdfStamper(reader, out);
 AcroFields acroFields = pdfStamper.getAcroFields();
 for (String field : acroFields.getFields().keySet()) {
 acroFields.setField(field, field);
 ResponseBuilder response = Response.ok(out.toByteArray());

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s