<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Technical Blog Of JackCHAN</title>
	<atom:link href="http://kaisechen.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kaisechen.wordpress.com</link>
	<description>The technical blog of jackCHAN</description>
	<lastBuildDate>Sat, 12 Mar 2011 03:31:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kaisechen.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Technical Blog Of JackCHAN</title>
		<link>http://kaisechen.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kaisechen.wordpress.com/osd.xml" title="Technical Blog Of JackCHAN" />
	<atom:link rel='hub' href='http://kaisechen.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Servlet Interview Question and Answer</title>
		<link>http://kaisechen.wordpress.com/2010/08/29/servlet-interview-question-and-answer/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/29/servlet-interview-question-and-answer/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 03:11:05 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Jsp/Servlet]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=364</guid>
		<description><![CDATA[Q:Explain the life cycle methods of a Servlet. A: The javax.servlet.Servlet interface defines the three methods known as life-cycle method. public void init(ServletConfig config) throws ServletException public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy() First the servlet is constructed, then initialized wih the init() method. Any request from client are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=364&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Q:Explain the life cycle methods of a Servlet.</strong><br />
<strong>A:</strong><br />
The javax.servlet.Servlet interface defines the three methods known as life-cycle method.<br />
public void <strong>init</strong>(ServletConfig config) throws ServletException<br />
public void <strong>service</strong>( ServletRequest req, ServletResponse res) throws ServletException, IOException<br />
public void <strong>destroy</strong>()<br />
First the servlet is constructed, then initialized wih the init() method.<br />
Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet.</p>
<p>The servlet is removed from service, destroyed with the destroy() methid, then garbaged collected and finalized.</p>
<p><strong>Q:What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?<br />
A:</strong><br />
The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a &#8220;/&#8221; it is interpreted as relative to the current context root.</p>
<p>The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths. All path must sart with a &#8220;/&#8221; and are interpreted as relative to curent context root.</p>
<p><strong>Q:Explain the directory structure of a web application.<br />
A:</strong><br />
The directory structure of a web application consists of two parts.<br />
A private directory called WEB-INF<br />
A public resource directory which contains public resource folder.</p>
<p>WEB-INF folder consists of<br />
1. web.xml<br />
2. classes directory<br />
3. lib directory</p>
<p><strong>Q:What are the common mechanisms used for session tracking?<br />
A:</strong><br />
Cookies<br />
SSL sessions<br />
URL- rewriting</p>
<p><strong>Q: Explain ServletContext.<br />
A: </strong><br />
ServletContext interface is a window for a servlet to view it&#8217;s environment. A servlet can use this interface to get information such as initialization parameters for the web applicationor servlet container&#8217;s version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.</p>
<p><strong>Q: What is preinitialization of a servlet?<br />
A: </strong><br />
A container does not initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the &lt;load-on-startup&gt; element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.</p>
<p><strong>Q: What is the difference between Difference between doGet() and doPost()?<br />
A: </strong><br />
A doGet() method is limited with 2k of data to be sent, and doPost() method doesn&#8217;t have this limitation. A request string for doGet() looks like the following:</p>
<p>http://www.allapplabs.com/svt1?p1=v1&#038;p2=v2&#038;&#8230;&#038;pN=vN</p>
<p>doPost() method call doesn&#8217;t need a long text tail after a servlet name in a request. All parameters are stored in a request itself, not in a request string, and it&#8217;s impossible to guess the data transmitted to a servlet only looking at a request string.</p>
<p><strong>Q: What is the difference between HttpServlet and GenericServlet?<br />
A: </strong><br />
A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).<br />
Both these classes are abstract.</p>
<p><strong>Q: What is the difference between ServletContext and ServletConfig?<br />
A: </strong><br />
ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized</p>
<p>ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet.</p>
<p>Every servlet has the same ServletContext but it also has its own ServletConfig.</p>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/servlet/'>servlet</a>, <a href='http://kaisechen.wordpress.com/tag/web/'>web</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/364/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/364/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/364/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=364&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/29/servlet-interview-question-and-answer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>JSP Interview Question and Answer</title>
		<link>http://kaisechen.wordpress.com/2010/08/29/356/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/29/356/#comments</comments>
		<pubDate>Sun, 29 Aug 2010 02:54:25 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Jsp/Servlet]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=356</guid>
		<description><![CDATA[1. What is a JSP and what is it used for? Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=356&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>1. What is a JSP and what is it used for? </strong><br />
Java Server Pages (JSP) is a platform independent presentation layer technology that comes with SUN s J2EE platform. JSPs are normal HTML pages with Java code pieces embedded in them. JSP pages are saved to *.jsp files. A JSP compiler is used in the background to generate a Servlet from the JSP page.</p>
<p><strong>2. What is difference between custom JSP tags and beans? </strong><br />
Custom JSP tag is a tag you defined. You define how a tag, its attributes and its body are interpreted, and then group your tags into collections called tag libraries that can be used in any number of JSP files. To use custom JSP tags, you need to define three separate components:</p>
<ol>
<li>The tag handler class that defines the tag\&#8217;s behavior</li>
<li>The tag library descriptor file that maps the XML element names to the tag implementations</li>
<li> the JSP file that uses the tag library<br />
When the first two components are done, you can use the tag by using taglib directive:</li>
</ol>
<p>Then you are ready to use the tags you defined. Let&#8217;s say the tag prefix is test:<br />
MyJSPTag or<br />
JavaBeans are Java utility classes you defined. Beans have a standard format for Java classes. You use tags<br />
to declare a bean and use<br />
to set value of the bean class and use<br />
to get value of the bean class.</p>
<p>Custom tags and beans accomplish the same goals — encapsulating complex behavior into simple and accessible forms. There are several differences:<br />
Custom tags can manipulate JSP content; beans cannot.<br />
Complex operations can be reduced to a significantly simpler form with custom tags than with beans. Custom tags require quite a bit more work to set up than do beans.<br />
Custom tags usually define relatively self-contained behavior, whereas beans are often defined in one servlet and used in a different servlet or JSP page.<br />
Custom tags are available only in JSP 1.1 and later, but beans can be used in all JSP 1.x versions.</p>
<p><strong>3. What are the two kinds of comments in JSP and what&#8217;s the difference between them. </strong><br />
&lt;%– JSP Comment –%&gt;<br />
&lt;!– HTML Comment –&gt;</p>
<p><strong> 4. What is JSP technology? </strong></p>
<p>Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.</p>
<p><strong> 5. What is JSP page? </strong></p>
<p>A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format such as HTML, SVG, WML, and XML, and JSP elements, which construct dynamic content.</p>
<p><strong> 6. What are the implicit objects? </strong></p>
<p>Implicit objects are objects that are created by the web container and contain information related to a particular request, page, or application. They are:<br />
–request<br />
–response<br />
–pageContext<br />
–session<br />
–application<br />
–out<br />
–config<br />
–page<br />
–exception</p>
<p><strong> 7. How many JSP scripting elements and what are they? </strong></p>
<p>There are three scripting language elements:<br />
–declarations<br />
–scriptlets<br />
–expressions</p>
<p><strong> 8. Why are JSP pages the preferred API for creating a web-based client program? </strong></p>
<p>Because no plug-ins or security policy files are needed on the client systems(applet does). Also, JSP pages enable cleaner and more module application design because they provide a way to separate applications programming from web page design. This means personnel involved in web page design do not need to understand Java programming language syntax to do their jobs.</p>
<p><strong> 9. Is JSP technology extensible? </strong></p>
<p>YES. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.</p>
<p><strong> 10. Can we use the constructor, instead of init(), to initialize servlet? </strong></p>
<p>Yes , of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.</p>
<p><strong> 11. How can a servlet refresh automatically if some new data has entered the database? </strong></p>
<p>You can use a client-side Refresh or Server Push.</p>
<p><strong> 12. The code in a finally clause will never fail to execute, right? </strong></p>
<p>Using System.exit(1); in try block will not allow finally code to execute.</p>
<p><strong> 13. How many messaging models do JMS provide for and what are they? </strong></p>
<p>JMS provide for two messaging models, publish-and-subscribe and point-to-point queuing.</p>
<p><strong> 14. What information is needed to create a TCP Socket? </strong></p>
<p>The Local Systems IP Address and Port Number. And the Remote System’s IPAddress and Port Number.</p>
<p><strong> 15. What Class.forName will do while loading drivers? </strong></p>
<p>It is used to create an instance of a driver and register it with the DriverManager. When you have loaded a driver, it is available for making a connection with a DBMS.</p>
<p><strong> 16. How to Retrieve Warnings? </strong></p>
<p>SQLWarning objects are a subclass of SQLException that deal with database access warnings. Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. A warning can be reported on a Connection object, a Statement object (including PreparedStatement and CallableStatement objects), or a ResultSet object. Each of these classes has a getWarnings method, which you must invoke in order to see the first warning reported on the calling object</p>
<div>SQLWarning warning = stmt.getWarnings();<br />
if (warning != null)<br />
{<br />
while (warning != null)<br />
{<br />
System.out.println(&#8220;Message: &#8221; +     warning.getMessage());<br />
System.out.println(&#8220;SQLState: &#8221; +     warning.getSQLState());<br />
System.out.print(&#8220;Vendor error code: &#8220;); 			    System.out.println(warning.getErrorCode());<br />
warning = warning.getNextWarning();<br />
}<br />
}</div>
<p><strong>17. How many JSP scripting elements are there and what are they? </strong></p>
<p>There are three scripting language elements: declarations, scriptlets, expressions.</p>
<p><strong> 18. In the Servlet 2.4 specification SingleThreadModel has been deprecated, why? </strong></p>
<p>Because it is not practical to have such model. Whether you set isThreadSafe to true or false, you should take care of concurrent client requests to the JSP page by synchronizing access to any shared objects defined at the page level.</p>
<p><strong> 19. What are stored procedures? How is it useful? </strong></p>
<p>A stored procedure is a set of statements/commands which reside in the database. The stored procedure is pre-compiled and saves the database the effort of parsing and compiling sql statements everytime a query is run. Each database has its own stored procedure language, usually a variant of C with a SQL preproceesor. Newer versions of db’s support writing stored procedures in Java and Perl too. Before the advent of 3-tier/n-tier architecture it was pretty common for stored procs to implement the business logic( A lot of systems still do it). The biggest advantage is of course speed. Also certain kind of data manipulations are not achieved in SQL. Stored procs provide a mechanism to do these manipulations. Stored procs are also useful when you want to do Batch updates/exports/houseKeeping kind of stuff on the db. The overhead of a JDBC Connection may be significant in these cases.</p>
<p><strong> 20. How do I include static files within a JSP page? </strong></p>
<p>Static resources should always be included using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should always supply a relative URL for the file attribute. Although you can also include static resources using the action, this is not advisable as the inclusion is then performed for each and every request.</p>
<p><strong>21. Why does JComponent have add() and remove() methods but Component does not? </strong></p>
<p>because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe JSP page? &#8211; You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive &lt;%@ page isThreadSafe=&#8221;false&#8221; % &gt; within your JSP page.</p>
<p><strong> 22. How can I enable session tracking for JSP pages if the browser has disabled cookies? </strong></p>
<p>We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair.<br />
However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input.</p>
<p>Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie. Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other.</p>
<p>Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page.Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object. Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages.</p>
<p>Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting.<br />
hello1.jsp<br />
hello2.jsp<br />
hello2.jsp<br />
&lt;%<br />
Integer i= (Integer )session.getValue(&#8220;num&#8221;);<br />
out.println(&#8220;Num value in session is &#8220;+i.intValue());</p>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/jsp/'>jsp</a>, <a href='http://kaisechen.wordpress.com/tag/servlet/'>servlet</a>, <a href='http://kaisechen.wordpress.com/tag/web/'>web</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/356/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/356/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/356/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=356&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/29/356/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>Hibernate Interview questions (3)</title>
		<link>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-3/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-3/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 02:13:31 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=348</guid>
		<description><![CDATA[31.What is the advantage of Hibernate over jdbc? Hibernate Vs. JDBC :- JDBC Hibernate With JDBC, developer has to write code to map an object model&#8217;s data representation to a relational data model and its corresponding database schema. Hibernate is flexible and powerful ORM solution to map Java classes to database tables. Hibernate itself takes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=348&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>31.What is the advantage of Hibernate over jdbc?</strong></p>
<p>Hibernate Vs. JDBC :-</p>
<table cellspacing="0" cellpadding="6">
<tbody>
<tr>
<th scope="col">JDBC</th>
<th scope="col">Hibernate</th>
</tr>
<tr>
<td width="50%">With JDBC, developer has to write code to map an object model&#8217;s data representation to a relational data model and its corresponding database schema.</td>
<td>Hibernate is flexible and powerful ORM solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this.</td>
</tr>
<tr>
<td>With JDBC, the automatic mapping of Java objects with database tables and vice versa conversion is to be taken care of by the developer manually with lines of code.</td>
<td>Hibernate provides transparent persistence and developer does not need to write code explicitly to map database tables tuples to application objects during interaction with RDBMS.</td>
</tr>
<tr>
<td>JDBC supports only native Structured Query Language (SQL). Developer has to find out the efficient way to access database, i.e. to select effective query from a number of queries to perform same task.</td>
<td>Hibernate provides a powerful query language Hibernate Query Language (independent from type of database) that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application.</td>
</tr>
<tr>
<td>Application using JDBC to handle persistent data (database tables) having database specific code in large amount. The code written to map table data to application objects and vice versa is actually to map table fields to object properties. As table changed or database changed then it’s essential to change object structure as well as to change code written to map table-to-object/object-to-table.</td>
<td>Hibernate provides this mapping itself. The actual mapping between tables and application objects is done in XML files. If there is change in Database or in any table then the only need to change XML file properties.</td>
</tr>
<tr>
<td>With JDBC, it is developer’s responsibility to handle JDBC result set and convert it to Java objects through code to use this persistent data in application. So with JDBC, mapping between Java objects and database tables is done manually.</td>
<td>Hibernate reduces lines of code by maintaining object-table mapping itself and returns result to application in form of Java objects. It relieves programmer from manual handling of persistent data, hence reducing the development time and maintenance cost.</td>
</tr>
<tr>
<td>With JDBC, caching is maintained by hand-coding.</td>
<td>Hibernate, with Transparent Persistence, cache is set to application work space. Relational tuples are moved to this cache as a result of query. It improves performance if client application reads same data many times for same write. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code.</td>
</tr>
<tr>
<td>In JDBC there is no check that always every user has updated data. This check has to be added by the developer.</td>
<td>Hibernate enables developer to define version type field to application, due to this defined field Hibernate updates version field of database table every time relational tuple is updated in form of Java class object to that table. So if two users retrieve same tuple and then modify it and one user save this modified tuple to database, version is automatically updated for this tuple by Hibernate. When other user tries to save updated tuple to database then it does not allow saving it because this user does not have updated data.</td>
</tr>
</tbody>
</table>
<p><strong>32.What are the Collection types in Hibernate ?</strong></p>
<ul>
<li>Bag</li>
<li>Set</li>
<li>List</li>
<li>Array</li>
<li>Map</li>
</ul>
<p><strong>33.What are the ways to express joins in HQL?</strong></p>
<p>HQL provides four ways of  expressing (inner and outer) joins:-</p>
<ul>
<li>An <em>implicit</em> association join</li>
<li>An ordinary join in the FROM  clause</li>
<li>A fetch join in the FROM clause.</li>
<li>A <em>theta-style</em> join in the WHERE clause.</li>
</ul>
<p><strong>34.Define cascade and inverse option in one-many mapping?</strong></p>
<p>cascade &#8211;  enable operations to cascade to child entities.<br />
cascade=&#8221;all|none|save-update|delete|all-delete-orphan&#8221;</p>
<p>inverse &#8211; mark this collection as the &#8220;inverse&#8221; end of a bidirectional association.<br />
inverse=&#8221;true|false&#8221;<br />
Essentially &#8220;inverse&#8221; indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?<br />
<strong>35.What is Hibernate proxy?</strong></p>
<p>The <code>proxy</code> attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked.</p>
<p><strong>36.How can Hibernate be configured to access an instance variable directly and not through a setter method ?</strong></p>
<p>By mapping the property with access=&#8221;field&#8221; in Hibernate metadata. This forces hibernate to bypass the setter method and access the instance variable directly while initializing a newly loaded object.<br />
<strong>37.How can a whole class be mapped as immutable?</strong></p>
<p>Mark the class as mutable=&#8221;false&#8221; (Default is true),. This specifies that instances of the class are (not) mutable. Immutable classes, may not be updated or deleted by the application.<br />
<strong>38.What is the use of dynamic-insert and dynamic-update attributes in a class mapping?</strong></p>
<p>Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like &#8220;search&#8221; screens where there is a variable number of conditions to be placed upon the result set.</p>
<ul>
<li><code>dynamic-update</code> (defaults to <code>false</code>): Specifies that <code>UPDATE</code> SQL should be generated at runtime and contain only those columns whose values have changed</li>
<li><code>dynamic-insert</code> (defaults to <code>false</code>): Specifies that <code>INSERT</code> SQL should be generated at runtime and contain only the columns whose values are not null.</li>
</ul>
<p><strong>39.What do you mean by fetching strategy ?</strong></p>
<p>A <em>fetching strategy</em> is the strategy Hibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or over-ridden by a particular HQL or <code>Criteria</code> query.</p>
<p><strong>40.What is automatic dirty checking?</strong></p>
<p>Automatic dirty checking is a feature that saves us the effort of explicitly asking Hibernate to update the database when we modify the state of an object inside a transaction.<br />
<strong>41.What is transactional write-behind?</strong></p>
<p>Hibernate uses a sophisticated algorithm to determine an efficient ordering that avoids database foreign key constraint violations but is still sufficiently predictable to the user. This feature is called transactional write-behind.</p>
<p><strong>42.What are Callback interfaces?</strong></p>
<p>Callback interfaces allow the application to receive a notification when something interesting happens to an object—for example, when an object is loaded, saved, or deleted. Hibernate applications don&#8217;t need to implement these callbacks, but they&#8217;re useful for implementing certain kinds of generic functionality.<br />
<strong>43.What are the types of Hibernate instance states ?</strong></p>
<p>Three types of instance states:</p>
<ul>
<li>Transient -The instance is not associated with any persistence context</li>
<li>Persistent -The instance is associated with a persistence context</li>
<li>Detached -The instance was associated with a persistence context which has been closed – currently not associated</li>
</ul>
<p><strong>44.What are the differences between EJB 3.0 &amp; Hibernate</strong></p>
<p>Hibernate Vs EJB 3.0 :-</p>
<table cellspacing="0" cellpadding="6">
<tbody>
<tr>
<th scope="col">Hibernate</th>
<th scope="col">EJB 3.0</th>
</tr>
<tr>
<td><strong>Session</strong>–Cache or collection of loaded objects relating to a single unit of work</td>
<td><strong>Persistence Context</strong>-Set of entities that can be managed by a given EntityManager is defined by a persistence unit</td>
</tr>
<tr>
<td><strong>XDoclet Annotations</strong> used to support Attribute Oriented Programming</td>
<td><strong>Java 5.0 Annotations</strong> used to support Attribute Oriented Programming</td>
</tr>
<tr>
<td><strong>Defines HQL</strong> for expressing queries to the database</td>
<td><strong>Defines EJB QL</strong> for expressing queries</td>
</tr>
<tr>
<td><strong>Supports Entity Relationships</strong> through mapping files and annotations in JavaDoc</td>
<td><strong>Support Entity Relationships</strong> through Java 5.0 annotations</td>
</tr>
<tr>
<td><strong>Provides a Persistence Manager API</strong> exposed via the Session, Query, Criteria, and Transaction API</td>
<td><strong>Provides and Entity Manager Interface</strong> for managing CRUD operations for an Entity</td>
</tr>
<tr>
<td><strong>Provides callback support</strong> through lifecycle, interceptor, and validatable interfaces</td>
<td><strong>Provides callback support</strong> through Entity Listener and Callback methods</td>
</tr>
<tr>
<td><strong>Entity Relationships are unidirectional</strong>. Bidirectional relationships are implemented by two unidirectional <strong>relationships</strong></td>
<td><strong>Entity Relationships are bidirectional or unidirectional</strong></td>
</tr>
</tbody>
</table>
<p><strong>45.What are the types of inheritance models  in Hibernate?</strong></p>
<p>There are three types of inheritance models in Hibernate:</p>
<ul>
<li>Table per class hierarchy</li>
<li>Table per subclass</li>
<li>Table per concrete class</li>
</ul>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/hibernate/'>hibernate</a>, <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/orm/'>orm</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/348/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/348/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/348/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=348&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>Hibernate Interview questions (2)</title>
		<link>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-2/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-2/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 02:12:29 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=346</guid>
		<description><![CDATA[16.What’s the difference between load() and get()? load() vs. get() :- load() get() Only use the load() method if you are sure that the object exists. If you are not sure that the object exists, then use one of the get() methods. load() method will throw an exception if the unique id is not found [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=346&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>16.What’s the difference between load() and get()? </strong></p>
<p>load() vs. get() :-</p>
<table cellspacing="0" cellpadding="6">
<tbody>
<tr>
<th scope="col">load()</th>
<th scope="col">get()</th>
</tr>
<tr>
<td>Only use the <code>load()</code> method if you are sure that the object exists.</td>
<td>If you are not sure that the object exists, then use one of the <code>get()</code> methods.</td>
</tr>
<tr>
<td><code>load()</code> method will throw an exception if the unique id is not found in the database.</td>
<td><code>get()</code> method will return null if the unique id is not found in the database.</td>
</tr>
<tr>
<td><code>load()</code> just returns a proxy by default and database won’t be hit until the proxy is first invoked.</td>
<td><code>get()</code> will hit the database immediately.</td>
</tr>
</tbody>
</table>
<p><strong>17.What is the difference between and merge and update ? </strong></p>
<p>Use <code>update()</code> if you are sure that the session does not contain an already persistent instance with the same identifier, and <code>merge()</code> if you want to merge your modifications at any time without consideration of the state of the session.<br />
<strong>18.How do you define sequence generated primary key in hibernate? </strong></p>
<p>Using &lt;generator&gt; tag.<br />
<strong>Example</strong>:-</p>
<pre><span style="color:#008000;">&lt;id</span> <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"USER_ID"</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"id"</span> <span style="color:#800040;">type</span>=<span style="color:#0000ff;">"java.lang.Long"</span><span style="color:#008000;">&gt; </span>
<strong> </strong>   <span style="color:#008000;">&lt;generator</span> <span style="color:#800040;">class</span>=<span style="color:#0000ff;">"sequence"</span><span style="color:#008000;">&gt; </span>
     <span style="color:#008000;">&lt;param</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"table"</span>&gt;SEQUENCE_NAME<span style="color:#008000;">&lt;/param&gt;</span>
<strong> </strong> <span style="color:#008000;"> &lt;generator&gt;</span>
<span style="color:#008000;">&lt;/id&gt;</span></pre>
<table cellspacing="0" cellpadding="0" width="336" align="right" bgcolor="#fafafa">
<tbody>
<tr>
<td>//<br />
// <ins><ins></ins></ins></td>
</tr>
</tbody>
</table>
<p><strong>19.Define cascade and inverse option in one-many mapping? </strong></p>
<p>cascade &#8211;  enable operations to cascade to child entities.<br />
cascade=&#8221;all|none|save-update|delete|all-delete-orphan&#8221;</p>
<p>inverse &#8211; mark this collection as the &#8220;inverse&#8221; end of a bidirectional association.<br />
inverse=&#8221;true|false&#8221;<br />
Essentially &#8220;inverse&#8221; indicates which end of a relationship should be ignored, so when persisting a parent who has a collection of children, should you ask the parent for its list of children, or ask the children who the parents are?<br />
<strong>20.What do you mean by Named – SQL query? </strong></p>
<p>Named SQL queries are defined in the mapping xml document and called wherever required.<br />
<strong>Example:</strong></p>
<pre><span style="color:#008000;">&lt;sql-query </span><span style="color:#800040;">name</span> =<span style="color:#0000ff;"> "empdetails"</span>&gt;
   <span style="color:#008000;">&lt;return</span> <span style="color:#800040;">alias</span>=<span style="color:#0000ff;">"emp"</span> <span style="color:#800040;">class</span>=<span style="color:#0000ff;">"com.test.Employee"</span><span style="color:#008000;">/&gt;</span>
<span style="color:#ff00ff;">      SELECT emp.EMP_ID AS {emp.empid},
                 emp.EMP_ADDRESS AS {emp.address},
                 emp.EMP_NAME AS {emp.name}
      FROM Employee EMP WHERE emp.NAME LIKE :name</span>
<span style="color:#008000;">&lt;/sql-query&gt;</span></pre>
<p>Invoke Named Query :</p>
<pre><span style="color:#800040;">List </span>people = <span style="color:#800040;">session</span>.getNamedQuery(<span style="color:#0000ff;">"empdetails"</span>)
		     .setString(<span style="color:#0000ff;">"TomBrady"</span>, name)
		     .setMaxResults(50)
		     .list();</pre>
<p><strong>21.How do you invoke Stored Procedures?</strong></p>
<pre><span style="color:#008000;">&lt;sql-query</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"selectAllEmployees_SP"</span> <span style="color:#800040;">callable</span>=<span style="color:#0000ff;">"true"</span><span style="color:#008000;">&gt;</span>
<span style="color:#008000;"> &lt;return</span> <span style="color:#800040;">alias</span>=<span style="color:#0000ff;">"emp"</span> <span style="color:#800040;">class</span>=<span style="color:#0000ff;">"employee"</span><span style="color:#008000;">&gt;</span>
   <span style="color:#008000;">&lt;return-property</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"empid"</span> <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"EMP_ID"</span><span style="color:#008000;">/&gt;</span>       

  <span style="color:#800040;"> <span style="color:#008000;">&lt;return-property</span></span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"name"</span> <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"EMP_NAME"</span><span style="color:#008000;">/&gt;</span><span style="color:#008000;"> </span>      
  <span style="color:#008000;"> &lt;return-property</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"address"</span> <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"EMP_ADDRESS"</span><span style="color:#008000;">/&gt;</span>
    { <span style="color:#000080;">? = call selectAllEmployees()</span> }
<span style="color:#008000;"> &lt;/return&gt;</span>
<span style="color:#008000;">&lt;/sql-query&gt;</span>
</pre>
<p><strong>22.Explain Criteria API </strong></p>
<p>Criteria is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like &#8220;search&#8221; screens where there is a variable number of conditions to be placed upon the result set.<br />
<strong>Example</strong> :</p>
<pre><span style="color:#800040;">List </span>employees = <span style="color:#800040;">session</span>.createCriteria(Employee.class)
		         .add(Restrictions.like(<span style="color:#0000ff;">"name"</span>, <span style="color:#0000ff;">"a%"</span>) )
		         .add(Restrictions.like(<span style="color:#0000ff;">"address"</span>, <span style="color:#0000a0;">"Boston"</span>))
			 .addOrder(Order.asc(<span style="color:#0000ff;">"name"</span>) )
			 .list();</pre>
<p><strong>23.Define HibernateTemplate? </strong></p>
<p><code>org.springframework.orm.hibernate.HibernateTemplate</code> is a helper class which provides different methods for querying/retrieving data from the database. It also converts checked HibernateExceptions into unchecked DataAccessExceptions.<br />
<strong>24.What are the benefits does HibernateTemplate provide? </strong></p>
<p>The benefits of HibernateTemplate are :</p>
<ul>
<li><code>HibernateTemplate</code>, a Spring Template class simplifies interactions with Hibernate Session.</li>
<li>Common functions are simplified to single method calls.</li>
<li>Sessions are automatically closed.</li>
<li>Exceptions are automatically caught and converted to runtime exceptions.</li>
</ul>
<table cellspacing="0" cellpadding="0" width="336" align="left" bgcolor="#fafafa">
<tbody>
<tr>
<td>//<br />
// <ins><ins></ins></ins></td>
</tr>
</tbody>
</table>
<p><strong>25.How do you switch between relational databases without code changes? </strong></p>
<p>Using Hibernate SQL Dialects , we can switch databases. Hibernate will generate appropriate hql queries based on the dialect defined.<br />
<strong>26.If you want to see the Hibernate generated SQL statements on console, what should we do? </strong></p>
<p>In Hibernate configuration file  set as follows:<br />
<code><span><span style="color:#008000;">&lt;property</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"show_sql"</span>&gt;true<span style="color:#008000;">&lt;/property&gt;</span></span></code><br />
<strong>27.What are derived properties? </strong></p>
<p>The properties that are not mapped to a column, but calculated at runtime by evaluation of an expression are called derived properties. The expression can be defined using the formula attribute of the element.</p>
<div>
<p><strong>28.What is component mapping in Hibernate? </strong></p>
<ul>
<li>A component is an object saved as a value, not as a reference</li>
<li> A component can be saved directly without needing to declare interfaces or identifier properties</li>
<li> Required to define an empty constructor</li>
<li> Shared references not supported</li>
</ul>
<p><strong>Example</strong>:</p>
<p><img src="http://www.developersbook.com/hibernate/images/component-mapping.png" border="0" alt="Component Mapping" width="642" height="415" /></p>
<p><strong>29.What is the difference between sorted and ordered collection in hibernate? </strong><strong>sorted collection vs. order collection</strong> :-</p>
<table cellspacing="0" cellpadding="6">
<tbody>
<tr>
<th scope="col">sorted collection</th>
<th scope="col">order collection</th>
</tr>
<tr>
<td width="50%">A sorted collection is sorting a collection by utilizing the sorting features provided by the Java collections framework. The sorting occurs in the memory of JVM which running Hibernate, after the data being read from database using java comparator.</td>
<td>Order collection is sorting a collection by specifying the order-by clause for sorting this collection when retrieval.</td>
</tr>
<tr>
<td>If your collection is not large, it will be more efficient way to sort it.</td>
<td>If your collection is very large, it will be more efficient way to sort it .</td>
</tr>
</tbody>
</table>
</div>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/hibernate/'>hibernate</a>, <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/orm/'>orm</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/346/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=346&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>

		<media:content url="http://www.developersbook.com/hibernate/images/component-mapping.png" medium="image">
			<media:title type="html">Component Mapping</media:title>
		</media:content>
	</item>
		<item>
		<title>Hibernate Interview questions (1)</title>
		<link>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-1/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-1/#comments</comments>
		<pubDate>Fri, 27 Aug 2010 01:59:58 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[orm]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=339</guid>
		<description><![CDATA[1.What is ORM ? ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database. 2.What does ORM consists of ? An ORM solution consists of the followig four pieces: API for performing basic CRUD operations API to express queries refering to classes [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=339&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>1.What is ORM ? </strong></p>
<p>ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.<br />
<strong>2.What does ORM consists of ? </strong></p>
<p>An  ORM solution consists of the followig four pieces:</p>
<ul>
<li>API for performing basic CRUD operations</li>
<li>API to express queries refering to classes</li>
<li>Facilities to specify metadata</li>
<li>Optimization facilities : dirty checking,lazy associations fetching</li>
</ul>
<p><strong>3.What are the ORM levels ? </strong></p>
<p>The ORM levels are:</p>
<ul>
<li>Pure relational (stored procedure.)</li>
<li>Light objects mapping (JDBC)</li>
<li>Medium object mapping</li>
<li>Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)</li>
</ul>
<p><strong>4.What is Hibernate? </strong></p>
<p>Hibernate is a pure Java object-relational mapping (ORM) and persistence framework that allows you to map plain old Java objects to relational database tables using (XML) configuration files.Its purpose is to relieve the developer from a significant amount of relational data persistence-related programming tasks.<br />
<strong>5.Why do you need ORM tools like hibernate? </strong></p>
<p>The  main advantage of ORM like hibernate is that it shields developers from messy SQL. 	Apart from this, ORM provides following benefits:</p>
<ul>
<li><strong>Improved productivity</strong>
<ul>
<li>High-level object-oriented API</li>
<li>Less Java code to write</li>
<li>No SQL to write</li>
</ul>
</li>
<li><strong>Improved performance</strong>
<ul>
<li>Sophisticated caching</li>
<li>Lazy loading</li>
<li>Eager loading</li>
</ul>
</li>
<li><strong>Improved maintainability</strong>
<ul>
<li>A lot less code to write</li>
</ul>
</li>
<li><strong>Improved portability</strong>
<ul>
<li>ORM framework generates database-specific SQL for you</li>
</ul>
</li>
</ul>
<p><strong>6.What Does Hibernate Simplify? </strong></p>
<p>Hibernate simplifies:</p>
<ul>
<li>Saving and retrieving your domain objects</li>
<li>Making database column and table name changes</li>
<li>Centralizing pre save and post retrieve logic</li>
<li>Complex joins for retrieving related items</li>
<li>Schema creation from object model</li>
</ul>
<p><strong>7.What is the need for Hibernate xml mapping file? </strong></p>
<p>Hibernate mapping file tells Hibernate which tables and columns to use to load and store objects. Typical mapping file look as follows:</p>
<p><img src="http://www.developersbook.com/hibernate/images/hbm-mapping.png" border="0" alt="Hibernate Mapping file" width="483" height="273" /></p>
<p><strong>8.What are the most common methods of Hibernate configuration? </strong></p>
<p>The most common methods of Hibernate configuration are:</p>
<ul>
<li>Programmatic configuration</li>
<li>XML configuration (<code>hibernate.cfg.xml</code>)</li>
</ul>
<p><strong>9.What are the important tags of hibernate.cfg.xml? </strong></p>
<p>Following are the important tags of hibernate.cfg.xml:</p>
<p><img src="http://www.developersbook.com/hibernate/images/hbm-config.png" border="0" alt="hibernate.cfg.xml" width="616" height="372" /></p>
<p><strong>10.What are the Core interfaces are of Hibernate framework?</strong></p>
<div>
<p>The five core interfaces are used in just about every Hibernate application. Using these interfaces, you can store and retrieve persistent objects and control transactions.</p>
<ul>
<li>Session interface</li>
<li>SessionFactory interface</li>
<li>Configuration interface</li>
<li>Transaction interface</li>
<li>Query and Criteria interfaces</li>
</ul>
</div>
<div>
<p><strong>11.What role does the Session interface play in Hibernate? </strong>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.</p>
<p><code>Session session = sessionFactory.openSession();</code></p>
<p><strong>Session interface role</strong>:</p>
<ul>
<li>Wraps a JDBC connection</li>
<li>Factory for Transaction</li>
<li>Holds a mandatory (first-level) cache of persistent objects, used when navigating the object graph or looking up objects by identifier</li>
</ul>
<p><strong>12.What role does the SessionFactory interface play in Hibernate? </strong>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</p>
<p><code>SessionFactory sessionFactory = configuration.buildSessionFactory();</code></p>
<p><strong>13.What is the general flow of Hibernate communication with RDBMS? </strong>The general flow of Hibernate communication with RDBMS is :</p>
<ul>
<li>Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files</li>
<li>Create session factory from configuration object</li>
<li>Get one session from this session factory</li>
<li>Create HQL Query</li>
<li>Execute query to get list containing Java objects</li>
</ul>
<p><strong>14.What is Hibernate Query Language (HQL)? </strong>Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update, and retrieve objects from a database. This language, the Hibernate query Language (HQL), is an object-oriented extension to SQL.<br />
<strong>15.How do you map Java Objects with Database tables? </strong></p>
<ul>
<li>First we need to write Java domain objects (beans with setter and getter).</li>
<li>Write hbm.xml, where we map java class to  table and database columns to Java class variables.</li>
</ul>
<p><strong>Example</strong> :</p>
<pre><span style="color:#008000;">&lt;hibernate-mapping&gt;</span>
  <span style="color:#008000;">&lt;class</span> <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"com.test.User"</span>  <span style="color:#800040;">table</span>=<span style="color:#0000ff;">"user"</span><span style="color:#008000;">&gt;</span>
   <span style="color:#008000;">&lt;property</span>  <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"USER_NAME"</span> <span style="color:#800040;">length</span>=<span style="color:#0000ff;">"255"</span>
      <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"userName"</span> <span style="color:#800040;">not-null</span>=<span style="color:#800040;">"true"</span>  <span style="color:#800040;">type</span>=<span style="color:#0000ff;">"java.lang.String"</span><span style="color:#008000;">/&gt;</span>
   <span style="color:#008000;">&lt;property</span>  <span style="color:#800040;">column</span>=<span style="color:#0000ff;">"USER_PASSWORD"</span> <span style="color:#800040;">length</span>=<span style="color:#0000ff;">"255"</span>
     <span style="color:#800040;">name</span>=<span style="color:#0000ff;">"userPassword"</span> <span style="color:#800040;">not-null</span>=<span style="color:#0000ff;">"true"</span>  <span style="color:#800040;">type</span>=<span style="color:#0000ff;">"java.lang.String"</span><span style="color:#008000;">/&gt;</span>
 <span style="color:#008000;">&lt;/class&gt;</span>
<span style="color:#008000;">&lt;/hibernate-mapping&gt;</span></pre>
</div>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/hibernate/'>hibernate</a>, <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/orm/'>orm</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=339&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/27/hibernate-interview-questions-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>

		<media:content url="http://www.developersbook.com/hibernate/images/hbm-mapping.png" medium="image">
			<media:title type="html">Hibernate Mapping file</media:title>
		</media:content>

		<media:content url="http://www.developersbook.com/hibernate/images/hbm-config.png" medium="image">
			<media:title type="html">hibernate.cfg.xml</media:title>
		</media:content>
	</item>
		<item>
		<title>The PHP development environment in Eclipse</title>
		<link>http://kaisechen.wordpress.com/2010/08/23/the-php-development-environment-in-eclipse/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/23/the-php-development-environment-in-eclipse/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 02:00:30 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[eclipse]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[pdt]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=335</guid>
		<description><![CDATA[The PDT project provides a PHP Development Tools framework for the Eclipse platform.This project encompasses all development components necessary to develop PHP and facilitate extensibility. It leverages the existing Web Tools Platform (WTP) and Dynamic Languages Toolkit (DLTK) in providing developers with PHP capabilities. 1. Firstly download eclipse classic version http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6-201006080911/eclipse-SDK-3.6-win32.zip Tagged: eclipse, pdt, php<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=335&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The PDT project provides a PHP Development Tools framework for the Eclipse platform.This project encompasses all development components necessary to develop PHP and facilitate extensibility. It leverages the existing Web Tools Platform (WTP) and Dynamic Languages Toolkit (DLTK) in providing developers with PHP capabilities.</p>
<p><strong>1. Firstly download eclipse classic version </strong></p>
<p>http://www.eclipse.org/downloads/download.php?file=/eclipse/downloads/drops/R-3.6-201006080911/eclipse-SDK-3.6-win32.zip</p>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/eclipse/'>eclipse</a>, <a href='http://kaisechen.wordpress.com/tag/pdt/'>pdt</a>, <a href='http://kaisechen.wordpress.com/tag/php/'>php</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/335/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=335&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/23/the-php-development-environment-in-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>Java concurrent programming (2) — Create a concurrent application in Java</title>
		<link>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-2-%e2%80%94-create-a-concurrent-application-in-java/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-2-%e2%80%94-create-a-concurrent-application-in-java/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 10:20:40 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[concurrent]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=332</guid>
		<description><![CDATA[In Java, each thread is associated with a instance of Thread class. Before JDK1.5, there is only one way to create a concurrent application. This way directly controls thread control and management, just instantiate Thread each time when the application need to initiate an asynchronous Task. After JDK1.5, there is a new package &#8216;java.util.concurrent&#8217; which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=332&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In Java, each thread is associated with a instance of Thread class.</p>
<p>Before JDK1.5, there is only one way to create a concurrent application. This way directly controls thread control and management, just instantiate Thread each time when the application need to initiate an asynchronous Task.</p>
<p>After JDK1.5, there is a new package &#8216;java.util.concurrent&#8217; which provides new API supporting concurrent.  A new concept &#8216;executor&#8217; can receive the application&#8217;s task and deal with it according to pre-defined rule. It separates the thread management and other rest part of the application.</p>
<p>The first way, when creating an instance of Thread, code running in the thread must be provided. There are two ways to do so.</p>
<p><strong>1. Implement <em>Runnable </em>interface</strong></p>
<p>Runnable interface defines run method, meant to contain the code execute in the thread.The Runnable object need be passed to a Thread constructor.</p>
<p>E.g.</p>
<pre>public class Runner1 implements Runnable {

 public void run() {
 for(int i=0;i&lt;30;i++){
 String s = Thread.currentThread().getName();
 System.out.println(s +" : "+i);
 }
 }

 public static void main(String args[]) {
 (new Thread(new Runner1())).start();
 }

}</pre>
<p><strong>2. Inherit Thread Class</strong></p>
<p>The Thread Class already implements Runnable interface.  An subclass extends Thread need to override run method.</p>
<p>E.g.</p>
<pre>public class MyRunner extends Thread {

 private int n;

 public MyRunner(int n) {
 this.n = n;
 }

 public void run(){
 for(int i=0;i&lt;n;i++){
 System.out.println(this.getName()+":"+i);            
 }
 System.out.println(this.getName()+" Finished");
 }

 public static void main(String[] args){
 MyRunner m = new MyRunner(4);
 m.start();
 }

}</pre>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/concurrent/'>concurrent</a>, <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/thread/'>thread</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/332/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/332/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/332/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=332&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-2-%e2%80%94-create-a-concurrent-application-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>Java concurrent programming (1) &#8212; Process and Thread</title>
		<link>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-1-process-and-thread/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-1-process-and-thread/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 06:16:28 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[concurrent]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[process]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=329</guid>
		<description><![CDATA[There are two units associated with concurrent programming: process and thread. But in Java environment, it is mostly concerned with Thread. Process A process has a self-contained executive environment, which owns  a complete , private  set of run-time resources and occupied memory space. In most situation, it is called program or application. But actually,  a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=329&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>There are two units associated with concurrent programming: process and thread. But in Java environment, it is mostly concerned with Thread.</p>
<p><strong>Process</strong></p>
<p>A process has a self-contained executive environment, which owns  a complete , private  set of run-time resources and occupied memory space.</p>
<p>In most situation, it is called program or application. But actually,  a single application may be consist of  a set of cooperating processes. Processes need communicate with each other, and most OSs support Inter Process Communication(IPC) resources, e.g. pipes and sockets.</p>
<p>In Java environment, JVM(Java Virtual Machine) mostly runs as a single process.  However, a Java application can create additional processes using a ProcessBuilder object.</p>
<p><strong>Thread</strong></p>
<p>Some people calls thread &#8216;lightweight process&#8217;.  Thread provides an execution environment like process, but less resource is required when creating a thread than creating a process.</p>
<p>Thread exists in process, so a process at least own a thread.</p>
<p>Threads exists in a process will share the process&#8217;s resource including memory and open files.</p>
<p>Java platform supports multi-thread, a Java application starts with one thread &#8212; &#8216;Main thread&#8217;,  main thread can create additional threads.</p>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/concurrent/'>concurrent</a>, <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/process/'>process</a>, <a href='http://kaisechen.wordpress.com/tag/thread/'>thread</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/329/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=329&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/10/java-concurrent-programming-1-process-and-thread/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>
	</item>
		<item>
		<title>Create web service (4) — JAX-RS and JAX-RS Client</title>
		<link>http://kaisechen.wordpress.com/2010/08/04/create-web-service-4-%e2%80%94-jax-rs-and-jax-rs-client/</link>
		<comments>http://kaisechen.wordpress.com/2010/08/04/create-web-service-4-%e2%80%94-jax-rs-and-jax-rs-client/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 01:19:51 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[myeclipse]]></category>
		<category><![CDATA[webservice]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=296</guid>
		<description><![CDATA[It is becoming more and more popular using rest-style web service. 1. create a new web service project, which name is TestWS_RS. Chose Web Service Framework: REST(JAX-RS) It automatically produces J2EE structure, and check the web.xml. &#60;?xml version="1.0" encoding="UTF-8"?&#62; &#60;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&#62; &#60;servlet&#62; &#60;display-name&#62;JAX-RS REST Servlet&#60;/display-name&#62; &#60;servlet-name&#62;JAX-RS REST Servlet&#60;/servlet-name&#62; &#60;servlet-class&#62; com.sun.jersey.spi.container.servlet.ServletContainer &#60;/servlet-class&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=296&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It is becoming more and more popular using rest-style web service.</p>
<p><strong>1. create a new web service project, which name is TestWS_RS.</strong></p>
<p>Chose Web Service Framework: REST(JAX-RS)</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_42.png"><img class="alignnone size-medium wp-image-297" title="ws_42" src="http://kaisechen.files.wordpress.com/2010/08/ws_42.png?w=271&#038;h=300" alt="" width="271" height="300" /></a></p>
<p>It automatically produces J2EE structure, and check the web.xml.</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
 &lt;servlet&gt;
 &lt;display-name&gt;JAX-RS REST Servlet&lt;/display-name&gt;
 &lt;servlet-name&gt;JAX-RS REST Servlet&lt;/servlet-name&gt;
 &lt;servlet-class&gt;
 com.sun.jersey.spi.container.servlet.ServletContainer
 &lt;/servlet-class&gt;
 &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
 &lt;/servlet&gt;
 &lt;servlet-mapping&gt;
 &lt;servlet-name&gt;JAX-RS REST Servlet&lt;/servlet-name&gt;
 &lt;url-pattern&gt;/services/*&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
 &lt;welcome-file-list&gt;
 &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
 &lt;/welcome-file-list&gt;
&lt;/web-app&gt;</pre>
<p><strong>2.Add JAX-RS libraries into the project</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_421.png"><img class="alignnone size-medium wp-image-300" title="ws_42" src="http://kaisechen.files.wordpress.com/2010/08/ws_421.png?w=271&#038;h=300" alt="" width="271" height="300" /></a></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_431.png"><img class="alignnone size-medium wp-image-301" title="ws_43" src="http://kaisechen.files.wordpress.com/2010/08/ws_431.png?w=300&#038;h=236" alt="" width="300" height="236" /></a></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_44.png"><img class="alignnone size-medium wp-image-303" title="ws_44" src="http://kaisechen.files.wordpress.com/2010/08/ws_44.png?w=291&#038;h=300" alt="" width="291" height="300" /></a></p>
<p>chose JAX-RS 1.0.2 Core Libraries(Project Jersey 1.0.2)</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_45.png"><img class="alignnone size-medium wp-image-304" title="ws_45" src="http://kaisechen.files.wordpress.com/2010/08/ws_45.png?w=290&#038;h=300" alt="" width="290" height="300" /></a></p>
<p>JAXB supports the transform between Class and XML.</p>
<p><strong>3. Create a POJO class User and add @XmlRootElement above it<br />
</strong></p>
<p>package com.jack.ws;</p>
<p>import javax.xml.bind.annotation.XmlRootElement;</p>
<p>@XmlRootElement<br />
public class User {<br />
private int id;<br />
private String preferedname;<br />
private String firstname;<br />
private String lastname;<br />
private String gender;</p>
<p>public int getId() {<br />
return id;<br />
}</p>
<p>public void setId(int id) {<br />
this.id = id;<br />
}</p>
<p>public String getPreferedname() {<br />
return preferedname;<br />
}</p>
<p>public void setPreferedname(String preferedname) {<br />
this.preferedname = preferedname;<br />
}</p>
<p>public String getFirstname() {<br />
return firstname;<br />
}</p>
<p>public void setFirstname(String firstname) {<br />
this.firstname = firstname;<br />
}</p>
<p>public String getLastname() {<br />
return lastname;<br />
}</p>
<p>public void setLastname(String lastname) {<br />
this.lastname = lastname;<br />
}</p>
<p>public String getGender() {<br />
return gender;<br />
}</p>
<p>public void setGender(String gender) {<br />
this.gender = gender;<br />
}</p>
<p>@Override<br />
public String toString() {<br />
return &#8220;Id:&#8221;+id+&#8221; , FirstName:&#8221;+firstname+&#8221; , LastName:&#8221;+lastname+&#8221; , PreferedName:&#8221;+preferedname+&#8221; , Gender:&#8221;+gender;<br />
}</p>
<p>}</p>
<p><strong>4.Build a web service, UserResource</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_46.png"><img class="alignnone size-full wp-image-305" title="ws_46" src="http://kaisechen.files.wordpress.com/2010/08/ws_46.png?w=275&#038;h=125" alt="" width="275" height="125" /></a></p>
<p>Chose Framework: REST(JAX-RS), Tick &#8216;Create new Java bean&#8217;</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_47.png"><img class="alignnone size-medium wp-image-306" title="ws_47" src="http://kaisechen.files.wordpress.com/2010/08/ws_47.png?w=298&#038;h=300" alt="" width="298" height="300" /></a></p>
<p>In java class field,input &#8216;UserResource&#8217;; chose Produces: application/xml; in URL path field, input &#8216;users&#8217;.</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_48.png"><img class="alignnone size-medium wp-image-307" title="ws_48" src="http://kaisechen.files.wordpress.com/2010/08/ws_48.png?w=298&#038;h=300" alt="" width="298" height="300" /></a></p>
<p>Then click finish button, it produces a &#8216;UserResource&#8217; Class</p>
<p><strong>5. Now begin to add REST methods in UserResource Class.</strong></p>
<p>Chose UserResource Class,right click ,chose &#8216;MyEclipse&#8217;,chose &#8216;Add REST Method&#8230;&#8217;.</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_49.png"><img class="alignnone size-medium wp-image-308" title="ws_49" src="http://kaisechen.files.wordpress.com/2010/08/ws_49.png?w=300&#038;h=135" alt="" width="300" height="135" /></a></p>
<p><em>Firstly, add &#8216;getUsers&#8217; method.</em></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_50.png"><img class="alignnone size-medium wp-image-309" title="ws_50" src="http://kaisechen.files.wordpress.com/2010/08/ws_50.png?w=300&#038;h=253" alt="" width="300" height="253" /></a></p>
<p><em>Secondly,add &#8216;getUser&#8217; method.</em></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_51.png"><img class="alignnone size-medium wp-image-310" title="ws_51" src="http://kaisechen.files.wordpress.com/2010/08/ws_51.png?w=300&#038;h=255" alt="" width="300" height="255" /></a></p>
<p><em>Thirdly,add &#8216;addUser&#8217; method.</em></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_52.png"><img class="alignnone size-medium wp-image-311" title="ws_52" src="http://kaisechen.files.wordpress.com/2010/08/ws_52.png?w=300&#038;h=254" alt="" width="300" height="254" /></a></p>
<p><strong>6. Modify UserResource Class</strong></p>
<pre>package com.jack.ws;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.sun.jersey.spi.resource.Singleton;

@Produces("application/xml")
@Path("users")
@Singleton
public class UserResource {
 private TreeMap&lt;Integer, User&gt; userMap = new TreeMap&lt;Integer, User&gt;();

 public UserResource(){
 User user = new User();
 user.setFirstname("Jackie");
 user.setLastname("Chan");
 user.setPreferedname("Jack");
 user.setGender("Male");
 addUser(user);
 }

 @GET
 public List&lt;User&gt; getUsers() {
 List&lt;User&gt; users = new ArrayList&lt;User&gt;();
 users.addAll(userMap.values());
 return users;
 }

 @GET
 @Path("{id}")
 public User getUser(@PathParam("id") int cId) {
 return userMap.get(cId);
 }

 @POST
 @Path("add")
 @Produces("text/plain")
 @Consumes("application/xml")
 public String addUser(User user) {
 int id = userMap.size();
 user.setId(id);
 userMap.put(id, user);
 return "Added a new User and Its info is "+user.toString();
 }
}</pre>
<p><strong>7.Deploy the web service into tomcat 6 server.</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_53.png"><img class="alignnone size-medium wp-image-312" title="ws_53" src="http://kaisechen.files.wordpress.com/2010/08/ws_53.png?w=256&#038;h=300" alt="" width="256" height="300" /></a></p>
<p><strong>8.Start Tomcat server</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_54.png"><img class="alignnone size-medium wp-image-313" title="ws_54" src="http://kaisechen.files.wordpress.com/2010/08/ws_54.png?w=300&#038;h=118" alt="" width="300" height="118" /></a></p>
<p>Some information about the web service in tomcat6server console.</p>
<pre>04/08/2010 11:57:18 AM org.apache.catalina.startup.HostConfig checkResources
INFO: Reloading context [/TestWS_RS]
04/08/2010 11:57:20 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Scanning for root resource and provider classes in the paths:
 D:\tomcat\webapps\TestWS_RS\WEB-INF\lib
 D:\tomcat\webapps\TestWS_RS\WEB-INF\classes
04/08/2010 11:57:21 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Root resource classes found:
 class com.jack.ws.CustomersResource
 class com.jack.ws.UserResource
04/08/2010 11:57:21 AM com.sun.jersey.api.core.ClasspathResourceConfig init
INFO: Provider classes found:</pre>
<p><strong>9.Now begin to test the web service</strong></p>
<p>chose this project, right click, chose &#8216;MyEclipse&#8217;,then chose &#8216;Test with web service explore&#8217;.</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_55.png"><img class="alignnone size-medium wp-image-314" title="ws_55" src="http://kaisechen.files.wordpress.com/2010/08/ws_55.png?w=300&#038;h=189" alt="" width="300" height="189" /></a></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_56.png"><img class="alignnone size-medium wp-image-315" title="ws_56" src="http://kaisechen.files.wordpress.com/2010/08/ws_56.png?w=300&#038;h=158" alt="" width="300" height="158" /></a></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_43.png"><br />
</a><strong>10.click id,input 0 in id field, then click Test button.</strong></p>
<p>It gets a Response</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 &lt;user&gt;
 &lt;firstname&gt;Jackie&lt;/firstname&gt;
 &lt;gender&gt;Male&lt;/gender&gt;
 &lt;id&gt;0&lt;/id&gt;
 &lt;lastname&gt;Chan&lt;/lastname&gt;
 &lt;preferedname&gt;Jack&lt;/preferedname&gt;
 &lt;/user&gt;</pre>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_57.png"><img class="alignnone size-medium wp-image-316" title="ws_57" src="http://kaisechen.files.wordpress.com/2010/08/ws_57.png?w=300&#038;h=271" alt="" width="300" height="271" /></a></p>
<p><strong>11. Click add,in the content field, input </strong></p>
<pre>&lt;user&gt;
 &lt;firstname&gt;Jeanny&lt;/firstname&gt;
 &lt;lastname&gt;Shery&lt;/lastname&gt;
 &lt;preferedname&gt;Jean&lt;/preferedname&gt;
 &lt;gender&gt;Female&lt;/gender&gt;
&lt;/user&gt;</pre>
<p>click test button, get a response</p>
<pre>Added a new User and Its info is Id:1 , FirstName:Jeanny , LastName:Shery , PreferedName:Jean , Gender:Female</pre>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_58.png"><img class="alignnone size-medium wp-image-318" title="ws_58" src="http://kaisechen.files.wordpress.com/2010/08/ws_58.png?w=300&#038;h=222" alt="" width="300" height="222" /></a></p>
<p><strong>12. Click User,Click Test Button, it gets a response.</strong></p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
 &lt;users&gt;
 &lt;user&gt;
 &lt;firstname&gt;Jackie&lt;/firstname&gt;
 &lt;gender&gt;Male&lt;/gender&gt;
 &lt;id&gt;0&lt;/id&gt;
 &lt;lastname&gt;Chan&lt;/lastname&gt;
 &lt;preferedname&gt;Jack&lt;/preferedname&gt;
 &lt;/user&gt;
 &lt;user&gt;
 &lt;firstname&gt;Jeanny&lt;/firstname&gt;
 &lt;gender&gt;Female&lt;/gender&gt;
 &lt;id&gt;1&lt;/id&gt;
 &lt;lastname&gt;Shery&lt;/lastname&gt;
 &lt;preferedname&gt;Jean&lt;/preferedname&gt;
 &lt;/user&gt;
 &lt;/users&gt;</pre>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_59.png"><img class="alignnone size-medium wp-image-319" title="ws_59" src="http://kaisechen.files.wordpress.com/2010/08/ws_59.png?w=300&#038;h=260" alt="" width="300" height="260" /></a></p>
<p><strong>13. Now begin to use other browser to test the web server, open firefox, input</strong></p>
<p>http://localhost:8080/TestWS_RS/services/users/0</p>
<p>It will invoke getUser method in UserResource Class, and return xml of a user object</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_60.png"><img class="alignnone size-medium wp-image-320" title="ws_60" src="http://kaisechen.files.wordpress.com/2010/08/ws_60.png?w=300&#038;h=217" alt="" width="300" height="217" /></a></p>
<p>http://localhost:8080/TestWS_RS/services/users/1</p>
<p>It will invoke getUser method in UserResource Class, and return xml of another user object</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_61.png"><img class="alignnone size-medium wp-image-321" title="ws_61" src="http://kaisechen.files.wordpress.com/2010/08/ws_61.png?w=300&#038;h=215" alt="" width="300" height="215" /></a></p>
<p><strong>14. Install poster plugin in firefox, use poster to test addUser method of the web service</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_62.png"><img class="alignnone size-medium wp-image-322" title="ws_62" src="http://kaisechen.files.wordpress.com/2010/08/ws_62.png?w=300&#038;h=217" alt="" width="300" height="217" /></a></p>
<p>Open poster</p>
<p>In URL field, input &#8216;http://localhost:8080/TestWS_RS/services/users/add&#8217;</p>
<p>In Content Type field, input &#8216;application/xml&#8217;</p>
<p>In Content field,input:</p>
<pre>&lt;user&gt;
 &lt;firstname&gt;Hilly&lt;/firstname&gt;
 &lt;lastname&gt;Paris&lt;/lastname&gt;
 &lt;preferedname&gt;Hill&lt;/preferedname&gt;
 &lt;gender&gt;Male&lt;/gender&gt;
&lt;/user&gt;

<a href="http://kaisechen.files.wordpress.com/2010/08/ws_63.png"><img class="alignnone size-medium wp-image-323" title="ws_63" src="http://kaisechen.files.wordpress.com/2010/08/ws_63.png?w=226&#038;h=300" alt="" width="226" height="300" /></a></pre>
<p>Click post button, it will get a response quickly.</p>
<pre>Added a new User and Its info is Id:2 , FirstName:Hilly , LastName:Paris , PreferedName:Hill , Gender:Male</pre>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_64.png"><img class="alignnone size-medium wp-image-324" title="ws_64" src="http://kaisechen.files.wordpress.com/2010/08/ws_64.png?w=286&#038;h=300" alt="" width="286" height="300" /></a></p>
<p><strong>15. Open firefox, input the url</strong></p>
<p>http://localhost:8080/TestWS_RS/services/users</p>
<p>It will invoke getUsers method in UserResource Class and return a user list.</p>
<pre>&lt;users&gt;
−
&lt;user&gt;
&lt;firstname&gt;Jackie&lt;/firstname&gt;
&lt;gender&gt;Male&lt;/gender&gt;
&lt;id&gt;0&lt;/id&gt;
&lt;lastname&gt;Chan&lt;/lastname&gt;
&lt;preferedname&gt;Jack&lt;/preferedname&gt;
&lt;/user&gt;
−
&lt;user&gt;
&lt;firstname&gt;Jeanny&lt;/firstname&gt;
&lt;gender&gt;Female&lt;/gender&gt;
&lt;id&gt;1&lt;/id&gt;
&lt;lastname&gt;Shery&lt;/lastname&gt;
&lt;preferedname&gt;Jean&lt;/preferedname&gt;
&lt;/user&gt;
−
&lt;user&gt;
&lt;firstname&gt;Hilly&lt;/firstname&gt;
&lt;gender&gt;Male&lt;/gender&gt;
&lt;id&gt;2&lt;/id&gt;
&lt;lastname&gt;Paris&lt;/lastname&gt;
&lt;preferedname&gt;Hill&lt;/preferedname&gt;
&lt;/user&gt;
&lt;/users&gt;</pre>
<p><a href="http://kaisechen.files.wordpress.com/2010/08/ws_65.png"><img class="alignnone size-medium wp-image-325" title="ws_65" src="http://kaisechen.files.wordpress.com/2010/08/ws_65.png?w=248&#038;h=300" alt="" width="248" height="300" /></a></p>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/myeclipse/'>myeclipse</a>, <a href='http://kaisechen.wordpress.com/tag/rest/'>rest</a>, <a href='http://kaisechen.wordpress.com/tag/web-service/'>web service</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/296/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/296/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/296/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=296&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/08/04/create-web-service-4-%e2%80%94-jax-rs-and-jax-rs-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_42.png?w=271" medium="image">
			<media:title type="html">ws_42</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_421.png?w=271" medium="image">
			<media:title type="html">ws_42</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_431.png?w=300" medium="image">
			<media:title type="html">ws_43</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_44.png?w=291" medium="image">
			<media:title type="html">ws_44</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_45.png?w=290" medium="image">
			<media:title type="html">ws_45</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_46.png" medium="image">
			<media:title type="html">ws_46</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_47.png?w=298" medium="image">
			<media:title type="html">ws_47</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_48.png?w=298" medium="image">
			<media:title type="html">ws_48</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_49.png?w=300" medium="image">
			<media:title type="html">ws_49</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_50.png?w=300" medium="image">
			<media:title type="html">ws_50</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_51.png?w=300" medium="image">
			<media:title type="html">ws_51</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_52.png?w=300" medium="image">
			<media:title type="html">ws_52</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_53.png?w=256" medium="image">
			<media:title type="html">ws_53</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_54.png?w=300" medium="image">
			<media:title type="html">ws_54</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_55.png?w=300" medium="image">
			<media:title type="html">ws_55</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_56.png?w=300" medium="image">
			<media:title type="html">ws_56</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_57.png?w=300" medium="image">
			<media:title type="html">ws_57</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_58.png?w=300" medium="image">
			<media:title type="html">ws_58</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_59.png?w=300" medium="image">
			<media:title type="html">ws_59</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_60.png?w=300" medium="image">
			<media:title type="html">ws_60</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_61.png?w=300" medium="image">
			<media:title type="html">ws_61</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_62.png?w=300" medium="image">
			<media:title type="html">ws_62</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_63.png?w=226" medium="image">
			<media:title type="html">ws_63</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_64.png?w=286" medium="image">
			<media:title type="html">ws_64</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/08/ws_65.png?w=248" medium="image">
			<media:title type="html">ws_65</media:title>
		</media:content>
	</item>
		<item>
		<title>Create web service (3) &#8212; JAX-WS and JAX-WS Client</title>
		<link>http://kaisechen.wordpress.com/2010/07/28/create-web-service-3-jax-ws/</link>
		<comments>http://kaisechen.wordpress.com/2010/07/28/create-web-service-3-jax-ws/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 12:18:44 +0000</pubDate>
		<dc:creator>kaisechen</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[myeclipse]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://kaisechen.wordpress.com/?p=266</guid>
		<description><![CDATA[In MyEclipse , it is very easy to build web service base on some web service framework. Let&#8217;s build a JAX-WS web service. 1.Create a web service project in MyEclipse, which name is TestWS_JAXWS. chose JAX-WS framework It automatically produces J2EE directory. 2. import JAX-WS 2.1 library into the project chose MyEclipse Libraries Select JAX-WS [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=266&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In MyEclipse , it is very easy to build web service base on some web service framework.</p>
<p>Let&#8217;s build a JAX-WS web service.</p>
<p><strong>1.Create a web service project in MyEclipse, which name is TestWS_JAXWS.</strong></p>
<p>chose JAX-WS framework</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_21.png"><img class="alignnone size-medium wp-image-267" title="ws_21" src="http://kaisechen.files.wordpress.com/2010/07/ws_21.png?w=271&#038;h=300" alt="" width="271" height="300" /></a></p>
<p>It automatically produces J2EE directory.</p>
<p><strong>2. import JAX-WS 2.1 library into the project</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_22.png"><img class="alignnone size-medium wp-image-268" title="ws_22" src="http://kaisechen.files.wordpress.com/2010/07/ws_22.png?w=300&#038;h=106" alt="" width="300" height="106" /></a></p>
<p>chose MyEclipse Libraries</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_23.png"><img class="alignnone size-medium wp-image-269" title="ws_23" src="http://kaisechen.files.wordpress.com/2010/07/ws_23.png?w=293&#038;h=300" alt="" width="293" height="300" /></a></p>
<p>Select JAX-WS 2.1 Libraries</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_24.png"><img class="alignnone size-medium wp-image-270" title="ws_24" src="http://kaisechen.files.wordpress.com/2010/07/ws_24.png?w=291&#038;h=300" alt="" width="291" height="300" /></a></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_25.png"><img class="alignnone size-medium wp-image-271" title="ws_25" src="http://kaisechen.files.wordpress.com/2010/07/ws_25.png?w=300&#038;h=157" alt="" width="300" height="157" /></a></p>
<p><strong>3. Build a simple java which includes several methods</strong></p>
<pre>public class Calculator {
 public int add(int a, int b) {
 return (a + b);
 }

 public int subtract(int a, int b) {
 return (a - b);
 }

 public int multiply(int a, int b) {
 return (a * b);
 }

 public int divide(int a, int b) {
 return (a / b);
 }
}</pre>
<p><strong>4. Build a new web service<br />
</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_26.png"><img class="alignnone size-medium wp-image-272" title="ws_26" src="http://kaisechen.files.wordpress.com/2010/07/ws_26.png?w=300&#038;h=103" alt="" width="300" height="103" /></a></p>
<p>chose JAX-WS Framework and &#8216;Create web service from Java class&#8217;</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_27.png"><img class="alignnone size-medium wp-image-273" title="ws_27" src="http://kaisechen.files.wordpress.com/2010/07/ws_27.png?w=253&#038;h=300" alt="" width="253" height="300" /></a></p>
<p>In Java Class field, input &#8216;com.jack.ws.Calculator&#8217; which is created at above step</p>
<p>It automatically fill in other fields,</p>
<p>Modify target namespace field to &#8216;http://localhost:8080/TestWS_JAXWS&#8217;</p>
<p>tick &#8216;Generate WSDL in project&#8217;</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_281.png"><img class="alignnone size-medium wp-image-284" title="ws_28" src="http://kaisechen.files.wordpress.com/2010/07/ws_281.png?w=212&#038;h=300" alt="" width="212" height="300" /></a></p>
<p><strong>5. After click Finish button, it produces a lot of  files, directory and class</strong></p>
<p>CalculatorDelegate class</p>
<p>wsdl directory, web.xml under WEB-INF</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_29.png"><img class="alignnone size-medium wp-image-275" title="ws_29" src="http://kaisechen.files.wordpress.com/2010/07/ws_29.png?w=300&#038;h=296" alt="" width="300" height="296" /></a></p>
<p>CalculatorDelegate.java</p>
<pre>@javax.jws.WebService(targetNamespace = "http://localhost:8080/TestWS_JAXWS", serviceName = "CalculatorService", portName = "CalculatorPort", wsdlLocation = "WEB-INF/wsdl/CalculatorService.wsdl")
public class CalculatorDelegate {

 com.jack.ws.Calculator calculator = new com.jack.ws.Calculator();

 public int add(int a, int b) {
 return calculator.add(a, b);
 }

 public int subtract(int a, int b) {
 return calculator.subtract(a, b);
 }

 public int multiply(int a, int b) {
 return calculator.multiply(a, b);
 }

 public int divide(int a, int b) {
 return calculator.divide(a, b);
 }

}</pre>
<p>web.xml</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
 &lt;servlet&gt;
 &lt;description&gt;JAX-WS endpoint - CalculatorService&lt;/description&gt;
 &lt;display-name&gt;CalculatorService&lt;/display-name&gt;
 &lt;servlet-name&gt;CalculatorService&lt;/servlet-name&gt;
 &lt;servlet-class&gt;
 com.sun.xml.ws.transport.http.servlet.WSServlet
 &lt;/servlet-class&gt;
 &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
 &lt;/servlet&gt;
 &lt;servlet-mapping&gt;
 &lt;servlet-name&gt;CalculatorService&lt;/servlet-name&gt;
 &lt;url-pattern&gt;/CalculatorPort&lt;/url-pattern&gt;
 &lt;/servlet-mapping&gt;
 &lt;welcome-file-list&gt;
 &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
 &lt;/welcome-file-list&gt;
 &lt;listener&gt;
 &lt;listener-class&gt;
 com.sun.xml.ws.transport.http.servlet.WSServletContextListener
 &lt;/listener-class&gt;
 &lt;/listener&gt;&lt;/web-app&gt;</pre>
<p>sun-jaxws.xml</p>
<pre>&lt;?xml version = "1.0"?&gt;
&lt;endpoints version="2.0"
 xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"&gt;
 &lt;endpoint name="CalculatorPort"
 implementation="com.jack.ws.CalculatorDelegate"
 url-pattern="/CalculatorPort"&gt;
 &lt;/endpoint&gt;&lt;/endpoints&gt;</pre>
<p>CalculatorService.wsdl</p>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-hudson-390-. --&gt;
&lt;definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8080/TestWS_JAXWS" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CalculatorService" targetNamespace="http://localhost:8080/TestWS_JAXWS"&gt;
 &lt;types&gt;
 &lt;xsd:schema&gt;
 &lt;xsd:import namespace="http://localhost:8080/TestWS_JAXWS" schemaLocation="CalculatorService_schema1.xsd"/&gt;
 &lt;/xsd:schema&gt;
 &lt;/types&gt;
 &lt;message name="add"&gt;
 &lt;part element="tns:add" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="addResponse"&gt;
 &lt;part element="tns:addResponse" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="divide"&gt;
 &lt;part element="tns:divide" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="divideResponse"&gt;
 &lt;part element="tns:divideResponse" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="multiply"&gt;
 &lt;part element="tns:multiply" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="multiplyResponse"&gt;
 &lt;part element="tns:multiplyResponse" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="subtract"&gt;
 &lt;part element="tns:subtract" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;message name="subtractResponse"&gt;
 &lt;part element="tns:subtractResponse" name="parameters"/&gt;
 &lt;/message&gt;
 &lt;portType name="CalculatorDelegate"&gt;
 &lt;operation name="add"&gt;
 &lt;input message="tns:add"/&gt;
 &lt;output message="tns:addResponse"/&gt;
 &lt;/operation&gt;
 &lt;operation name="divide"&gt;
 &lt;input message="tns:divide"/&gt;
 &lt;output message="tns:divideResponse"/&gt;
 &lt;/operation&gt;
 &lt;operation name="multiply"&gt;
 &lt;input message="tns:multiply"/&gt;
 &lt;output message="tns:multiplyResponse"/&gt;
 &lt;/operation&gt;
 &lt;operation name="subtract"&gt;
 &lt;input message="tns:subtract"/&gt;
 &lt;output message="tns:subtractResponse"/&gt;
 &lt;/operation&gt;
 &lt;/portType&gt;
 &lt;binding name="CalculatorPortBinding" type="tns:CalculatorDelegate"&gt;
 &lt;soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/&gt;
 &lt;operation name="add"&gt;
 &lt;soap:operation soapAction=""/&gt;
 &lt;input&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/input&gt;
 &lt;output&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/output&gt;
 &lt;/operation&gt;
 &lt;operation name="divide"&gt;
 &lt;soap:operation soapAction=""/&gt;
 &lt;input&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/input&gt;
 &lt;output&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/output&gt;
 &lt;/operation&gt;
 &lt;operation name="multiply"&gt;
 &lt;soap:operation soapAction=""/&gt;
 &lt;input&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/input&gt;
 &lt;output&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/output&gt;
 &lt;/operation&gt;
 &lt;operation name="subtract"&gt;
 &lt;soap:operation soapAction=""/&gt;
 &lt;input&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/input&gt;
 &lt;output&gt;
 &lt;soap:body use="literal"/&gt;
 &lt;/output&gt;
 &lt;/operation&gt;
 &lt;/binding&gt;
 &lt;service name="CalculatorService"&gt;
 &lt;port binding="tns:CalculatorPortBinding" name="CalculatorPort"&gt;
 &lt;soap:address location="http://localhost:8080/TestWS_JAXWS/CalculatorPort"/&gt;
 &lt;/port&gt;
 &lt;/service&gt;
&lt;/definitions&gt;</pre>
<p><strong>6.Launch SOAP Web Service Explore to make a test to the web sercie</strong></p>
<p>There are two Explores to be chosen. Select one.</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_30.png"><img class="alignnone size-medium wp-image-276" title="ws_30" src="http://kaisechen.files.wordpress.com/2010/07/ws_30.png?w=300&#038;h=107" alt="" width="300" height="107" /></a></p>
<p>Then click &#8216;WSDL Page&#8217; in right-top , then click &#8216;WSDL Main&#8217; in left-top</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_31.png"><img class="alignnone size-medium wp-image-277" title="ws_31" src="http://kaisechen.files.wordpress.com/2010/07/ws_31.png?w=300&#038;h=193" alt="" width="300" height="193" /></a></p>
<p>Input &#8220;http://localhost:8080/TestWS_JAXWS/CalculatorPort?WSDL&#8221;</p>
<p>Notice:</p>
<p>1) CalculatorPort  match the servlet-mapping in web.xml</p>
<p>2)<strong>?WSDL</strong></p>
<p>This is a universal query string argument that can be added to the end of any web service which will tell the web service to return it&#8217;s full WSDL to the caller. In this case, the WSDL is returned to our Web Services Explorer tool which loads it up, and displays the web services exposed operations to us.</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_32.png"><img class="alignnone size-medium wp-image-279" title="ws_32" src="http://kaisechen.files.wordpress.com/2010/07/ws_32.png?w=300&#038;h=258" alt="" width="300" height="258" /></a></p>
<p>click &#8216;add&#8217;, &#8216;divide&#8217;,'subtract&#8217;,'multiply&#8217; etc methods</p>
<p>e.g. add, input 100, 555, add go, then will get a return value:655</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_33.png"><img class="alignnone size-medium wp-image-280" title="ws_33" src="http://kaisechen.files.wordpress.com/2010/07/ws_33.png?w=300&#038;h=261" alt="" width="300" height="261" /></a></p>
<p>e.g. subtract, input 1000, 20 , will get 980</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_34.png"><img class="alignnone size-medium wp-image-281" title="ws_34" src="http://kaisechen.files.wordpress.com/2010/07/ws_34.png?w=300&#038;h=261" alt="" width="300" height="261" /></a></p>
<p><strong>7. Build a new Java project as the client to use the web service, which name is TestWS_JAXWSClient</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_35.png"><img class="alignnone size-medium wp-image-285" title="ws_35" src="http://kaisechen.files.wordpress.com/2010/07/ws_35.png?w=213&#038;h=300" alt="" width="213" height="300" /></a></p>
<p><strong>8. Build a new web service client in the project<br />
</strong></p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_36.png"><img class="alignnone size-medium wp-image-286" title="ws_36" src="http://kaisechen.files.wordpress.com/2010/07/ws_36.png?w=300&#038;h=124" alt="" width="300" height="124" /></a></p>
<p>Chose JAX-WS framework</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_37.png"><img class="alignnone size-medium wp-image-287" title="ws_37" src="http://kaisechen.files.wordpress.com/2010/07/ws_37.png?w=300&#038;h=295" alt="" width="300" height="295" /></a></p>
<p>In WSDL URL input &#8216;http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl&#8217;</p>
<p>Create a java package</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_38.png"><img class="alignnone size-medium wp-image-288" title="ws_38" src="http://kaisechen.files.wordpress.com/2010/07/ws_38.png?w=300&#038;h=293" alt="" width="300" height="293" /></a></p>
<p>It has  a WSDL validation, click finish button</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_39.png"><img class="alignnone size-medium wp-image-289" title="ws_39" src="http://kaisechen.files.wordpress.com/2010/07/ws_39.png?w=300&#038;h=294" alt="" width="300" height="294" /></a></p>
<p><strong>9. It automatically produces a lot of classes under new created java package</strong></p>
<p>It is very easy to understand the meaning of those classes from literal</p>
<p><a href="http://kaisechen.files.wordpress.com/2010/07/ws_40.png"><img class="alignnone size-full wp-image-290" title="ws_40" src="http://kaisechen.files.wordpress.com/2010/07/ws_40.png?w=235&#038;h=253" alt="" width="235" height="253" /></a></p>
<p><strong>10. Build a Test Class in the java client project</strong></p>
<pre>public class Test {</pre>
<pre>public static void main(String[] args) {
CalculatorService  cs = new CalculatorService();
CalculatorDelegate cd = cs.getCalculatorPort();</pre>
<pre>System.out.println("3 + 10    = "+cd.add(3,10));
System.out.println("999 - 222 = "+cd.subtract(999,222));
System.out.println("250 / 5   = "+cd.divide(250, 5));
System.out.println("7 * 23    = "+cd.multiply(7,23));
}
}</pre>
<p><strong>11. Run Test.java, it will get</strong></p>
<pre>3 + 10    = 13
999 - 222 = 777
250 / 5   = 50
7 * 23    = 161
<a href="http://kaisechen.files.wordpress.com/2010/07/ws_41.png"><img class="alignnone size-medium wp-image-294" title="ws_41" src="http://kaisechen.files.wordpress.com/2010/07/ws_41.png?w=300&#038;h=105" alt="" width="300" height="105" /></a>
</pre>
<div id="_mcePaste" style="overflow:hidden;position:absolute;left:-10000px;top:9408px;width:1px;height:1px;">package com.jack.ws;</p>
<p>import java.net.MalformedURLException;<br />
import java.net.URL;<br />
import java.util.logging.Logger;<br />
import javax.xml.namespace.QName;<br />
import javax.xml.ws.Service;<br />
import javax.xml.ws.WebEndpoint;<br />
import javax.xml.ws.WebServiceClient;</p>
<p>/**<br />
* This class was generated by the JAX-WS RI. JAX-WS RI 2.1.3-hudson-390-<br />
* Generated source version: 2.0<br />
* &lt;p&gt;<br />
* An example of how this class may be used:<br />
*<br />
* &lt;pre&gt;<br />
* CalculatorService service = new CalculatorService();<br />
* CalculatorDelegate portType = service.getCalculatorPort();<br />
* portType.add(&#8230;);<br />
* &lt;/pre&gt;<br />
*<br />
* &lt;/p&gt;<br />
*<br />
*/<br />
@WebServiceClient(name = &#8220;CalculatorService&#8221;, targetNamespace = &#8220;http://localhost:8080/TestWS_JAXWS&#8221;, wsdlLocation = &#8220;http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl&#8221;)<br />
public class CalculatorService extends Service {</p>
<p>private final static URL CALCULATORSERVICE_WSDL_LOCATION;<br />
private final static Logger logger = Logger<br />
.getLogger(com.jack.ws.CalculatorService.class.getName());</p>
<p>static {<br />
URL url = null;<br />
try {<br />
URL baseUrl;<br />
baseUrl = com.jack.ws.CalculatorService.class.getResource(&#8220;.&#8221;);<br />
url = new URL(baseUrl,<br />
&#8220;http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl&#8221;);<br />
} catch (MalformedURLException e) {<br />
logger<br />
.warning(&#8220;Failed to create URL for the wsdl Location: &#8216;http://localhost:8080/TestWS_JAXWS/CalculatorPort?wsdl&#8217;, retrying as a local file&#8221;);<br />
logger.warning(e.getMessage());<br />
}<br />
CALCULATORSERVICE_WSDL_LOCATION = url;<br />
}</p>
<p>public CalculatorService(URL wsdlLocation, QName serviceName) {<br />
super(wsdlLocation, serviceName);<br />
}</p>
<p>public CalculatorService() {<br />
super(CALCULATORSERVICE_WSDL_LOCATION, new QName(<br />
&#8220;http://localhost:8080/TestWS_JAXWS&#8221;, &#8220;CalculatorService&#8221;));<br />
}</p>
<p>/**<br />
*<br />
* @return returns CalculatorDelegate<br />
*/<br />
@WebEndpoint(name = &#8220;CalculatorPort&#8221;)<br />
public CalculatorDelegate getCalculatorPort() {<br />
return super.getPort(new QName(&#8220;http://localhost:8080/TestWS_JAXWS&#8221;,<br />
&#8220;CalculatorPort&#8221;), CalculatorDelegate.class);<br />
}</p>
<p>}</p>
</div>
<br /> Tagged: <a href='http://kaisechen.wordpress.com/tag/java/'>java</a>, <a href='http://kaisechen.wordpress.com/tag/myeclipse/'>myeclipse</a>, <a href='http://kaisechen.wordpress.com/tag/webservice/'>webservice</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kaisechen.wordpress.com/266/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kaisechen.wordpress.com/266/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kaisechen.wordpress.com/266/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kaisechen.wordpress.com&amp;blog=9601714&amp;post=266&amp;subd=kaisechen&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kaisechen.wordpress.com/2010/07/28/create-web-service-3-jax-ws/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/15112139c93a3268e6f940ef4ff02ae4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">kaisechen</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_21.png?w=271" medium="image">
			<media:title type="html">ws_21</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_22.png?w=300" medium="image">
			<media:title type="html">ws_22</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_23.png?w=293" medium="image">
			<media:title type="html">ws_23</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_24.png?w=291" medium="image">
			<media:title type="html">ws_24</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_25.png?w=300" medium="image">
			<media:title type="html">ws_25</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_26.png?w=300" medium="image">
			<media:title type="html">ws_26</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_27.png?w=253" medium="image">
			<media:title type="html">ws_27</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_281.png?w=212" medium="image">
			<media:title type="html">ws_28</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_29.png?w=300" medium="image">
			<media:title type="html">ws_29</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_30.png?w=300" medium="image">
			<media:title type="html">ws_30</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_31.png?w=300" medium="image">
			<media:title type="html">ws_31</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_32.png?w=300" medium="image">
			<media:title type="html">ws_32</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_33.png?w=300" medium="image">
			<media:title type="html">ws_33</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_34.png?w=300" medium="image">
			<media:title type="html">ws_34</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_35.png?w=213" medium="image">
			<media:title type="html">ws_35</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_36.png?w=300" medium="image">
			<media:title type="html">ws_36</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_37.png?w=300" medium="image">
			<media:title type="html">ws_37</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_38.png?w=300" medium="image">
			<media:title type="html">ws_38</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_39.png?w=300" medium="image">
			<media:title type="html">ws_39</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_40.png" medium="image">
			<media:title type="html">ws_40</media:title>
		</media:content>

		<media:content url="http://kaisechen.files.wordpress.com/2010/07/ws_41.png?w=300" medium="image">
			<media:title type="html">ws_41</media:title>
		</media:content>
	</item>
	</channel>
</rss>
