Fortunately, there is one way to handle the static generation of the code. Looking at the previous listing, you will see tags such as the following:
<XDtMerge:merge file="local-custom.xdt"> </XDtMerge:merge>
These tags represent merge points where the code generator will try to find the specified file, pull in all of the code verbatim from the file, and place it in the resulting source code.
To use the merge points, just place the code you want to have
merged into the specified file. There are
XDoclet is a technology that allows developers to increase their
productivity by automatically generating code for many of the
mundane tasks involved in EJB, Hibernate, or other technology
development. When the XDoclet technology was first created, EJB
development was its driving factor simply because of the numerous
files needed for every EJB created. Clearly the developmental
advantages of XDoclet caught on because
If you take a look at the current Internet software development
arena, a good amount of the heavy
We will use a step-by-step process to show how to use XDoclet with servlets by creating a simple servlet and deploying it on an application server. For this example, we will be using Resin as the application server. First we will create a simple servlet, as shown inthe following listing. This servlet will query an account database, display the information in a table, and output some other simple text.
package example;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sgl.*;
import javax.naming.*;
public class SimpleServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
protected void handleUser(HttpServletRequest request,
HttpServletResponse response)
throws ServletException }
try {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My Simple Servlet" ),
out.println("</head>");
out.println("<body>");
out.println("The current database is Production<br>");
out.println("The username is 'accounts'<br><br>");
displayTable(out);
out.print In ("</body>");
out.println("</html>");
out.close();
} catch (Exception e) {
throw new ServletException (e);
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
handleUser(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
handleUser(request, response);
}
private void displayTable(java.io.PrintWriter out)
throws Exception {
Object obj = new InitialContext().lookup("java:comp/en v/jdbc/dbconnection");
DataSource pool = (DataSource) obj;
if (pool != null) {
Connection connection = pool.getConnetion();
out.println("<table>");
out.println("<tr><td>description</td><td>price</td></tr>" );
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(
"SELECT * FROM production WHERE username="accounts");
while(rs.nextQ) {
out.println("<tr><td>" + rs.getString("description") +"</td><td>" +
rs.getString(`price") + "</td></tr>");
}
} finally {
connection.close();
}
out.println("</table>");
}
}
}
For this servlet we will need to create a web.xml deployment descriptor, as shown in the following listing.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.
//DTD Web Application 2.3//EN" http://Iava.sun.comldtd/web-app 2 3.dtd>
<web-app>
<servlet servlet-name="SimpleServlet"
servlet-class="example. SimpleServlet"
/>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/Example/*</url-pattern >
</servlet-mapping>
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>SimpleServlet</url-pattern>
</servlet-mapping>
<resource-ref>
<description>JDBC Connection</description>
<res-ref-name>jdbc/dbconnection</res-ref-name>
<res-type>javax.sgl.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
If we deploy the code in the two previous listings on an application server and place the appropriate database table in our database, we will see an output page with the contents of the database table. Now, let's consider what the code is doing, even though it is just an example.
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}The primary purpose of this code example is to pull various rows from a database table based on an account name. During a production run, we want the table used to be called Production and have a username of Accounts. However, during development or testing work, we don't want to mess with trying to find the appropriate places within the code to make the changes. Instead, we'd like a common location.
Even more important, the deployment descriptor must be
package example;
/**
* @version 0.5
* @web.servlet name="SimpleServlet"
* display-name="Simple Servlet"
* load-on-startup=" l"
*
* @web-servlet-init-param name="table" value="production"
* @web-servlet-init-param name="account" value="accounts"
*
* @web.resource-ref description="database connection"
* name="jdbc/dbconnection"
* type="javax.sql.DataSource"
* auth="Container"
*
* @web.servlet-mapping url-pattern ="/Example/*"
* @web.servlet-mapping url-pattern ="/SimpleServlet"
*/
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sgl.*;
import java.sgl.*;
import javax.naming.*;
public class SimpleServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
protected void handleUser(HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
try {
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>My Simple Servlet");
out.println("</head>");
out.println("<body>");
out.println("The current database is Production<br>");
out.println("The username is `accounts'<br><br>");
displayTable(out);
out.println("</body>");
out.println("</html>");
out.close();
} catch (Exception e) {
throw new ServletException (e);
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
handleUser(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
handleUser(request, response);
}
private void displayTable(java.io.PrintWriter out)
throws Exception {
ServletConfig config = this.getServletConfig();
String table = config.getInitParameter("table");
String username = config.getln1tParameter("account");
Object obj = new InitialContext().lookup("java:comp/env/jdbc/dbconnection");
DataSource pool = (DataSource) obj;
if (pool != null) {
Connection connection = pool.getConnection();
out.println("<table>");
out.print ln("<tr><td>description</td><td>price</td><Itr>");
try {
Statement statement = connection.createStatementO;
ResultSet rs = statement.executeQuery(
"SELECT * FROM " + table + " WHERE username="' + username + ""');
while(rs.next()) {
out.println("<tr><td>" + rs.getString("description") +"</td><td>" +
rs.getString("price") + "</td></tr>");
}
} finally {
connection.close();
}
out.println("</table>");
}
}
}
There are two primary changes in this listing. The first is the comment section, and we will discuss that section in a moment. The second change occurred in the displayTable() method. In the function, we now pull the table and account name to use in the database query directory from the deployment descriptor because they will be declared as init parameters.