Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

converting java code to servlet

 
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...
 
Ranch Hand
Posts: 689
Scala Eclipse IDE Java
  • 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.
 
Rancher
Posts: 43016
76
  • 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?
 
joke time: What is brown and sticky? ... ... ... A stick! Use it to beat this tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic