• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

my servlet can't initialize: null

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
i am a newer with jboss and mysql and eclipse , i did the following steps to make my servlet connect to mysql :
1.create mysql data base [db.name]
2.copy mm.mysql-2.0.14-bin.jar to [jboss.dir]/server/default/lib
3. server/default/conf/standardjbosscmp-jdbc.xml, replace the Hyper SQL with these 2:
<datasource>java:/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
4. server/default/conf/standardjaws.xml, replace the Hyper SQL with these 2:
<datasource>java:/MySqlDS</datasource>
<type-mapping>mySQL</type-mapping>
5. copy docs/examples/jca/mysql-service.xml into /server/default/deploy
6. update mysql-service.xml with your MySql login/password, and use the following:
<?xml version="1.0" encoding="UTF-8"?>
<!-- ===================================================================== -->
<!-- -->
<!-- JBoss Server Configuration -->
<!-- -->
<!-- ===================================================================== -->
<server>
<!-- ==================================================================== -->
<!-- New ConnectionManager setup for mysql using 2.0.11 driver -->
<!-- Build jmx-api (build/build.sh all) and view for config documentation -->
<!-- ==================================================================== -->
<mbean code="org.jboss.resource.connectionmanager.LocalTxConnectionManager" name="jboss.jca:service=LocalTxCM,name=MySqlDS">

<!-- Include a login module configuration named MySqlDbRealm.
Update your login-conf.xml, here is an example for a
ConfiguredIdentityLoginModule:
<application-policy name = "MySqlDbRealm">
<authentication>
<login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required">
<module-option name = "principal">yourprincipal</module-option>
<module-option name = "userName">yourusername</module-option>
<module-option name = "password">yourpassword</module-option>
<module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=MySqlDS</module-option>
</login-module>
</authentication>
</application-policy>
NOTE: the application-policy name attribute must match SecurityDomainJndiName, and the
module-option name = "managedConnectionFactoryName"
must match the object name of the ConnectionManager you are configuring here.
-->
<!--uncomment out this line if you are using the MySqlDbRealm above
<attribute name="SecurityDomainJndiName">MySqlDbRealm</attribute>
-->
<depends optional-attribute-name="ManagedConnectionFactoryName">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.RARDeployment" name="jboss.jca:service=LocalTxDS,name=MySqlDS">
<attribute name="JndiName">MySqlDS</attribute>
<attribute name="ManagedConnectionFactoryProperties">
<properties>
<config-property name="ConnectionURL" type="java.lang.String">jdbc:mysql://localhost:3306/partdb</config-property>
<config-property name="DriverClass" type="java.lang.String">org.gjt.mm.mysql.Driver</config-property>
<!--set these only if you want only default logins, not through JAAS -->
<config-property name="userName" type="java.lang.String">test</config-property>
<config-property name="password" type="java.lang.String">test</config-property>
</properties>
</attribute>
<!--Below here are advanced properties -->
<!--hack-->
<depends optional-attribute-name="OldRarDeployment">jboss.jca:service=RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name="ManagedConnectionPool">
<!--embedded mbean-->
<mbean code="org.jboss.resource.connectionmanager.JBossManagedConnectionPool" name="jboss.jca:service=LocalTxPool,name=MySqlDS">
<attribute name="MinSize">0</attribute>
<attribute name="MaxSize">50</attribute>
<attribute name="BlockingTimeoutMillis">5000</attribute>
<attribute name="IdleTimeoutMinutes">15</attribute>
<!--criteria indicates if Subject (from security domain) or app supplied
parameters (such as from getConnection(user, pw)) are used to distinguish
connections in the pool. Choices are
ByContainerAndApplication (use both),
ByContainer (use Subject),
ByApplication (use app supplied params only),
ByNothing (all connections are equivalent, usually if adapter supports
reauthentication)-->
<attribute name="Criteria">ByContainer</attribute>
</mbean>
</depends>
<depends optional-attribute-name="CachedConnectionManager">jboss.jca:service=CachedConnectionManager</depends>
<depends optional-attribute-name="JaasSecurityManagerService">jboss.security:service=JaasSecurityManager</depends>
<attribute name="TransactionManager">java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment-->
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>

</server>
7. restart jboss
8. write my servlet within eclipse
package jdbctest;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.io.*;
/**
* @author Administrator
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class Query extends HttpServlet {
Connection db; // This is the shared JDBC database connection
public void init() throws ServletException {
// Read initialization parameters from the web.xml file
ServletConfig config = getServletConfig();
String driverClassName = config.getInitParameter("DriverClass");
String url = config.getInitParameter("ConnectionURL");
String username = config.getInitParameter("username");
String password = config.getInitParameter("password");

// Use those init params to establish a connection to the database
// If anything goes wrong, log it, wrap the exception and re-throw it
try {
Class.forName(driverClassName);
db = DriverManager.getConnection(url, username, password);
}
catch (Exception e) {
log("Can't create DB connection", e);
throw new ServletException("Query: can't initialize: " +
e.getMessage(), e);
}
}
/** Close the database connection when the servlet is unloaded */
public void destroy() {
try { db.close(); } // Try to close the connection
catch (SQLException e) {} // Ignore errors; at least we tried!
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{

response.setContentType("text/html"); // We're outputting HTML
PrintWriter out = response.getWriter(); // Where to output it to
// Output document header and a form for entering SQL queries
// When the form is submitted, this servlet is reloaded
out.println("<head><title>DB Query</title></head>\n" +
"<body bgcolor=white><h1>DB Query</h1>\n" +
"<form><b>Query: </b><input name='q'>" +
"<input type=submit></form>");
// See if a query was specified in this request.
String query = request.getParameter("q");
if (query != null) {
// display the query text as a page heading
out.println("<h1>" + query + "</h1>");
// Now try to execute the query and display the results in a table
Statement statement = null; // An object to execute the query
try {
// Create a statement to use
statement = db.createStatement();
// Use it to execute the specified query, and get result set
ResultSet results = statement.executeQuery(query);
// Ask for extra information about the results
ResultSetMetaData metadata = results.getMetaData();
// How many columns are there in the results?
int numcols = metadata.getColumnCount();
// Begin a table, and output a header row of column names
out.println("<table border=2><tr>");
for(int i = 0; i < numcols; i++)
out.print("<th>" + metadata.getColumnLabel(i+1) + "</th>");
out.println("</tr>");
// Now loop through the "rows" of the result set
while(results.next()) {
// For each row, display the the values for each column
out.print("<tr>");
for(int i = 0; i < numcols; i++)
out.print("<td>" + results.getObject(i+1) + "</td>");
out.println("</tr>");
}
out.println("</table>"); // end the table
}
catch (SQLException e) {
// If anything goes wrong (usually a SQL error) display the
// error to the user so they can correct it.
out.println("SQL Error: " + e.getMessage());
}
finally { // Whatever happens, always close the Statement object
try { statement.close(); }
catch(Exception e) {}
}
}

// Finally, end the HTML output
out.println("</body>");
}
}
9- write web.xml
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>
<servlet>
<servlet-name>Query</servlet-name>
<display-name>This is the display name of my J2EE component</display-name>
<description>This is the description of my J2EE component</description>
<servlet-class>jdbctest.Query</servlet-class>

<init-param>
<param-name>ConnectionURL</param-name>
<param-value>jdbc:mysql://localhost:3306/partdb</param-value>
</init-param>

<init-param>
<param-name>DriverClass</param-name>
<param-value>org.gjt.mm.mysql.Driver</param-value>
</init-param>

<init-param>
<param-name>userName</param-name>
<param-value>test</param-value>
</init-param>

<init-param>
<param-name>password</param-name>
<param-value>test</param-value>
</init-param>

</servlet>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>404</error-code>
<location>error.jsp</location>
</error-page>
</web-app>
10- deploy my servlet and run it by
http://localhost:8080/webjdbc/servlet/jdbctest.Query
and got the following error
HTTP ERROR: 503 javax.servlet.ServletException: Query: can't initialize: null
pls tell me what is the wrong?
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic