Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database access with servlet

 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI guys !
I trying to access database through my servlet. I am using Visual age for Java. I am using access database for which I have created a dsn on my machine.
But this the error I am getting on invoking the servlet
<b>
Error 405
An error has occured while processing request:http://localhost:8080/servlet/aetna.DBServlet
Message: HTTP method GET is not supported by this URL
Target Servlet: aetna.DBServlet
StackTrace:
--------------------------------------------------------------------------------
Root Error-1: HTTP method GET is not supported by this URL
com.ibm.servlet.engine.webapp.WebAppErrorReport: HTTP method GET is not supported by this URL
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
javax.servlet.ServletException(java.lang.String)
com.ibm.websphere.servlet.error.ServletErrorReport(java.lang.String)
com.ibm.servlet.engine.webapp.WebAppErrorReport(java.lang.String)
void com.ibm.servlet.engine.webapp.WebAppDispatcherResponse.sendError(int, java.lang.String)
void javax.servlet.http.HttpServlet.doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictServletInstance.doService(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.IdleServletState.service(com.ibm.servlet.engine.webapp.StrictLifecycleServlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.ServletInstance.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(com.ibm.servlet.engine.webapp.ServletInstanceReference, javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(com.ibm.servlet.engine.webapp.WebAppRequest, javax.servlet.http.HttpServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.InvokerServlet.service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
void javax.servlet.http.HttpServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictServletInstance.doService(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.IdleServletState.service(com.ibm.servlet.engine.webapp.StrictLifecycleServlet, javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.webapp.ServletInstance.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(com.ibm.servlet.engine.webapp.ServletInstanceReference, javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, com.ibm.servlet.engine.webapp.WebAppServletInvocationEvent)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(com.ibm.servlet.engine.webapp.WebAppRequest, javax.servlet.http.HttpServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(javax.servlet.ServletRequest, javax.servlet.ServletResponse, boolean)
void com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
void com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(java.lang.Object)
void com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(java.lang.Object)
void com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(java.lang.String, com.ibm.servlet.engine.srp.ISRPConnection)
void com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(com.ibm.servlet.engine.oselistener.api.IOSEConnection)
void com.ibm.servlet.engine.http_transport.HttpTransportHandler.handleConnection(java.net.Socket)
void com.ibm.servlet.engine.http_transport.HttpTransportHandler.run()
void java.lang.Thread.run()
</b>
This is my code:
<b><i>
public void performTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String user="n017710";
String pwd ="190103";
String dsn = "jdbc dbc:mydsn";
response.setContentType("text/html");
PrintWriter out = response.getWriter();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dsn,user,pwd);

stmt = con.createStatement();

rs = stmt.executeQuery("Select * from employees");
out.println("<html><head><title>Employees</title></head>");
out.println("<body>");
while(rs.next()) {
out.println("<b>" + rs.getString("Name") + " " + rs.getString("password"));
}
out.println("</b></body></html>");
}catch(ClassNotFoundException e){
out.println("COuldn't load database driver " + e);
}catch(SQLException e){
out.println("SQL Exception caught: "+ e);
}finally {
try {
if(con != null)
con.close();
}catch (SQLException e){
System.out.println(e);
}
}
}
</b></i>
Waiting for replies ....
thnx Manoj
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I missing something here? I did not see where you are overiding the GET or POST method.

Bosun
 
Manoj Singh
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The full code is given below:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Creation date: (09/06/2001 9:44:11 AM)
* @author: Manoj Singh
*/
public class DBServlet extends HttpServlet {

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

}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(HttpServletRequest request, HttpServletResponse response) throws IOException {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String user="n017710";
String pwd ="190103";
String dsn = "jdbc dbc:mydsn";
response.setContentType("text/html");
PrintWriter out = response.getWriter();

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(dsn,user,pwd);

stmt = con.createStatement();

rs = stmt.executeQuery("Select * from employees");
out.println("<html><head><title>Employees</title></head>");
out.println("<body><b>");
//while(rs.next()) {
//out.println(rs.getString("Name"));
//}
out.println("</b></body></html>");
}catch(ClassNotFoundException e){
out.println("COuldn't load database driver " + e);
}catch(SQLException e){
out.println("SQL Exception caught: "+ e);
}finally {
try {
if(con != null)
con.close();
}catch (SQLException e){
System.out.println(e);
}
}

}


}
 
Tim Holloway
Saloon Keeper
Posts: 18304
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd check the web.xml file. It looks like the server implementation's HttpServlet.doGet method may be checking for authority and finding that it's denied so it throws an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic