PDA

View Full Version : JSP Example


newbie2
08-21-2005, 06:26 AM
Hi There,

I am going thru the doc at http://www.laszlosystems.com/lps-3.0/docs/guide/data_app.html and am using this to help me get JSP and Laszlo talking :-)

I am unfortunately getting errors on the JSP example(s) that was given in Chapter 33, section 6 ... Has anyone got a working example of JSP formatting DB output for XML working that they would be willing to share?

Here is my JSP code and the error:

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

<%
Connection connetion=null;
try {
String u_login=request.getParameter("u_login");

Class.forName("org.postgresql.Driver");
Connection db =DriverManager.getConnection("jdbc:postgresql:dbName","postgres","password");

Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM t_user WHERE u_login='"+u_login+"';");

while (rs.next()) {
%>
<t_user firstName="<%= rs.getString("u_fname")%>"
lastName="<%= rs.getString("u_lname")%>"
userTypeId="<%= rs.getString("u_typeid")%>"
userSportId="<%= rs.getString("u_sportid")%>"
userLevelId="<%= rs.getString("u_levelid")%>"/>
<%
}
}
finally {
try{
connection.close();
}
}
%>


Error:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 24 in the jsp file: /peak/getworkoutdetailsXML.jsp
Generated servlet error:
Syntax error, insert "Finally" to complete BlockStatements


org.apache.jasper.compiler.DefaultErrorHandler.jav acError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacEr ror(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateCla ss(JDTCompiler.java:389)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:288)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:267)
org.apache.jasper.compiler.Compiler.compile(Compil er.java:255)
org.apache.jasper.JspCompilationContext.compile(Js pCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:296)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.jasper.runtime.PageContextImpl.doForwar d(PageContextImpl.java:690)
org.apache.jasper.runtime.PageContextImpl.forward( PageContextImpl.java:657)
org.apache.jsp.peak.validate_jsp._jspx_meth_c_when _0(org.apache.jsp.peak.validate_jsp:516)
org.apache.jsp.peak.validate_jsp._jspx_meth_c_choo se_0(org.apache.jsp.peak.validate_jsp:482)
org.apache.jsp.peak.validate_jsp._jspService(org.a pache.jsp.peak.validate_jsp:123)
org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:99)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:325)
org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:295)
org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:245)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)

hqm
08-21-2005, 07:06 AM
Your "try" clause in the finally block needs its own catch or finally clause or else it is incomplete.


You can always do

catch (Exception e) {}

to begin with. or

catch (Exception e) {
out.println(e);
}

newbie2
08-21-2005, 07:17 AM
Thanks so much ... do i feel stupid!!!

One more thing. Now I get a connection error. Note that if I use something like:

<%
Connection connetion=null;
try {
Class.forName("org.postgresql.Driver");

Connection db =DriverManager.getConnection("jdbc:postgresql:dbName","postgres","password");
}
finally {
}
%>


<sql:query var="athWOinfo" scope="session">
SELECT * FROM t_user

</sql:query>

...

I Do NOT get a connection error. I assume I am doing something odd/wrong at the top part of my file but all examples seem to do the same thing!

hqm
08-21-2005, 07:37 AM
Did you copy one of the examples verbatim from the tutorial? I haven't tried the examples, so I don't know if they are up to date...


You're using postgres? Maybe there are some example Java servlet or JSP code that ship with it that can help you get a successful connection example??

newbie2
08-21-2005, 09:47 AM
Perhaps I was not clear.

1. Yes, i copied verbatim :-)

2. I CAN get a sucessfull connection if I use the second sample of code I posted above (and can read/write anything i want using that same method). It is only when trying to produce an XML dataset (following the example given) that I get the no connection error ...

The delta is that the sql query is defined differently and in a different spot --> in the same secton as the db connection in the XML examples.

Any help is appreciated. I am at a loss

Thanks again!

newbie2
08-29-2005, 07:02 AM
Just so you all know. The above does work ... you just have to spell connection consistently :-)

Had a moment so thought i'd post this for any other PostGres users out there!

hqm
08-29-2005, 07:05 AM
live and learn ...