aspose file tools*
The moose likes Servlets and the fly likes converting java code to servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "converting java code to servlet" Watch "converting java code to servlet" New topic
Author

converting java code to servlet

Deepthi Xavy
Ranch Hand

Joined: Aug 31, 2009
Posts: 32


import java.io.*;
import java.sql.*;

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*;

public class FlatFileXml {

BufferedReader br;
StreamResult sr;

TransformerHandler tfh;
AttributesImpl ai;

public static void main(String args[]) throws Exception{
try
{
new FlatFileXml().datamain();
System.out.println("You are Successfully create XML and data insert into the database.");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}

}

public void datamain () {
try {
br = new BufferedReader(new FileReader("flatfile.txt"));
sr = new StreamResult("flatfile.xml");
xmlMain();
String str;
while ((str = br.readLine()) != null) {
doWork(str);
}
br.close();
xmlEnd();
}
catch (Exception e) { e.printStackTrace(); }
}


public void xmlMain() throws Exception {
SAXTransformerFactory tf = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();

tfh = tf.newTransformerHandler();
Transformer serTf = tfh.getTransformer();
serTf.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
tfh.setResult(sr);
tfh.startDocument();
ai = new AttributesImpl();
}

public void doWork (String s) throws Exception{

String [] ar = s.split("\\-");
try{
Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection(url+dbName,userName,password);
String queryString = "INSERT INTO flatfile ( id,name,address,contact_no) values('"+ar[0]+"','"+ar[1]+"','"+ar[2]+"','"+ar[3]+"')";

stmt=con.createStatement();
val = stmt.executeUpdate(queryString);

}
catch(Exception e)
{
System.out.println("e : " + e);
}
ai.clear();
tfh.startElement("","","ID",ai);
tfh.characters(ar[0].toCharArray(),0,ar[0].length());
tfh.endElement("","","ID");

}

public void xmlEnd() throws Exception{
tfh.endDocument(); }
}




I got the following code, how to convert this code to servlet...
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi Deepthi,

UseCodeTags ..Code is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
What does it mean to convert this application to a servlet? Where are the inputs coming from? Where should the outputs go?


Ping & DNS - updated with new look and Ping home screen widget
 
 
subject: converting java code to servlet
 
Similar Threads
Read/Writer Thread problem in Socket
getting error while running small program
plz help me (Socket problem)
full duplex chatty in Java (CODE PASTED)
How to convert a text file into an xml?