aspose file tools*
The moose likes Servlets and the fly likes Why is the servlet not available? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why is the servlet not available?" Watch "Why is the servlet not available?" New topic
Author

Why is the servlet not available?

Jeff Ma
Greenhorn

Joined: Feb 14, 2006
Posts: 6
[Tomcat error information]
[type] Status report
[message] Servlet tran is not available
[description] The requested resource (Servlet tran is not available) is not available.

[servlet source code]
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Tran extends HttpServlet {

public void init(ServletConfig config)
throws ServletException {
super.init(config);
try {
// Load the driver
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance( );
}
catch (ClassNotFoundException e) {
throw new UnavailableException(
"TransactionConnection.init( ) ClassNotFoundException: " +
e.getMessage( ));
}
catch (IllegalAccessException e) {
throw new UnavailableException(
"TransactionConnection.init( ) IllegalAccessException: " +
e.getMessage( ));
}
catch (InstantiationException e) {
throw new UnavailableException(
"TransactionConnection.init( ) InstantiationException: " +
e.getMessage( ));
}
}

public void doGet(
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter( );
out.println("<html>");
out.println("<head>");
out.println("<title>A Per Transaction Connection</title>");
out.println("</head>");
out.println("<body>");
Connection connection = null;
try {
// Establish a connection
connection = DriverManager.getConnection(
"jdbc racle:thin:@unioninn-jeffma:1521:test", "scott", "tiger");
}
catch (SQLException e) {
throw new UnavailableException(
"TransactionConnection.init( ) SQLException: " +
e.getMessage( ));
}
Statement statement = null;
ResultSet resultSet = null;
String userName = null;
try {
// Test the connection
statement = connection.createStatement( );
resultSet = statement.executeQuery(
"select initcap(user) from sys.dual");
if (resultSet.next( ))
userName = resultSet.getString(1);
}
catch (SQLException e) {
out.println(
"TransactionConnection.doGet( ) SQLException: " +
e.getMessage( ) + "<p>");
}
finally {
if (resultSet != null)
try { resultSet.close( ); } catch (SQLException ignore) { }
if (statement != null)
try { statement.close( ); } catch (SQLException ignore) { }
}
if (connection != null) {
// Close the connection
try { connection.close( ); } catch (SQLException ignore) { }
}
out.println("Hello " + userName + "!<p>");
out.println("You're using a per transaction connection!<p>");
out.println("</body>");
out.println("</html>");
}

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

[setting of web.xml]
<servlet>
<servlet-name>tran</servlet-name>
<servlet-class>Tran</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>tran</servlet-name>
<url-pattern>/tran/*</url-pattern>
</servlet-mapping>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Bear,
I'm always delighted of your warm-hearted commments.
I'll delete this post, which was, indeed, not correct.

"Il n'y a que les imbeciles qui ne se trompent jamais."
Only fools are always right.
Are you ?
[ February 14, 2006: Message edited by: Satou kurinosuke ]

[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Originally posted by Satou kurinosuke:
Using mappings like "/tran/*" needs the Invoker to be enabled (disabled by default).
Either enable it, or use a more explicit mapping, like "/tran/Tran" (recommended)


That is not correct. There is nothing wrong with that mapping. I use similar mappings all the time.

It's more likely that an exception is being thrown in the init method.
[ February 14, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Ma
Greenhorn

Joined: Feb 14, 2006
Posts: 6
I move the following code segment to doGet()
try {
// Load the driver
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance( );
}
catch (Exception e) {
throw new UnavailableException(
"TransactionConnection.init( ) Exception: " +
e.getMessage( ));
}

then I found the following information in tomcat logs

2006-02-15 11:26:18 StandardWrapperValve[tran]: Servlet.service() for servlet tran threw exception
javax.servlet.UnavailableException: TransactionConnection.init( ) Exception: oracle.jdbc.driver.OracleDriver
at Tran.doGet(Tran.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Originally posted by Satou kurinosuke:
Bear,
I'm always delighted of your warm-hearted commments.


And how approriate for Valentine's Day!


"Il n'y a que les imbeciles qui ne se trompent jamais."
Only fools are always right.
Are you ?


Hardly! "He wept, for he realized that without mistakes, there was nothing left to learn."
Jeff Ma
Greenhorn

Joined: Feb 14, 2006
Posts: 6
what?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

what?

It was about my post
Bruno Boehr
Greenhorn

Joined: Feb 15, 2006
Posts: 17
Jeff Ma,

From what I see it seems that the error occurs when you try to instantiate the Oracle JDBC driver. Unfortunately, when you catch and re-throw the exception, you lose track of its origin. As a result, lots of information that otherwise might be very helpful in troubleshooting the issue is gone.

What you can do is call the initCause(Throwable t) method to wrap an instance of UnavailableException around the original one:

catch (Exception e) {
UnavailableException ue = new UnavailableException(
"TransactionConnection.init( ) Exception: " +
e.getMessage( ));
ue.initCause(e);
throw ue;
}

or simply re-throw it as a ServletException (which, unlike UnavailableException, has a convenience constructor that takes a Throwable):

catch (Exception e) {
throw new ServletException(
"TransactionConnection.init( ) Exception: " +
e.getMessage( ), e);
}

When you put these changes in place, you will most likely find out that you are getting a ClassNotFoundException. To fix it, make sure your Oracle driver is either under your WEB-INF/lib or somewhere on the classpath.

Oh, and if you are using classes12.zip, some containers may not identify it as a Java archive. Your best bet in this case would be to rename it to something that has a .jar extension, like oracle.jar.

Hope this helps.


<a href="http://webjavenue.com/" target="_blank" rel="nofollow">Your first website in Java: easier than you think</a>
Jeff Ma
Greenhorn

Joined: Feb 14, 2006
Posts: 6
Bruno Boehr,thank u very much!

I copy classes12.jar to WEB-INF\lib, that problem is resovled.
And I learn from u the method of debugging.
Thank u!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why is the servlet not available?