Monday, July 2, 2012

Small JSP to display the content of a file

pass the filename as ?file=/path/to/myfile.txt

<%@page import="java.io.*"%>

<%

String filename = request.getParameter("file");
FileReader fr = new FileReader(filename); 
BufferedReader br = new BufferedReader(fr);
String line;
while ( (line = br.readLine() ) != null ) {
    out.write(line);
    out.write("\n");
}
br.close();
fr.close();
%>

Oracle DB BLOB to String conversion

the problem is mainly with BLOBs > 2K

This is a collection of strategies:

this doesn't work, I get a "ORA-00932: inconsistent datatypes: expected NUMBER got BLOB" :
select TO_CHAR(DATA_VALUE) from wli_qs_report_data where msg_guid = 'uuid:7f9b72b69446518a:3591707b:1382c7d7451:-73f8';


this one is better, but still gives "ORA-06502: PL/SQL: numeric or value error: raw variable length too long":
select utl_raw.cast_to_varchar2(dbms_lob.substr(DATA_VALUE)) from wli_qs_report_data where msg_guid = 'uuid:7f9b72b69446518a:3591707b:1382c7d7451:-73f8';


this one gives "ORA-06502: PL/SQL: numeric or value error: raw variable length too long":
select DBMS_LOB.SUBSTR(DATA_VALUE, 32767, 1) from wli_qs_report_data where msg_guid = 'uuid:7f9b72b69446518a:3591707b:1382c7d7451:-73f8';


this one gives "ORA-00904: "DBMS_LOB"."CONVERTTOCLOB": invalid identifier":
select dbms_lob.converttoclob(DATA_VALUE) from wli_qs_report_data where msg_guid = 'uuid:7f9b72b69446518a:3591707b:1382c7d7451:-73f8';


after a while I give up, and do stuff directly in Java:


<sql:query var="rsMessageCount" dataSource="jdbc/soainfra" scope="request">
select DATA_VALUE from WLI_QS_REPORT_DATA where MSG_GUID='${msg_guid}'
</sql:query>

and I display ${rsMessageCount.getRowsByIndex()[0][0]} :
its class is oracle.sql.BLOB

I am planning to use http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/sql/BLOB.html#getBinaryStream__

which is a java.sql.blob

how to display a java.sql.Blob as a String?

http://www.jguru.com/faq/view.jsp?EID=1325

java.sql.Blob blob = (java.sql.Blob)((org.apache.taglibs.standard.tag.common.sql.ResultImpl)(request.getAttribute("rsMessageCount"))).getRowsByIndex()[0][0];

byte[] bdata = blob.getBytes(1, (int) blob.length());

String text = new String(bdata);
text = text.replace("<", "&lt;");

out.write(text);


Sunday, July 1, 2012

Tomcat error on startup: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()

java.lang.NoSuchMethodError: Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig;


Caused by: java.lang.NoSuchMethodError: javax.servlet.ServletContext.getSessionCookieConfig()Ljavax/servlet/SessionCookieConfig;
at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1309)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1310)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)




The offending method is:

SessionCookieConfig scc = context.getServletContext().getSessionCookieConfig();

and context is passed as "configureContext(Context context)"

and Context is "org.apache.catalina.Context"


I found out a different version of the javax.servlet package in wlfullclient.jar



in the servlet-api.jar in Tomcat in the Interface javax.servlet.ServletContext you can find the method:

public abstract SessionCookieConfig getSessionCookieConfig();


The error is triggered in org.apache.catalina.deploy.WebXml class, which is in catalina.jar.




I have tried this and it doesn work:

copy to tomcat/lib directory these files
javax.persistence-2.0.0.jar http://mvnrepository.com/artifact/org.eclipse.persistence/javax.persistence
persistence-api-1.0.2.jar http://mvnrepository.com/artifact/javax.persistence/persistence-api


The only thing which works is detecting the extra jar introducing the wrong version of the javax.servlet package and get it out of the way.