gpt4 book ai didi

java - 接收 java.lang.Object 作为输入的方法不应该也接收 javax.servlet.jsp.JspWriter 作为输入吗?

转载 作者:行者123 更新时间:2023-11-29 08:17:06 25 4
gpt4 key购买 nike

我想 consolidate two functions .

在找到可行的解决方案后,我决定进一步研究代码,并得出以下结论:

package hu.flux.helper;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.servlet.jsp.JspWriter;
import com.objectmentor.library.web.framework.mocks.*;


// A holder for formatting data
public class NameAndAddress
{
public String firstName;
public String middleName;
public String lastName;
public String address1;
public String address2;
public String city;
public String state;
public String zip;

public String FormattedString()
{
String formattedString = "<PRE>\n" + firstName;

// Add the middle name only if it contains data.
if ((middleName != null) && (middleName.length() > 0))
{formattedString += " " + middleName;}

formattedString += " " + lastName + "\n";

formattedString += address1 + "\n";

if ((address2 != null) && (address2.length() > 0))
formattedString += address2 + "\n";

formattedString += city + ", " + state + " " + zip + "\n</PRE>";
return formattedString;
}

// Print out the name and address.
public void print(Writer writer) {
long now = System.currentTimeMillis();
System.out.println("--Entering-- " + now);
PrintWriter p = new PrintWriter (writer);
p.write(this.FormattedString());
now = System.currentTimeMillis();
System.out.println("--Exiting-- " + now);
}

/*
public void print(JspWriter out) throws java.io.IOException
{ print (new PrintWriter(out)); }
*/

@SuppressWarnings("deprecation")
public static void main (String args[])
{
NameAndAddress naa = new NameAndAddress();
naa.firstName = "Brian";
naa.middleName = "Matthew";
naa.lastName = "Kessler";
naa.address1 = "Tatra u. 15/b V/3";
naa.city = "Budapest";
naa.state = "Hungary";
naa.zip = "HU-1136";

System.out.println("\nTesting PrintWriter...");
PrintWriter p = null;
try { p = new PrintWriter("d:/temp/pwriter_text.txt"); }
catch (FileNotFoundException e)
{
System.err.print ("Can not create new PrintWriter: " + e);
e.printStackTrace();
}
naa.print(p);
p.flush();

FileInputStream fis;
DataInputStream dis;
try
{
fis = new FileInputStream("d:/temp/pwriter_text.txt");
dis = new DataInputStream (fis);
while (dis.available() != 0)
{ System.out.println(dis.readLine()); }
dis.close();
}
catch (Exception e)
{
System.err.println("File input error");
}


System.out.println("\nTested PrintWriter...");
System.out.println("---------------------");

System.out.println("\nTesting JSPWriter...");
JspWriter j = null;
naa.print(j);
System.out.print("\nTested JSPWriter...");
System.out.println("---------------------");

System.out.println("\nTesting MockJspWriter");
MockJspWriter m = null;
m = new MockJspWriter(255, true);
naa.print(m);
System.out.print(m.getContent());
System.out.println("\nTested MockJSPWriter...");
System.out.println("---------------------");
}
}

我预计 print() 方法会捕获 JspWriter 和 PrintWriter。

虽然此解决方案适用于 PrintWriter,但当我尝试将其作为控制台应用程序运行时,我得到了以下输出:

Testing PrintWriter...
--Entering--
--Exiting--
<PRE>
Brian Matthew Kessler
Tatra u. 15/b V/3
Budapest, Hungary HU-1136
</PRE>

Tested PrintWriter...
---------------------

Testing JSPWriter...
--Entering--
Exception in thread "main" java.lang.NullPointerException
at hu.flux.helper.NameAndAddress.print(NameAndAddress.java:46)
at hu.flux.helper.NameAndAddress.main(NameAndAddress.java:101)

但是,如果我尝试从 JSP 访问 print(Writer writer),我会得到一个不同的错误:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoSuchMethodError: hu.flux.helper.NameAndAddress.print(Ljavax/servlet/jsp/JspWriter;)V
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:492)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:407)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

javax.servlet.ServletException: java.lang.NoSuchMethodError: hu.flux.helper.NameAndAddress.print(Ljavax/servlet/jsp/JspWriter;)V
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:898)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:827)
org.apache.jsp.Address_jsp._jspService(Address_jsp.java:92)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause

java.lang.NoSuchMethodError: hu.flux.helper.NameAndAddress.print(Ljavax/servlet/jsp/JspWriter;)V
org.apache.jsp.Address_jsp._jspService(Address_jsp.java:81)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.2 logs.

Apache Tomcat/7.0.2

当从 JSP 调用时,我可以通过添加以下代码来调用具有 JspWriter 工作的类:

public void print(JspWriter out) throws java.io.IOException 
{ print (new PrintWriter(out)); }

但是,当尝试从控制台应用程序使用 JspWriter 时(为了测试——我认为任何人都不需要在控制台中使用 JspWriter!),上面的控制台错误被移至此函数。

如果 print(JspWriter out) 可以解决 JSP 的问题,难道它不应该也解决控制台应用程序的问题吗?

此外,如果 JspWriter 是一个 Writer 对象,它不应该总是一个 Writer 对象,无论它是从控制台还是从 JSP 调用的?

最佳答案

此异常表示在将 print(JspWriter) 更改为 print(Object) 后,您的 JSP 代码尚未重新编译,因此它仍会尝试调用 print(JspWriter) 找不到它。

为了强制重新编译,您可以修改您的 JSP 页面。

关于java - 接收 java.lang.Object 作为输入的方法不应该也接收 javax.servlet.jsp.JspWriter 作为输入吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com