I am using Eclipse, Oracle 10g and Apache Tomcat. This is my 1st sample application, so I am trying to print table only. But it’s not working.
This is my source code. testtable.java
package com.sla;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class newuser_db
*/
public class testtable extends HttpServlet {
private static final long serialVersionUID = 1L;
PreparedStatement stmt=null;
Connection con =null;
ResultSet rs=null;
/**
* @see HttpServlet#HttpServlet()
*/
public testtable() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
try
{
Class.forName("sun.Jdbc.Odbc.JdbcOdbcDriver");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
try
{
con =DriverManager.getConnection("Jdbc:odbc:servletdb","system","mahes12345");
}
catch(SQLException ex)
{
ex.printStackTrace();
}
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.service(arg0, arg1);
PrintWriter out=arg1.getWriter();
out.println("<html>" +
"<head>" +
"<table border='3' bordercolor='red'>" +
"<tr><th>FKUserId</th><" +
"th>IncNo</th>" +
"<th>Summary</th>" +
"<th>Status</th>" +
"<th>Priority</th>" +
"<th>IncidetDate</th>" +
"<th>ReportDate</th>" +
"<th>Reopen</th>" +
"<th>Attachment</th>" +
"<th>RequestMode</th>" +
"</tr><tr></tr>" +
"</table></head></html>");
}
@Override
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
try
{
if(stmt!=null)
stmt.close();
stmt=null;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
try
{
if(con!=null)
con.close();
con=null;
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>testtable</display-name>
<servlet>
<description>
</description>
<display-name>testtable</display-name>
<servlet-name>testtable</servlet-name>
<servlet-class>
com.sla.testtable</servlet-class>
</servlet>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
error
HTTP Status 404 - /testtable/servlet/com.sla.testtable
--------------------------------------------------------------------------------
type Status report
message /testtable/servlet/com.sla.testtable
description The requested resource (/testtable/servlet/com.sla.testtable) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/5.5.28
How is this problem caused and how can I solve it?
You forgot to map the servlet on an URL. Add the following below the
<servlet>of yourweb.xml.This way it’s available by http://localhost:8080/testtable/yourservleturl. You can change
/yourservleturlto whatever you want.See also:
Unrelated to the concrete problem: your servlet is not thread safe. Also, your servlet will crash whenever the webapp runs longer than the DB connection is allowed to be kept open according the DB server. Fix that accordingly as well.