错误:如何使用servlet打印表?

时间:2011-10-05 19:20:25

标签: jsp servlets

我正在使用Eclipse,Oracle 10g和Apache Tomcat。这是我的第一个示例应用程序,所以我只想打印表。但它不起作用。

这是我的源代码。 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

    }


}

这是我的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>

错误

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

这个问题是如何引起的?如何解决?

1 个答案:

答案 0 :(得分:2)

您忘记在URL上映射servlet。在<servlet>的{​​{1}}下方添加以下内容。

web.xml

这种方式由http://localhost:8080/testtable/yourservleturl提供。您可以将<servlet-mapping> <servlet-name>testtable</servlet-name> <url-pattern>/yourservleturl</url-pattern> </servlet-mapping> 更改为您想要的任何内容。

另见:


无关具体问题:您的servlet不是线程安全的。此外,只要Web应用程序运行的时间超过允许根据数据库服务器保持打开的数据库连接,您的servlet就会崩溃。相应地修复它。