Skip to main content

How to: Write a file inside webapp directory of Tomcat with Struts2 or JSP & Servlet





Hi! This time its for struts break for database, ever felt you wanna store files temporarly inside your server directory? then this post is to help you out if you are new to java and you have interest in learning, this is good to know! trust me even big folks won’t know this I’m going to tell for Struts2 framework.

============================================

Using ServletContext & ServletActionContext in Struts2

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

============================================

public String execute() {

try {

String path = “/folder1/file1.txt” ;

System.out.println(“where it might write ”

+ System.getProperty(“user.dir”));

ServletContext context = ServletActionContext.getServletContext(); //Line No.6

path = context.getRealPath(path);

File file = new File(path);

// if file doesnt exists, then create it

if (!file.exists()) {

System.out.println(“trying to creat new file at ” + path);

// Getting parent directory then creating it

File parent = file.getParentFile();

parent.mkdirs();

// Creating file

file.createNewFile();

}


FileWriter fw = new FileWriter(file);

BufferedWriter bw = new BufferedWriter(fw);

bw.write(“This is Written to file”);

System.out.println(“Where it wrote ” + file.getAbsolutePath());

bw.close();

System.out.println(“Done”);

return “success”;

} catch (IOException e) {

e.printStackTrace();

return “input”;

}

}

“String pathname = context.getRealPath(filename);” is the trick here. getRealPath(fielpath) return the exact path corresponding to the server. I said right even experienced folks dont know, cause this is not goot practice :oh!no: if the server is unpacket getRealPath() will return null!! so the best was is to return as String and handle it in JSP or whatever way to can. Anyway Server will be unpacked by default but if something screw up!  Ok TC keep thinking.


For Jsp & Servlet replace line no. 6 with *ServletContext context = this.getServletContext();* Class must extend HttpServlet and init(config) must have the call super.init(config) else context will be null.


Popular posts from this blog

Up and Close with Sudalai Madan: The Encounter

Night of 13th April all the preparations for the rituals were done in the temple, Sudalaimadam swamies idol was decorated with flowers and fruits and coconuts dedicated to the lord reached above his chest.My cousin Sudalai muthu, senior priest of the shrine reached home by late night got blessings of his father Late Shanmugam Sundaram and blessed the family members in 8x10 room dedicated for God, then started towards the temple. There peoples in huge number were waiting for the Sudalaimada swamy's arrival. Different rituals were offered by people to the Lord in order to get the blessings. As its is believed Sudaimada swamy, son Lord Siva used to consume meat in Kailash for this reason he is sent to earth were he can satisfy his earthy hunger there by not polluting Kailash.



Offering meat to Sudalaimadan is the most important and an watchful event in the festival. Many devotees offer their goat & pig so as to please the lord and get his blessings. That midnight Sudalaimada swamy …

Training @ Cognizant

I received my Date of Joining mail on 16 Oct 2011, I was so happy that finally I got that. I was the only one from my college to get the mail to report on that particular day so I went pretty much alone. I reported in MEPZ on Oct 20th from the next Monday my training began in Siruseri. I was able to spot many people who studied with me in school on the first day of Induction.
Initially I was placed in customized batch then was moved to Advance JAVA or J2EE in a generic term. I got pinned to IME vertical that handles clients related to Information, Media and Entertainment domain.We were trained in batches of 24, curse or charm I got into a batch with 20 girls! Whatever it didn't last for long, I was pretty much comfortable after getting out of it. My school was co-ed but classes were separate for boys and girls never mind 8th and 12th grades, where I was put into co-ed classes because I'm a good boy :P In college I remember studying together with girls nope remember seeing girls…

why do people sweat more in chennai???

One of the famous ornament of chennai is its summer!. Everyone here would have at least once talked about its effect and the way they affected. People comes out with sweats flowing from the body is the usual scene of chennai noon. The sweat produced is not just due to the temperature of the region. It is in fact due to the high humidity of chennai.

To explain this lets see a fact, people in chennai sweats more than the people in delhi where the temperature is higher than the former.

This is because of chennai's location. Chennai is located near sea due to which sea water evaporation is high during summer which in turn increases the humidity (Amount of water vapour present in air).
Normally human blood temperature is about 35 deg Celsius. If the outside temperature is more than that, then our system tries to automatically cools down by releasing sweat from the body.

The sweat which is released cools the surrounding heat by converting into vapour thereby reducing the body temperature…