Monday, October 26, 2009

How will you store your exception stack trace in a file?

ANS:

try
{
printWriter=new PrintWriter(”D:/Exception.txt”);
int i=10,j=0;
int res=i/j;
}
catch(Exception e)
{
StackTraceElement[]str=e.getStackTrace(); printWriter.write(e.getMessage());
printWriter.println(”————-”);
for(int i=0; i < str.length ;i++ )
{ printWriter.write(str[i].toString()); printWriter.write(str[i].getLineNumber()); printWriter.println();
}
printWriter.close();
}

it will generates a file name Exception.txt in D:

how can we display our web pages(jsp,xhtml extc) always from top ?

Using following script we can display our web pages(jsp,xhtml etc.) always from top.

window.scroll(0,0);

Creating BeanFacroty, Application Context and WebApplicationContext

Creating BeanFactory:

ClassPathResource res = new ClassPathResource("beans.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

Creating Application Context:

ClassPathXmlApplicationContext : It Loads context definition from an XML file located in the classpath
ApplicationContext context = new ClassPathXmlApplicationContext("bean_test.xml");

*FileSystemXmlApplicationContext : It loads context definition from an XML file in the filesystem.
ApplicationContext context = new FileSystemXmlApplicationContext("bean_test.xml");


Creating WebApplicationContext;

WebApplicationContext springApplicationContext = WebApplicationContextUtils
.getWebApplicationContext(config.getContext());

Diff b/w Session and SessionFactory

The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application—created during application initialization. The SessionFactory caches generate SQL statements and other mapping
metadata that Hibernate uses at runtime. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work

SessionFactory sessionFactory = configuration.buildSessionFactory();

The Session interface is the primary interface used by Hibernate applications. It is a single-threaded, short-lived object representing a conversation between the application and the persistent store. It allows you to create query objects to
retrieve persistent objects.

Lucene search Example

Creating Index Files;

Document document = new Document();
document.add(Field.Text("author", author));
document.add(Field.Text("title", title));
document.add(Field.Text("topic", topic));
document.add(Field.UnIndexed("url", url));
document.add(Field.Keyword("date", dateWritten));
document.add(Field.UnStored("article", article));
return document;

Analyzer analyzer = new StandardAnalyzer();
IndexWriter writer = new IndexWriter(indexDirectory, analyzer, false);
writer.addDocument(document);
writer.optimize();
writer.close();


Searching string :

String searchCriteria="some artical name";
IndexSearcher is = new IndexSearcher(indexDirectory);
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser = new QueryParser("article", analyzer);
Query query = parser.parse(searchCriteria);
Hits hits = is.search(query);


for (int i=0; i < hits.length(); i++ )
{
Document doc = hits.doc(i);
// display the articles that were found to the user
}
is.close();


for more information you can find Here