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

发布于 2024-12-08 00:58:03 字数 5281 浏览 1 评论 0原文

我正在使用 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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-12-15 00:58:03

您忘记将 servlet 映射到 URL。在 web.xml 下面添加以下内容。

<servlet-mapping>
    <servlet-name>testtable</servlet-name>
    <url-pattern>/yourservleturl</url-pattern>
</servlet-mapping>

这样,它就可以通过 http://localhost:8080/testtable/yourservleturl 获得。您可以将 /yourservleturl 更改为您想要的任何内容。

另请参阅:


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

You forgot to map the servlet on an URL. Add the following below the <servlet> of your web.xml.

<servlet-mapping>
    <servlet-name>testtable</servlet-name>
    <url-pattern>/yourservleturl</url-pattern>
</servlet-mapping>

This way it's available by http://localhost:8080/testtable/yourservleturl. You can change /yourservleturl to 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文