Mock tests, Interview questions, Tutorials and Tech news
 
 

Archive

Archive for the ‘Programming / tutorials’ Category

Reading properties file in Spring

June 5th, 2011 Vinay 2 comments

There are multiple ways to read properties file in Spring

Let us say you have database user name , password etc configured in jdbc.properties and would like to inject the values at run time.

Here is what will go in applicationContext.xml

<bean id=”dataSource” destroy-method=”close”>
<property name=”driverClass” value=”${jdbc.driverClass}”/>
<property name=”jdbcUrl” value=”${jdbc.url}”/>
<property name=”user” value=”${jdbc.user}”/>
<property name=”password” value=”${jdbc.password}”/>
<property name=”minPoolSize” value=”${jdbc.minPoolSize}”/>
<property name=”maxPoolSize” value=”${jdbc.maxPoolSize}”/>
</bean>

and this will be your properties file

jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@abc.com:1521:ORAC
jdbc.user=hello
jdbc.password=hello
jdbc.minPoolSize=1
jdbc.maxPoolSize=10

At run time, the properties file value will be injected  into the applicationContext.xml

Eclipse Short Cut Keys- The most useful ones

May 31st, 2011 Vinay No comments

I am summarizing the most useful Eclipse shortcut keys which are really helpful in day to day work

Ctrl + Space : Content Assist

syso + Ctrl + space = writes out System.out.Println for you (I love this one)

Ctrl + Shift + T : Open resource box to look for a Java file

Ctrl + Shift + R: Open resource box to look for a non Java file

Ctrl + F8 : Switch between perspective

Ctrl + F11 : Run the last launched

F11 : Run the last launched in debug mode

Ctrl + Page Up / Ctrl + Page Down : Move between open classes

Alt +Shift +J : Add block comments to a clas sor method

Ctrl + Shift + / : Comment the selected code Read more…

Chrome browser not showing google ads for xhtml file

May 25th, 2011 Vinay No comments

We we migrated from JSF1.0 to  JSF 2.0 ,  we had to convert the files to .After this change, it was found that the  google ads were not  displayed in chrome browser although the same code worked on Firefox and IE.

for eg if you have this code and save it as ,xhtml file , the ads will not be displayed

<html xmlns=”http://www.w3.org/1999/xhtml”>

<body>

<script language=”javascript”>

google_ad_client = “pub-494222175576702985″;

google_ad_slot = “00987428928″;

google_ad_width = 728;

google_ad_height = 90;

</script>

<script language=”javascript”

src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>

</script>

</body>

</html>

</html>

<html xmlns=”http://www.w3.org/1999/xhtml”><body><script language=”javascript”>

google_ad_client = “pub-494222175576702985″; google_ad_slot = “00987428928″;

google_ad_width = 728; google_ad_height = 90;

</script> <script language=”javascript” src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>

</script>

</body>

The reason being that google js file uses document.write, which is not allowed in a application/xhtml+xml page. As per W3C Read more…

Categories: Programming / tutorials Tags: ,

How FlushMode.Never affects the performance ?

May 20th, 2011 Vinay No comments

In hibernate if you set this in you code

session.setFlushMode(FlushMode.NEVER)

What does it signify ?

It means that the Hibernate session not to flush any state changes to database unless flush() is explicitly called by the application.

When setting the FlushMode.NEVER , will it improve the performance and reduce the run time  ?

It would not affect if you are running a single update or a series of small updates. It really affects you when you are doing batch processing. The reason for this is the way hibernate implements dirty checking. Once you load an object in memory and do not evict it, the hibernate session keeps track of it in case if it was changed. So, any time you perform a query, the session iterates over all objects in the session checking dirtiness and flushes any dirty objects to the database. It does this to ensure that all state changes that might affect the query are present in the database before issuing the query to it. That’s fine when you have only a few objects in session, but when you have thousands and are performing thousands of queries, it becomes a real drain on performance.

Tim fennel has made this interesting observation about the FlushMode.NEVER in his blog here.

Spring provides a similar way to annotate methods @Transactional(readOnly=true) in Spring which internally has the same affect.

Character data type conversion when using SQL Server JDBC drivers

May 3rd, 2011 sadhna No comments

We have a existing old portal in asp with Sql server and now trying to get new features in new site developing in grails,java & same sql server.

Sometime ago, I was working on a login page so that the same old portal credentials could be used.   One issue I had to address was that the web app captures the password in clear text but sends an encoded password to the database (where the password is also stored in our encoded format).

Now I was trying to implement a simple logic for encryption; add 20 to each character’s int value and convert it to char and store the encrypted password.

For example the java code-

StringBuffer t = new StringBuffer(“”);

String test = “sviadha”;for ( int i = 0; i < test.length(); ++i ) {

char c = test.charAt( i );

int k = (int) c ;

int kk = k +20;

char ss = (char)kk;

t = t.append(ss);

}

System.out.println(“original password =”+ test);

System.out.println(“encrypted password ==”+ t);

Now when I tried to match the stored password using new UI it was failed all the time.

No matter what I tried, in the Java-tier I could not get past the fact that by the time the password was received in the SQL-tier, there was an encoded password mismatch.   So I worked around the problem, but passing the clear text password to the database and the stored procedure did the encoding and finally the validation.

After digging it found the  real problem; it turns out that this is a MS SQL Server JDBC driver configuration.  By default MS JDBC driver is sent to pass all strings as NVARCHAR, not VARCHAR.  This forced a Unicode conversion on the way to the database.

That’s why the same encoding logic was working fine for asp-sql server based old site but not on java based new site.

Here’s the magic to change this behavior so VARCHAR are sent and received…

MyDataSource

jdbc:jtds:sqlserver://mac1.temp.test:1434;DatabaseName=MyDatabase;tds=8.0;lastupdatecount=false; sendStringParametersAsUnicode=false

net.sourceforge.jtds.jdbc.Driver

. . .
Read more…

Spring JdbcTemplate batch Update-Insert example

April 12th, 2011 Vinay No comments

This post explain how to use Spring JDBCTemplate to do batch insert and update.

If you are using SimpleJDBCtemplate then the Spirng docs are good place to find a good explanation for it Simple JDBC Template

If you are using JDBCtemplate , here is the example to use prepared statements and do batch updates

public boolean createGroceyList(List<Grocery> groceryList){

final List<Grocery> aGroceryList =  groceryList

int result[] = this.jdbcTemplate.batchUpdate(

“the query goes here “,new BatchPreparedStatementSetter() {

public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setLong(1, “param 1 goes here “);
ps.setString(2, “param 2 goes here “);

ps.setString(3, “param 3 goes here “);
}

public int getBatchSize() {
return aGroceryList.size();
}
});

return (aGroceryList.size() == result.length ? true : false);
}

The query would be something similar to

INSERT INTO GROCERY(ID, NAME, QUANTITY) VALUES(?,?,?)

Categories: Programming / tutorials Tags: ,

Spring 3 MVC – Using mvc:interceptors

March 30th, 2011 Vinay No comments

One of the new feature in Spring 3 MVC  has been introduction of  mvc  XML configuration namespace that simplifies the setup of Spring MVC inside the web application.

Spring 3 allows you to intercept the request and response headers without writing the filter explicitly .  Spring’s handler mapping mechanism includes handler interceptors.

If you would prefer to write a filter explicitly, it has been covered in Servlet Filter to prevent No Cache.

From Spring MVC docs

Interceptors located in the handler mapping must implement HandlerInterceptor from the org.springframework.web.servlet package. This interface defines three methods: one is called before the actual handler is executed; one is called after the handler is executed; and one is called after the complete request has finished. These three methods should provide enough flexibility to do all kinds of preprocessing and postprocessing

You have an option in which instead of writing your own custom class, you can modify the servlet-Context.xml file to provide no caching feature

Here is what will go inside the file

<mvc:annotation-driven/>
<mvc:interceptors>
<bean id=”webContentInterceptor”>
<property name=”cacheSeconds” value=”0″/>
<property name=”useExpiresHeader” value=”true”/>
<property name=”useCacheControlHeader” value=”true”/>
<property name=”useCacheControlNoStore” value=”true”/>
</bean>
</mvc:interceptors> Read more…

Servlet Filter to prevent No-Cache

March 27th, 2011 Vinay No comments

In one of the posts, I had talked about IE caching AJAX calls. To solve this issue , you would have to modify response such that it informs browser not to cache the data coming from this server.

I am explaining it in plain old fashioned way of ServletFilter.

Below is the code for ServletFilter.

public class NoCacheFilter implements Filter {

private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

public void destroy() {
this.filterConfig = null;
}

/**
* Modifies the response. Sets the appropriate headers
* and invoke the next filter in the chain:
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

HttpServletResponse httpResponse = (HttpServletResponse) response;

httpResponse.setHeader(“Cache-Control”, “No-cache”);
httpResponse.setDateHeader(“Expires”, 0);
httpResponse.setHeader(“Pragma”, “No-cache”);
chain.doFilter(request, response);

}
}

This is helpful for browsers like IE which Cache your data by default and do not display fresh data. This is one of the situations when you are working with JSON REST web services in which you might to pull and display fresh data.

Categories: Programming / tutorials Tags: , ,
Get Adobe Flash playerPlugin by wpburn.com wordpress themes