Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet Writing a file using servlets

Have Programming Question? Ask it here!
 
 
 


Kiran
Writing a file using servlets
2 Answer(s)      4 years and 3 months ago
Posted in : JSP-Servlet


I'm using a servlet to read an input from a jsp file and write into a .txt file in my computer. The code is good until reading the data and creating a file in my space. but it is not writing the values. The code i tried is pasted below. I can get some value into string s, but its not writing in the xyz.txt file. I think writing in a servlet should be in a different manner. But I donno hw to implement it. Any help is appreciated.


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//String sub =(String) request.getParameter("Submit");
//int IP = request.getRemotePort();
PrintWriter out = response.getWriter();
//out.println(IP);
//if (sub.trim().equalsIgnoreCase("Submit")){


String s = request.getParameter("Name").toString();
out.println("\t"+s);
while (s != null){
try {
boolean append = true;
//out = new PrintWriter(new FileWriter("C:/Users/Karin/NewFolder/xyz.txt", true));

FileWriter writer = new FileWriter("C:/Users/Karin/NewFolder/xyz.txt",append);
writer.write(s);
// out.println(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

}


Advertisement
View Answers

June 5, 2010 at 5:29 PM


Hi Friend,

Try the following code:

1)register.jsp:
<html>
<body>
<center><h3>Registration Form</h3></center>
<form method="get" action="../WriteToFileServlet">
<center>
<table>
<tr><td>First Name:</td><td><input type="text" name="firstname" size="20"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lastname" size="20"></td></tr>
<tr><td>User Name:</td><td><input type="text" name="username" size="20"></td></tr>
<tr><td>Address </td><td><input type="text" name="address" size="20"></td></tr>
<tr><td>Contact No </td><td><input type="text" name="contact" size="20"></td></tr>
<tr><td>Email: </td><td><input type="text" name="email" size="20"></td></tr>
<tr><td>City: </td><td><input type="text" name="city" size="20"></td></tr>
<tr><td>State: </td><td><input type="text" name="state" size="20"></td></tr>
<tr><td>Country: </td><td> <input type="text" name="country" size="20"></td></tr>
<tr><td><input type="submit" value="Submit" name="B1"></td><td><input type="reset" value="Reset" name="B2"></td></tr>
</table>
</center>
</form>

2)WriteToFileServlet.java

import java.io.*;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WriteToFileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
String fname= request.getParameter("firstname");
String lname= request.getParameter("lastname");
String uname= request.getParameter("username");
String pass= request.getParameter("password");
String address= request.getParameter("address");
String contact=request.getParameter("contact");
String email=request.getParameter("email");
String city=request.getParameter("city");
String state=request.getParameter("state");
String country=request.getParameter("country");
try{
File file = new File("C:/xyz.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(fname+" "+lname+" "+uname+" "+address+" "+contact+" "+email+" "+city+" "+state+" "+country);
out.newLine();
out.close();
pw.println("File is created successfully");
}

catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
}

Thanks
</body>
</html>


June 5, 2010 at 11:19 PM


Thanks a lot, i got it. it worked. thanks once again.


Related Tutorials/Questions & Answers:
Writing a file using servlets - JSP-Servlet
Writing a file using servlets  I'm using a servlet to read an input... reading the data and creating a file in my space. but it is not writing..., but its not writing in the xyz.txt file. I think writing in a servlet should
form based file upload using servlets
form based file upload using servlets  Hai all, I wrote a program to upload a file into specified directory and store the file path and username... file path it uploaded successfully but in database table it gives null value
Advertisements
read XML file and display it using java servlets
read XML file and display it using java servlets  sir, i can't access Xml which is present in my d drive plz can u should go through my code n tell me the things where i went wrong java servlet program protected void
Upload CSV File into Columns of sql table using servlets and jsp
Upload CSV File into Columns of sql table using servlets and jsp  Hello sir, plz give me the code to upload csv file data into respective columns of table
accessing data from a json file writing javascript or using jQuery
accessing data from a json file writing javascript or using jQuery  I am trying to create a page from json objects using javascript. I have this huge json file containing several objects that contain arrays for the objects
Problem while using a HashMap for writing text ina RTF file. - Framework
Problem while using a HashMap for writing text ina RTF file.  Hi, I am trying to generate a RTF file using iText.jar, wherein I am taking... matched to the string then only write to the RTF file. Problem is Document writes
java struts servlets writing - Struts
java struts servlets writing  sir..i am using eclipse here for software development.and i just want to know what all are the neccessary steps to write servlets pragrams for stuts applicatuin.here am developing a small application
How to Upload a file directly to Oracle database using JSP or Servlets?
How to Upload a file directly to Oracle database using JSP or Servlets?  Hi I want to upload a file(csv or excel) to Oracle 10g Database using JSP...;Upload File to Oracle Database</h2> <form id="form1" enctype
File saving and Writing
File saving and Writing   Hello Rose india..... I have a doubt... a "file create option" it will be ".csv" file and i want to write the data from arraylist into .csv file and i want to save that file so it will ask for "save
How to access the image file from project folder itself while writing to pdf using itext?
How to access the image file from project folder itself while writing to pdf using itext?  I am writing a pdf using itext where i add a image... accomplish this.I am using simple java program to write the pdf.Not a servlet.Thanks
Writing and Reading A File
Writing and Reading A File  Hello, I've been trying to learn writing and reading data from file for our assignment, but just stuck on how to proceed... (FirstName, LastName, Age). I also made an empty file I named "contactsFile.txt" where
writing and appending to a file
writing and appending to a file  My input file includes data from... and values and writes into an existing file for 'male'. How can I do the same..." + " :class :" + Name);// appends the string to the file
File Writing - Java Beginners
File Writing  Hi... I need a syntax or logic or program by which we can write into the desired shell of the Excel file from console window...  Hi friend, I am sending you a link. This is will help you. Please
Writing xml file - Java Beginners
Writing xml file  Thank you for the quick response The values which... XmlServlet().createXmlTree(doc); System.out.println("Xml File Created... = sw.toString(); File file = new File("c:/newxml.xml
Writing a loop statement using Netbean
Writing a loop statement using Netbean  Write a loop program using NetBeans.   Java Loops class Loops{ public static void main(String[] args){ int sum=0; for(int i=1;i<=10;i
run servlets using Glassfish V3
run servlets using Glassfish V3  Hello to all, I was wondering how to run servlets using Glassfish V3. I'm using NetBeans 6.7 and need to use servlets, but I don't want to install Tomcat to do so, any suggestions
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program
counting the values in input file and and writing the output to a file
counting the values in input file and and writing the output to a file  Can any one please correct this code. This is to read a file and later...-code/16483-counting-values-input-file-writing-output-file.html this code
servlets DD file - JSP-Servlet
servlets DD file  how many DD files can we possibly have in our servlets project
Writing Log Records to a Log File
Writing Log Records to a Log File       This section demonstrates for writing log records to a log file. Logger provides different types of level like: warning, info
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks..... after line number four my text will display in text file using java program  ... = ""; int lineNo; try { File f=new File("c
servlets
servlets  How to open and read the contents of a text file in servlets?   Please visit the following link: Read text file using Servlet
fetching data using servlets - SQL
for fetching data from a ORACLE10g database using SERVLETS.   Hi Friend...fetching data using servlets  I have successfully made connection with the oracle10g database that i am using and also able to put in data using JSP
Retrieve data from xml using servlets
Retrieve data from xml using servlets  Hi plz send me the code for retrieving the data from xml File using Servlets.   Hi, Do you want.... Thanks Deepak   Hi, Here is one tutorial: Create XML File using Servlet
problem of writing to a local file ( JApplet ) - Applet
problem of writing to a local file ( JApplet )  Dear All, I want to program a guestbook using java applets but now I have problem of writing to a file from JApplet. it is working without any problem if i run the program using
How to read text file in Servlets
How to read text file in Servlets  ... file in servlets. In this example we will use the input stream to read the text... the file InputStream using ServletContext.getResourceAsStream() method. If input
servlets
servlets  why we are using servlets
Writing to and reading from a binary file in java.
Writing to and reading from a binary file in java.  I have written the following code to convert an ASCII text file to a binary file: public static... the binary file from another program as follows: m_dis = new DataInputStream
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my... xml file and storet that in particlar location. Please help me out Thanks
Using Servlets, JSP for Online Shopping
Using Servlets, JSP for Online Shopping  What is wrong with my code ? JSP <form action="LoginServlet "method="post" > <table> <tr ><td>Username:</td><td><input type="text" name="Id
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... stumped when it comes to reading the file and writing to a new file. My code
servlets
what is the architecture of a servlets package  what is the architecture of a servlets package   The javax.servlet package provides interfaces and classes for writing servlets. The Servlet Interface The central
servlets
servlets  How do you communicate between the servlets?   We can communicate between servlets by using RequestDespatcher interface and servlet chaining
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.
Writing a Program to calculate Circumference of planets reading from a file and writing to new file.  Hello, I would like to know how to write... it comes to reading the file and writing to a new file. My code is as follows
how to create a reminder app using threads in Servlets?
how to create a reminder app using threads in Servlets?  I want to make a Reminder application in Servlets. When the date and/or time is matched... get re-directed!". I have used threads for core java, but never used for Servlets
courier management service (using servlets,jsp,jdbc)
courier management service (using servlets,jsp,jdbc)   is anyone having information about courier management service ? plz share it .its urgent THANKING YOU IN ADVANCE
display Helloworld through servlets using jboss
display Helloworld through servlets using jboss  I'm beginner,Can You please Write the code for this.Including WEB.INF   Please visit the following link: http://www.roseindia.net/jboss
Java File Writing Example
Java File Writing Example To write some data on the file you need to first open the file in append mode by using the java.io.FileWriter class and then pass... using bufferedWriter object and finally close it. FileWriter fileWriter
servlets using weblogic server - Java Beginners
servlets using weblogic server  [J2EE:160043]Missing deployment descriptor "WEB-INF/web.xml" at "C:\servlets\rgv.war" im getting this error... the detail procedure to deploy a web application module using weblogic..thnx
Writing Quick Articles Using Information in Public Domain
Writing Quick Articles Using Information in Public Domain... writing articles as a tough task and consequently have a habit of procrastinating... are not interested in researching on topics for writing original articles
without writing web.xml file we can write servlet program
without writing web.xml file we can write servlet program  Sir Morning... Sir I have one Question "without writing web.xml file we can write servlet program". if yes which way? if no why? but without use Annotation........ Plz
using jsp and servlets andin Mvc architecture
using jsp and servlets andin Mvc architecture  how to insert a form data in different tables, that means education details in education table, address details in address table,like that and finally if we fetch all must come
Register page using servlets,jsp and java beans
Register page using servlets,jsp and java beans  i want code for register page using jsp,serlets and java beans.iam getting error for my code in java...://www.roseindia.net/jsp/user-registration-form-using-jsp.shtml Thanks
problem in linking the html page using servlets
problem in linking the html page using servlets  i want to create a login page using html & servlets and as soon as i click the login button... using MS-Access. how should i do this? please help me..m a fresher
online test project on java using servlets and jsp
online test project on java using servlets and jsp  as i am doing online test project on java using jsp and servlets ,,,the problem is in the code of jsp file which i have sent,...,i used next and previous buttons and used two
Building Search Engine Applications Using Servlets !
Building Search Engine Applications Using Servlets Introduction... using Java Servlets. You can Download the source code of search engines and modify it according to requirement. Java Servlets
servlets
of filters. A filter is configured in a web.xml file. The class using... functionality to the servlets apart from processing request and response paradigm..., in the order they appear in web.xml file. A filter also has life cycle
servlets
servlets   Hi what is pre initialized servlets, how can we achives?   When servlet container is loaded, all the servlets defined in the web.xml file does not initialized by default. But the container receives
servlets
are the differ ways you can communicat between servlets   Different ways of communicating between servlets:- 1)Using RequestDispatcher object. 2)Sharing resource using ServletContext object. 3)Include response of the resource
Advertisements
 

 

 

DMCA.com