The following is extracted from http://java.sun.com/developer/technicalArticles/Intl/HTTPCharset/#browser
Server: Tomcat
1. Setting the context parameter is done in the WEB-INF/web.xml file.
<context-param>
<param-name>PARAMETER_ENCODING</param-name>
<param-value>UTF-8</param-value>
</context-param>
2. Add the following code to your JSP file.
<%
//Must put in the first section of the jsp page
String paramEncoding = application.getInitParameter("PARAMETER_ENCODING");
request.setCharacterEncoding(paramEncoding);
%>
*Please note that the following lines must be inserted at the begining of the JSP page after header declaration (eg. tagLib or Page), before any other content processing.
Friday, January 13, 2012
Friday, January 6, 2012
Run JasperReport (ireport) in JSP
(1) Import the following libraries to your project
===================================================
jasperreports-3.7.2.jar
iText-2.1.7.jar
jdt-compiler-3.1.1.jar
groovy-all-1.5.5.jar
commons-logging-api-1.1.jar
commons-logging-adapters-1.1.jar
commons-logging-1.1.jar
commons-digester-1.8.jar
commons-collections-testframework-3.2.jar
commons-collections-3.2.jar
commons-beanutils-core.jar
commons-beanutils-bean-collections.jar
commons-beanutils.jar
(2) Sample JSP Code
===================================================
String dirPath = "ireports";
String realPath = this.getServletContext().getRealPath(dirPath);
String orderNo = request.getParameter("orderNo");
String jasperReport = "nameCardFront.jasper";
JasperPrint print = null;
String outputFileName = null;
Connection conn = null;
try{
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/DOS");
conn = ds.getConnection();
Map parameters = new HashMap();
parameters.put("orderNo", "a");
print = JasperFillManager.fillReport(realPath + "//" + jasperReport, parameters, conn);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
outputFileName = "file.pdf";
out.clearBuffer();
//set the content type(can be excel/word/powerpoint etc..)
response.setContentType ("application/pdf");
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment; filename=\"" + outputFileName);
JasperExportManager.exportReportToPdfStream(print, response.getOutputStream());
}
catch(Exception ex){
ex.printStackTrace();
out.println(ex.getMessage());
}
finally{
if (conn != null)
conn.close();
}
===================================================
jasperreports-3.7.2.jar
iText-2.1.7.jar
jdt-compiler-3.1.1.jar
groovy-all-1.5.5.jar
commons-logging-api-1.1.jar
commons-logging-adapters-1.1.jar
commons-logging-1.1.jar
commons-digester-1.8.jar
commons-collections-testframework-3.2.jar
commons-collections-3.2.jar
commons-beanutils-core.jar
commons-beanutils-bean-collections.jar
commons-beanutils.jar
(2) Sample JSP Code
===================================================
String dirPath = "ireports";
String realPath = this.getServletContext().getRealPath(dirPath);
String orderNo = request.getParameter("orderNo");
String jasperReport = "nameCardFront.jasper";
JasperPrint print = null;
String outputFileName = null;
Connection conn = null;
try{
InitialContext initialContext = new InitialContext();
DataSource ds = (DataSource)initialContext.lookup("java:comp/env/jdbc/DOS");
conn = ds.getConnection();
Map parameters = new HashMap();
parameters.put("orderNo", "a");
print = JasperFillManager.fillReport(realPath + "//" + jasperReport, parameters, conn);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
outputFileName = "file.pdf";
out.clearBuffer();
//set the content type(can be excel/word/powerpoint etc..)
response.setContentType ("application/pdf");
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment; filename=\"" + outputFileName);
JasperExportManager.exportReportToPdfStream(print, response.getOutputStream());
}
catch(Exception ex){
ex.printStackTrace();
out.println(ex.getMessage());
}
finally{
if (conn != null)
conn.close();
}
Monday, August 16, 2010
Netbeans 6.8 with Tomcat 6.0.20 - Requires JDK 6 Update 20
Netbeans 6.8 + Tomcat 6.0.20
Tested worked with JDK 6 Update 20
JDK 6 Update 19, JDK 6 Update 21 failed to work
Error:
In any jsp file, Netbeans will highligh error for the following lines
<%@ page contentType="text/html" session="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
*Courtesy of Andy Lim*
Tested worked with JDK 6 Update 20
JDK 6 Update 19, JDK 6 Update 21 failed to work
Error:
In any jsp file, Netbeans will highligh error for the following lines
<%@ page contentType="text/html" session="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
*Courtesy of Andy Lim*
Friday, March 12, 2010
给宠物狗的鞋子 : 一双/两双?
周五,吃了晚饭和同事到新开的宠物店闲逛。一边赞叹琳琅满目的宠物食/用品,同事指了指包装精美的四只宠物狗鞋子。
下意识的,我回应说“不错哦,有两双可以替换”。。。。
“两双?”,同事楞了楞说道。
我说“对啊,四只不就两双吗?”。。。。
“狗有四只脚。。。。”
哈哈,惯性思维。总想着人就一双两只鞋呗,所以见到四只鞋子就理所当然的以为是两双,还“替换”呢!
其实日常工作中,常有因为惯性思维而闹笑话的事,很多时候当事人并不知晓犯了什么错误,就像我一样,愣是得意着。
注:请不要咬文嚼字计较四只,该说是一套/一双/两双?重点在于“替换” 与 “惯性思维”
下意识的,我回应说“不错哦,有两双可以替换”。。。。
“两双?”,同事楞了楞说道。
我说“对啊,四只不就两双吗?”。。。。
“狗有四只脚。。。。”
哈哈,惯性思维。总想着人就一双两只鞋呗,所以见到四只鞋子就理所当然的以为是两双,还“替换”呢!
其实日常工作中,常有因为惯性思维而闹笑话的事,很多时候当事人并不知晓犯了什么错误,就像我一样,愣是得意着。
注:请不要咬文嚼字计较四只,该说是一套/一双/两双?重点在于“替换” 与 “惯性思维”
Tuesday, March 2, 2010
Jasper Report + Oracle CURSOR_SHARING='FORCE'
Notice that if i set the Oracle Initialization Parameter
CURSOR_SHARING = 'FORCE' or
CURSOR_SHARING = 'SIMILAR'
the Jasper Report (iReport) with UNION query does not work. It shall give the following error:
java sql sqlexception : no more data read from socket
To solve this, I have to tell the optimiser to switch off the cursor sharing feature for this query by inserting the phrase /*+ CURSOR_SHARING_EXACT */ into the query.
E.g.
SELECT /*+ CURSOR_SHARING_EXACT */ field 1 FROM TABLE 1
UNION
SELECT field 2 FROM TABLE 2
References:
1. Extract from http://www.psoug.org/reference/hints.html
Oracle can replace literals in SQL statements with bind variables, when it is safe to do so. This replacement is controlled with the CURSOR_SHARING initialization parameter. The CURSOR_SHARING_EXACT hint instructs the optimizer to switch this behavior off. In other words, Oracle executes the SQL statement without any attempt to replace literals with bind variables.
2. CURSOR_SHARING :
http://www.oracle.com/technology/oramag/oracle/06-jan/o16asktom.html
CURSOR_SHARING = 'FORCE' or
CURSOR_SHARING = 'SIMILAR'
the Jasper Report (iReport) with UNION query does not work. It shall give the following error:
java sql sqlexception : no more data read from socket
To solve this, I have to tell the optimiser to switch off the cursor sharing feature for this query by inserting the phrase /*+ CURSOR_SHARING_EXACT */ into the query.
E.g.
SELECT /*+ CURSOR_SHARING_EXACT */ field 1 FROM TABLE 1
UNION
SELECT field 2 FROM TABLE 2
References:
1. Extract from http://www.psoug.org/reference/hints.html
Oracle can replace literals in SQL statements with bind variables, when it is safe to do so. This replacement is controlled with the CURSOR_SHARING initialization parameter. The CURSOR_SHARING_EXACT hint instructs the optimizer to switch this behavior off. In other words, Oracle executes the SQL statement without any attempt to replace literals with bind variables.
2. CURSOR_SHARING :
http://www.oracle.com/technology/oramag/oracle/06-jan/o16asktom.html
Thursday, February 4, 2010
jQuery/Plugin : Problem with Add/Remove row for Tablesorter Plugin
For using the jQuery tablesorter (http://tablesorter.com/docs/ ), the table will not refresh correctly if you use the additional pager plugin.
To solve this problem, you have to add in 2 customised functions/events to take care of the row adding/removal.
Add the following 2 functions to the tablesoter javascript file.
You can bind the functions 2 customised events as follows:
Finally you can call the events from your page as follows:
To solve this problem, you have to add in 2 customised functions/events to take care of the row adding/removal.
Add the following 2 functions to the tablesoter javascript file.
function addRow(table, newRow){
//Rebuild html table from the modified rowsCopy
$.tablesorter.clearTableBody(table);
if (table.config.rowsCopy!=undefined)
$.each(table.config.rowsCopy, function() {
$(table).find('tbody:first').append($(this));
});
//Append new row to the HTML table
$(table).find('tbody:first').append(newRow);
//Update call will rebuild tablesorter.cache from the existing HTML table
$(table).trigger("update");
//With pager plugin, this will call appender function in pager
//In turn, it will redraw the table with pagination
$(table).trigger("appendCache");
//Retain sorting if any
$(table).trigger("sorton",[table.config.sortList]);
}
function removeRow(table, rowId){
//Remove the selected row from pager.rowsCopy
var newRowsCopy = $.grep(table.config.rowsCopy, function(n, i){
return ($(n).attr('id')!=rowId);
});
table.config.rowsCopy = newRowsCopy;
//Rebuild html table from the modified rowsCopy
$.tablesorter.clearTableBody(table);
if (table.config.rowsCopy!=undefined)
$.each(table.config.rowsCopy, function() {
$(table).find('tbody:first').append($(this));
});
//Update call will rebuild tablesorter.cache from the existing HTML table
$(table).trigger("update");
//With pager plugin, this will call appender function in pager
//In turn, it will redraw the table with pagination
$(table).trigger("appendCache");
}
You can bind the functions 2 customised events as follows:
$this.bind("addRow",function(event,newRow){
addRow(this,newRow);
});
$this.bind("removeRow",function(event,rowId){
removeRow(this,rowId);
});
Finally you can call the events from your page as follows:
$(sortTable).trigger("removeRow",[id for the row to be removed]);
var newTr = $('<tr><td>New Row</td></tr>');
$(sortTable).trigger("addRow",[newTr]);
Deploy Netbeans 6.1 Web Application to Tomcat 5.5
Netbeans 6.1 come bundles with Tomcat 6.0. So you will encounter problem if you upload the war file directly to your production Tomcat 5.5 server.
Reason being the web.xml in you war file use version 2.5 XML declaration but Tomcat 5.5 uses version 2.4. The web.xml is located at
<catalina-home>/webapps/<your apps>/WEB_INF
Look at the first two lines of the web.xml, you shall see something like the following
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Whereas in Tomcat 5.5, the two lines shall be replaced with the following
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
After which, you have to clear the cache of compiled JSP file.
<catalina-home>\work\Catalina\<your apps>
Remove the whole folder with name "_" (underscore). This is the compiled JSP file (java class) by tomcat and is compiled only once when the jsp page is accessed/requested for the first time.
You have to stop the tomcat before removing the folder.
The folder will be rebuild automatically after you have restarted the tomcat and the jsp is being accessed.
Reason being the web.xml in you war file use version 2.5 XML declaration but Tomcat 5.5 uses version 2.4. The web.xml is located at
<catalina-home>/webapps/<your apps>/WEB_INF
Look at the first two lines of the web.xml, you shall see something like the following
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Whereas in Tomcat 5.5, the two lines shall be replaced with the following
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
After which, you have to clear the cache of compiled JSP file.
<catalina-home>\work\Catalina\<your apps>
Remove the whole folder with name "_" (underscore). This is the compiled JSP file (java class) by tomcat and is compiled only once when the jsp page is accessed/requested for the first time.
You have to stop the tomcat before removing the folder.
The folder will be rebuild automatically after you have restarted the tomcat and the jsp is being accessed.
Subscribe to:
Posts (Atom)