Reputation: 402
There are two problems:
relative path
After reading the content from the file i am storing that content into a string and when I print the string it is not print the actual
values of cn and ci
following is the code.
String content = "";
String cn, ci;
cn = request.getParameter("carrier_name");
ci = request.getParameter("carrier_id");
BufferedReader in = new BufferedReader(new FileReader("C:\\Users\\abcd\\Documents\\NetBeansProjects\\sendmail\\web\\mailformat.html"));
String str;
while ((str = in.readLine()) != null) {
content += str;
}
in.close();
out.println(""+content+"");
mailformat.html
hello
carrier name :<label >"+cn+"</label>
carrier id <label>"+ci+"</label>
Upvotes: 0
Views: 77
Reputation: 17595
To get a relative path to your web-application you could use ServletContext#getRealPath
But since this seems to be a template for mail message you could just put into your classpath and retrieve it using getClass().getResourceAsStream(name)
InputStream is = getClass().getResourceAsStream(name);
BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
As for getting the values into your content you could replace the pace holders
<label >"+cn+"</label>
and
<label >"+ci+"</label>
with the values you get from the request using String#replace(str, replacement).
Upvotes: 0
Reputation: 2923
Instantiate a File instance from your path. Note that a relative path has to be relative to something, such as the current directory. BTW, use "/" instead of "\" in the path, as Windows doesn't care, its *nix friendly, and you avoid all the escaping.
Use a StringBuilder rather than concatenating String. It is more efficient and more flexible.
Initialize non-final local variables to null; it's a good practice.
You don't do anything in the code you show with cn and ci. Do you intend to replace "cn" and "ci" in content with the values of the variables? You'll have to do some sort of find and replace.
Upvotes: 1
Reputation: 3913
You can't just read in text and expect the JVM to understand it and treat it as a custom string format. You have to go through the string that you read from the file and find the tokens that should be replaced with the parameters.
This method might come in handy: String.replace(CharSequence, CharSequennce)
For the relative path part: Just use a relative path. There is no magic to it.
Upvotes: 1