|
Joe Pluta wrote:
Thanks, what is the relationship of the servlet to the JSP Page. Where would you put the servlet and the JSP. Do you need to be running Websphere and if so then what version?returnsFrom: Bruce Collins
I have a java program that connects to the iSeries via JDBC and
output. If I want to put that into a JSP and present it via a web browser what would be involved.
If you have examples that would be wonderful
Bruce, the JSP Model II way to do this is to put the data from the result set into a bean, and then expose the bean to the JSP page. The quick and dirty way is to simply expose the result set. I posted something on Ignite/400 a while back that compromises: it puts the result set in a bean.
The servlet reads a file called "DATAFILE" in library "DATA", with six fields (name, address, phone, email, department and occupation) and then spits out a report in a JSP. It also does some timings.
I'll repost it here:
THE SERVLET: ------------ package com.pbd.datatest;
import java.io.*; import java.sql.*; import java.util.Date;
import javax.servlet.*; import javax.servlet.http.*;
public class DataServlet extends HttpServlet implements Servlet {
public class Data
{
public Connection conn;
public Statement s;
public ResultSet rs;
public java.util.Date start;
public java.util.Date read;
public void close() throws Exception
{
rs.close();
s.close();
conn.close();
}
public long getReadTime()
{
return read.getTime() - start.getTime();
}
public long getTotalTime()
{
return new java.util.Date().getTime() - start.getTime();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
Data data = new Data();
data.start = new Date();
Class.forName("com.ibm.as400.access.AS400JDBCDriver").
newInstance(); if (System.getProperty("os.name").equals("OS/400"))
data.conn = DriverManager.getConnection(
"jdbc:as400://localhost", "USER", "PWD");
else
data.conn = DriverManager.getConnection(
"jdbc:as400://host", "USER", "PWD");
data.s = data.conn.createStatement();
data.rs = data.s.executeQuery("SELECT * FROM
DATA.DATAFILE");
data.read = new Date();
HttpSession hs = req.getSession(true);
hs.setAttribute("data", data);
getServletContext().
getRequestDispatcher("/app/showdata.jsp").
forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }
}
THE JSP:
--------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML>
<HEAD> <TITLE>showdata.jsp</TITLE> </HEAD> <BODY> <jsp:useBean
class="com.pbd.datatest.DataServlet.Data"
id="data" scope="session"></jsp:useBean>
<table border="0" width="100%">
<tr align="left">
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>e-mail</th>
<th>Department</th>
<th>Occupation</th>
</tr> <% while (data.rs.next()) { %> <tr>
<td><%= data.rs.getString(1) %></td>
<td><%= data.rs.getString(2) %></td>
<td><%= data.rs.getString(3) %></td>
<td><%= data.rs.getString(4) %></td>
<td><%= data.rs.getString(5) %></td>
<td><%= data.rs.getString(6) %></td>
</tr>
<% }
data.close();
%>
</table>
Read time: <%= data.getReadTime() %> ms<br>
Total time: <%= data.getTotalTime() %> ms<br>
</BODY>
</HTML>
Nothing to it.
Joe
Thanks
--
Bruce "Hoss" Collins
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.