Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting java code to servlet

 
Deepthi Xavy
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does it mean to convert this application to a servlet? Where are the inputs coming from? Where should the outputs go?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic