【问题标题】:I'm having trouble understanding how to get PrintWriter to print to my JSP from my Bean我无法理解如何让 PrintWriter 从我的 Bean 打印到我的 JSP
【发布时间】:2021-08-03 04:19:07
【问题描述】:

我是一个初学者,这是一个学校作业,但我的问题不是关于作业的功能方面。

我必须使用 Java bean 和 JSP 来执行各种数据库功能,例如创建表、更新表、删除表等。我的所有页面都正常工作,但是我想在一个方法时向页面输出一条消息是否运行,取决于操作是否成功。

现在我有错误/成功消息通过System.out.println() 发送到控制台,它工作正常,但我希望它显示在页面上,以便用户查看操作是否成功。我一直在网上寻找,似乎我需要使用 PrintWriter out = response.getWriter();这样我就可以使用out.println(); 将我的成功/错误消息发送到 JSP 而不是控制台。

我导入了 java.io.PrintWriter,当我添加 PrintWriter out = response.getWriter();对于我的一种方法,我得到“无法解决响应。”

在网上看到有人说要创建一个参数'response',这是Eclipse中的聪明建议之一。当我这样做时,它会将(ServletResponse 响应)作为参数添加到我的方法中,并且我必须包含一个“抛出 IOException”声明。那么问题是我的 JSP 不会运行该方法,因为我没有向它传递参数。

所以我尝试的下一件事是在方法中声明响应变量(另一个 Eclipse 建议)。我单击建议的修复程序,它会添加 ServletResponse 响应;

现在我得到变量需要初始化的错误,这是有道理的。但是当我让 Eclipse 为我初始化它时,它会设置 response = null;

此时它清除了错误,但是当我尝试运行它时,我得到:

java.lang.NullPointerException:无法调用 "javax.servlet.ServletResponse.getWriter()"

因为“响应”为空

我不确定我是否缺少一些简单的东西,或者我只是误解了使用 PrintWriter 的整个概念。我已经看到了几篇解决这个问题的帖子,但它们都不适用于我正在尝试做的事情。

我将只包括 DROP TABLE 方法和 JSP,因为它是最简单的方法。

这是我的 Bean 代码:

    // This drop table method can be used to clear the tables
    public void dropTable() {
    
        Connection con = null;
        Statement stmt = null;
        
        // Attempt to connect to the database       
        try{

            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student1","pass");
            stmt = con.createStatement();
        }
        
        // Catch any exceptions and display a failure message before exiting the program    
        catch(Exception e){
    
            System.out.println(e);      
            System.exit(0);                 
        }

        // Attempt to DROP the tables if they exist
        try {
            
            stmt.executeUpdate("DROP TABLE CUSTOMERS");
            stmt.executeUpdate("DROP TABLE ITEMS");
            
            System.out.println("Tables Dropped");
            
            //Close the statement and connection
            stmt.close();
            con.close();    
        }
        
        // Catch any exceptions
        catch (SQLException e) {
            
            System.out.println(e);
        }       
    }

这是我非常简单的 JSP,带有一个按钮,当按下该按钮时,将调用指向同一 JSP 并从我的 Bean 运行 dropTable() 方法的 POST 方法。

<%@ page import = "java.io.*,java.util.*,java.sql.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %> 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<!-- Create the main HTML for the page -->
<!DOCTYPE html>
<html>
    <head>
        <title>Drop Tables</title>
    </head>
    <body>
        <div>
        <h1>Click the button to drop the CUSTOMERS and ITEMS tables</h1>
            <form method="post">
                <input type='submit' value='Drop'/>
            </form>
        <br>
    
        <!-- Invoke and name the JavaBean to be used -->
        <jsp:useBean id='DBBean' class='bubsBeans.DBBean' />
    
        <!-- Java code to determine if the post method has been invoked through the submit button click -->
        <%
        
        // If the POST method is invoked invoke the dropTable method from the bean
        if(request.getMethod().equals("POST")){     
            
            DBBean.dropTable();
        }
        %>
    
        </div>
    </body>
</html>

【问题讨论】:

  • 我只能说你这个问题问得很好。您准确地发布了您遇到的问题以及所有相关代码。干得好!
  • 谢谢!我试图提供尽可能多的信息和背景,因为那里有很多类似但不完全是我要问的问题。我知道当我尝试解决问题时,尽可能多地获取信息会有所帮助。

标签: java jsp javabeans


【解决方案1】:

response 对象在您的 JSP 中隐式声明。因此,您可以在 JSP 中的任何位置使用类似代码 &lt;% response.getWriter().println("Debugging Code"); %&gt; 的内容。但是 servlet 只是一个普通的 Java 类,所有东西都需要显式声明。如果你想使用response,它作为一个参数传递给你的doPostdoGet方法作为HttpServletResponse

【讨论】:

  • 谢谢!它并没有完全解决我的问题,但它帮助我理解我在这个概念上哪里出了问题,并且确实导致了解决方案。我现在正在更新我的问题以反映我的修复。
【解决方案2】:

根据 Yserbius 的回复,我知道我在使用 getWriter 时出了什么问题。这是我的解决方案,以防其他人遇到此问题并需要帮助。

我在方法中初始化了一个新的 String 变量并将我的消息分配给该变量。然后我改变了我的方法来返回那个字符串而不是 void。

这样,在我的 JSP 中,我可以将方法的返回值分配为字符串,它将执行数据库函数(或抛出异常)并将正确的消息返回给我的 JSP 字符串变量。

然后我可以使用out.print(messageVariable);将消息打印到页面。

就像我在 Java 中苦恼的大多数事情一样,这是一个简单的解决方案,我反复查看。

这是我的新方法代码:

// This drop table method can be used to clear the table
    public String dropTable() {
    
        Connection con = null;
        Statement stmt = null;
        
        String message;
        
        // Attempt to connect to the database       
        try{

            DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "student1","pass");
            stmt = con.createStatement();
        }
        
        // Catch any exceptions and display a failure message before exiting the program    
        catch(Exception e){
    
            message = "Error connecting to database.";
            System.exit(0);                 
        }

        // Attempt to DROP the table if it exists
        try {
            
            stmt.executeUpdate("DROP TABLE CUSTOMERS");
            stmt.executeUpdate("DROP TABLE ITEMS");
            
            message = "Tables Dropped.";
            
            //Close the statement and connection
            stmt.close();
            con.close();    
        }
        
        // Catch any exceptions
        catch (SQLException e) {
            
            message = e.toString();
        }
        return message;
    }

这是我的新 JSP 代码的相关部分:

<%
        // If the POST method is invoked, invoke the dropTable method from the bean
        if(request.getMethod().equals("POST")){     
            
            String message = DBBean.dropTable();
            
            out.print(message);
        }
%>

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2019-10-11
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多