Archive for November, 2005

Automatically load a page after a few seconds

Written by coregps on Tuesday, November 15th, 2005 in Web Design.

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

We’ve developed a high load online exam system in which instructors create exams and deliver them through browser. The questions on the paper is randomly chose based on the parameters specified by the instructor. When students are ready to take the exam, the paper for each student will be sent to the browser from the server. I’ve created two paper pages. One is used to display the empty paper when the student login the first time. The other is used to display the paper with the answers the student already entered when he exit the system abnormally while taking an exam, he can simply re-connect, go back to the exam, and resume the exam. On an average we have about 50 to 100 students taking tests concurrently.

Sometimes, one or two machines cannot fetch the paper page successfully. Instead an error page is displayed. If the student then hit the refresh button the page is displayed. I want to direct the student to a friendly error page with this message: ‘The server is busy now, you will be redirected to the paper page after 5 seconds.’, and then reload the paper page automatically, instead of telling the students to refresh the page manually. However, as I mentioned above, there are two paper pages, both can throw the exception. So I must know which page throw the exception. I first thought of using request.getHeader(”Referer”) to get it, but it is always null. Finally, I added an additional parameter to the custom error page in the page directive, which is used to tell the error page which paper page throws the exception. In the error page, I can fetch the parameter from the request, and then set the “Refresh” header to redirect the student to the paper page which throws the exception.

In order to verify my idea, I write the following test code:

// the web page throws the exception, exception.jsp

<%@page pageEncoding=”UTF-8″ contentType=”text/html; charset=UTF-8″
%><%@ page errorPage=”retry.jsp?from=exception.jsp
%><%
response.setHeader(”Pragma”, “no-cache”);
response.setHeader(”Expires”, “0″);
response.setHeader(”Cache-Control”, “no-store”);
%><html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title></title>
</head>
<body><h2>Exception Testing</h2><%
String test = request.getParameter(”id”);
out.println(Integer.parseInt(test));
%>
</body>
</html>

// error handler page, retry.jsp

<%@page pageEncoding=”UTF-8″ contentType=”text/html; charset=UTF-8″
%><%@ page isErrorPage=”true”
%><%
String from = request.getParameter(”from”);
response.setHeader(”Refresh”, “5; URL=” + from);
%><html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title></title>
</head>
<body>
<div>
  The server is busy now, you will be redirected to the paper page after 5 seconds. <br />
  <a href=”<%=from%>”>Click here if your page doesn’t automatically refresh after 5 seconds.</a>
</div>
</body>
</html>

As you can see, I also set the cache control headers in order to avoid the browser cache the exception page.

Formatting date retrieved from database

Written by coregps on Tuesday, November 8th, 2005 in Java, Database.

While getting the value of the date column from table using ResultSet.getString, I always get the date in the format: 2005-11-07 00:00:00.0. In order to display it in the format ‘YYYY-MM-DD’, I’ve tried two solutions:


1. Use ResultSet.getTimestamp to retrieve the value as a java.sql.Timestamp or ResultSet.getDate to get the value as a java.sql.Date. and then use SimpleDateFormat to format the result for display:


SimpleDateFormat df = new SimpleDateFormat(”yyyy-MM-dd”); String orderdate = df.format(rs.getDate(”orderdate”));

2. Use the ResultSet.getString to retrieve the value as a String and parse the date string into a Date object, and the format the result for display:


SimpleDateFormat df = new SimpleDateFormat(”yyyy-MM-dd hh:mm:ss”); SimpleDateFormat df1 = new SimpleDateFormat(”yyyy-MM-dd”); String orderdate = df1.format(df.parse(rs.getString(”orderdate”));

Both solutions work well.

Get SQLNestedException again

Written by coregps on Saturday, November 5th, 2005 in Java, SQL Server.

When I configured SQL Server connection pool in Tomcat 5.5.9, I got the SQLNestedException again.


org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class ‘’ for connect URL ‘null’
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:780)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
 at org.apache.commons.dbutils.QueryRunner.prepareConnection(QueryRunner.java:189)
 at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:300)
 at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:322)
 at com.homesoft.db.DBConn.searchToMapList(DBConn.java:278)
 at platform.Employee.getEmployeeList(Employee.java:27)
 at org.apache.jsp.newtask_jsp._jspService(org.apache.jsp.newtask_jsp:110)
 at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:99)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:325)
 at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
 at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:245)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
 at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:825)
 at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:738)
 at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:526)
 at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
 at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
 at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: No suitable driver
 at java.sql.DriverManager.getDriver(Unknown Source)
 at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:773)
 … 27 more

I reviewed my configuration again and again, but can’t find out the reason. The following is the JNDI DataSource configuration in Tomcat server.xml file:


<Context path=”" reloadable=”true” docBase=”d:/eclipse/workspace/demo” debug=”0″>
    <Resource name=”jdbc/demoDB” auth=”Container” type=”javax.sql.DataSource”
    factory=”org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory”
    username=”sa”
    password=”secret”
    driverClassName=”com.microsoft.jdbc.sqlserver.SQLServerDriver”
    url=”jdbc:microsoft:sqlserver://localhost:1433;selectMethod=cursor;DatabaseName=demodb”
    maxWait=”1000″ removeAbandoned=”true”
    maxActive=”100″ maxIdle=”30″
    removeAbandonedTimeout=”60″ logAbandoned=”true”/>
</Context>

Finally, I found that I’ve not created the web application deployment descriptor file - web.xml. What a stupid mistake!!!


You may similarily have received or will receive the SQLNestedException. In my experience, the following key points should be noticed carefully:


1. The JNDI DataSource should be both defined inside the server.xml and declared in the web.xml.


2. We have to set up the JNDI resources by attribute instead of resource-params like we used to.


3. The value of the ‘factory’ attribute of the ‘Resource’ element should be “org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory” instead of “org.apache.commons.dbcp.BasicDataSourceFactory”.



Site Navigation