Question

I trying to access the html files inside the WebContent/alerts folder using its relative path from servlet. But i unable to access it using its relative path,

enter image description here

Access the file inside WebContent from Servlet using relative path:

protected Element getSummary(String value) throws IOException
{
    Element element=null;
    Summary summary = Summary.valueOf(value);
    switch(summary) {
    case rtp_summary:
        element=parseDIV(new File("../../WebContent/alerts/rtp_bcklg_mail.html"),"rtp_summary");
        break;
    case ffu_summary:
        element=parseDIV(new File("/../../WebContent/alerts/ffu_comp_bcklg_mail.html"),"ffu_summary");
        break;    
    default:
        System.out.println("Enter a valid choice");
        break;
    }
    return element;
}

Access the file inside WebContent from Java Thread using relative path:

 public class WriteJSONFile implements Runnable{

WriteJSONFile(){
}

@Override

public void run()
{
    try {
        createJSONFile();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

@SuppressWarnings("unchecked")
private static void createJSONFile() throws IOException
{
    String path="C:/Users/Thiru/Documents/Website Design/Macaw/";
    JSONArray jsonArray=new JSONArray();
    JSONObject rtpStatus=new JSONObject();
    rtpStatus.put("name","RTP");
    rtpStatus.put("status",parseDIV(new File(path+"WebContent/alerts/rtp_bcklg_mail.html"),"rtp_health"));
    jsonArray.add(rtpStatus);

    JSONObject proposalattribStatus=new JSONObject();
    proposalattribStatus.put("name","PROPOSAL ATTRIBUTE");
    proposalattribStatus.put("status",parseDIV(new File(path+"WebContent/alerts/prpsl_txtsrch.html"),"prpsl_attrb_health"));
    jsonArray.add(proposalattribStatus);

    writetoFile(jsonArray);
}


private static void writetoFile(JSONArray jsonArray) {
    try {
        String path="C:/Users/Thiru/Documents/Website Design/Macaw/";
        FileWriter file = new FileWriter(path+"WebContent/properties/status.json");
        file.write(jsonArray.toJSONString());
        file.flush();
        file.close();

    } catch (IOException e) {
        e.printStackTrace();
    }   
}

protected static String parseDIV(File input, String divElement)
        throws IOException {
    Document doc = Jsoup.parse(input, "UTF-8");
    String content = doc.getElementById(divElement).val();
    System.out.println(divElement +" "+content);
    return content;
}

}

And i also want to access the files inside Webcontent from RESTful web service method using relative path. Thanks in advance.

Was it helpful?

Solution

Use ServletContext.getRealPath() to locate the file on disk. Note that this only works if the webapp is "exploded" during deployment.

For example:

   File f = new File(getServletContext().getRealPath("alerts/rtp_bcklg_mail.html"));

OTHER TIPS

Current location of your files could be considered as a security issue. It is advisable to place such files in WEB-INF.

String filePath = getServletContext().getRealPath("/WEB-INF/alerts/rtp_bcklg_mail.html");
Properties props=new Properties();
    props.load(this.getServletContext().getResourceAsStream("/alerts/"+your_fileName+".html"));

by Calling ServletContext object we can get WebContext root from now on wards we can pass our file file statically

You can use servlet url pattern in your html page form action or html page a href tag.But your web.xml use servlet-mapping's url pattern like this (/projectname/servleturl)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top