Strut example
This is a good example of struts and a db connection. Later, you may want to try db pooling.
web.xml
========
xml version=“1.0″ encoding=“ISO-8859-1″?>
<servlet>
<servlet-name>actionservlet-name>
<servlet-class>org.apache.struts.action.ActionServletservlet-class>
<init-param>
<param-name>configparam-name>
<param-value>/WEB-INF/struts-config.xmlparam-value>
init-param>
<load-on-startup>2load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>actionservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
web-app>
Struts-Config.xml
Struts-Config.xml===============
xml version=“1.0″ encoding=“ISO-8859-1″ ?>
<form-beans>
<form-beanname=“TheForm”
type=“org.app.apache.forms.TheForm”>
<form-property name=“results” type=“org.app.apache.forms.results” />
form-bean>
form-beans>
<global-forwards>
<forwardname=“welcome”
path=“/welcome.do”/>
global-forwards>
<action-mappings>
<action path=“/welcome” type=“org.app.apache.actions.TheAction”
name=“TheForm”>
<forward name=“success” path=“/pages/welcome.jsp”/>
action>
action-mappings><message-resources parameter=“MessageResources” />
struts-config>
index.jsp
========
<logic:redirect forward=“welcome”/>
<html:html>
<head><
title><bean:message key=“welcome.title”/>title>
<html:base/>
head><body bgcolor=“white”>
<logic:notPresent name=“org.apache.struts.action.MESSAGE” scope=“application”>
<font color=“red”>
ERROR: Application resources not loaded — check servlet container
logs for error messages.
font>
logic:notPresent>
<logic:iterate id=“rsts” name=“results”>
<bean:write name=“rsts”/>
<br>>><br>
logic:iterate>
<h3><bean:message key=“welcome.heading”/>h3>
<p><bean:message key=“welcome.message”/>p>
body>
html:html>
TheAction.java
=========
package org.app.apache.actions;
import org.app.apache.db.MyOracleConnection;
import java.util.ArrayList;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TheAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
MyOracleConnection db = new MyOracleConnection();ResultSet rs =
null;
db.getConnection();
ArrayList results = new ArrayList();
rs=db.executeSQL(“select agente_id from agente”);
while(rs.next()){results.add(rs.getString(“agente_id”));
}
db.closeConnection();
request.setAttribute(“results”, results);return mapping.findForward(“success”);
}
}
MyOracleConnection.java
=================
package org.app.apache.db;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class MyOracleConnection {
Connection con;
ResultSet rs;
String driverName;
public MyOracleConnection(){
}
public void getConnection(){
try{
driverName = “oracle.jdbc.driver.OracleDriver”;
Class.forName(driverName);
con= DriverManager.getConnection(”jdbc:oracle:oci:userid/password@dbInstance”);
}
catch(Exception e){
System.out.println(”dbConnect error”);
}
}
public void closeConnection(){
try{
if (con.isClosed() == false ){
//close the connection if open
con.close();
}
rs.close();
}
catch(Exception e){
System.out.println(”dbClose error”);
}
}
public ResultSet executeSQL(String SQL){
try{
Statement stmt = con.createStatement();
stmt.execute(SQL);
rs = stmt.getResultSet();
}
catch(Exception e){
System.out.println(”rsGrap error”);
}
return rs;
}
}
TheForm.java
==============
package org.app.apache.forms;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
/* this is called a form bean*/
public class TheForm extends ActionForm{
private ArrayList results = new ArrayList();
public void setResults(ArrayList results){
this.results = results;
}
public ArrayList getResults(){
return results;
}
// Reset form fields.
public void reset(ActionMapping mapping, HttpServletRequest request)
{
results = null;
}
}