AXIS2 WEBSERVICE CLIENT USING STRUTS2 AND DAO WITH MYSQL
Hi Reader,
Now, lets discuss about Axis2 Webservice Client Withs Struts2 and MySql and Glassfish from my previous blog.
Step 4 :
We need to start the MySql server.
URL - jdbc:mysql://localhost:3306/satya
UserName - root
Password - root
From Glassfish server Resource to JDBC and JDBC Connection Pools. Create a new JDBC Resource and provide JNDI Name as jdbc/mysql.
We need to provide datasource classname as com.mysql.jdbc.jdbc2.optional.MysqlDataSource. in order to communicate with MySQL database.
Step 5 :
BaseDAO is used to get the connection from the datasource object, closing statement and resultset object. We need to set the dataSource object in BaseDAO class.
BaseDAO :
import
java.sql.DriverManager;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import javax.sql.DataSource;
public class BaseDAO {
private static final String CLASSNAME =
BaseDao.class.getName();
private DataSource datasource;
public void setDataSource(DataSource dataSource){
this.dataSource=dataSource;
}
public static Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
System.out.println("Error getting
connection. Data Source = "
+ e.toString());
}
return connection;
}
public static void
closeConnection(Connection connection) {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
System.out.println("Error
closeDbConnection = " + e.toString());
}
}
}
public static void closeStatement(Statement stmt) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
}
}
public static void closeResultSet(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
System.out.println("Error
closeResultSet. " + e.toString());
}
}
}
}
Step 6 :
For database access we need to implement our own DAO class by extending BaseDAO.
We need to use DataSouce in BaseDAO for getting the connection object.
AllPoliceDAO :
public class AllPoliceDAO extends BaseDAO{
public final String CLASS_NAME=AllPoliceDAO.class.getName();
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
public PoliceBean dbconn(AllPoliceBean bean) throws DBException{
connection = getConnection();
String query="select * from ipp_Date";
preparedStatement = connection.prepareStatement(query);
resultSet = preparedStatement(query);
while(resultSet.next()){
validdate = resultSet.getString("ipp_date");
}catch(Exception e){
e.toString();
}finally{
connection.close();
preparedStatement.close();
resultSet.close();
}
LoggerMSG.info(CLASSNAME + " " + METHOD_NAME + " : " + "Ends");
return bean;
}
We need to create the bean class using struts2 by extending the BaseModel bean. Bean class will contain all the user input information.
PoliceBean :
public class PoliceBean extends BaseModelBean{
String policeName;
int policeNumber;
// getter and setter methods.
}
Step 7 :
We need to create the bean class using struts2 by extending the BaseModel bean. Bean class will contain all the user input information.
PoliceBean :
public class PoliceBean extends BaseModelBean{
String policeName;
int policeNumber;
// getter and setter methods.
}
Step 8 :
Action class is used to call the Delegate class to get the response object. Finally Action class send the response to the JSP page.
PoliceAction :
package com.satya; public class PoliceAction extends BaseAction implements ModelDriven<policebean>
{
PoliceBean pe = new PoliceBean();
public policebean getModel(){
return pe ;
}
public String getPoliceInfo(pb){
return page;
}
Step 9 :
We need to create struts2 Action to redirect the request to the PoliceAction class. We are implementing the wildcard method selection technique to enable the struts2 framework to dynamically select the correct method to call at runtime we need to use wildcard character "*" in your name value and an attribute value place holder {1} for the method value.
ipp.xml
<action name="police" method="{1}" class="com.satya.PoliceAction">
<result name="homepage" type="tiles">homepage</result>
</action>
Step 9 :
We need to create struts2 Action to redirect the request to the PoliceAction class. We are implementing the wildcard method selection technique to enable the struts2 framework to dynamically select the correct method to call at runtime we need to use wildcard character "*" in your name value and an attribute value place holder {1} for the method value.
ipp.xml
<action name="police" method="{1}" class="com.satya.PoliceAction">
<result name="homepage" type="tiles">homepage</result>
</action>
Step 9 :
Create the tile definition to forward to the JSP page. We need to create base tile definition layout and extends the layout definition in all forward call to JSP page.
ipp.tiles.xml
<tiles-definitions>
<definitions name="layout" templet="/jsp/layout/logout.jsp">
<put-attribute name="header" value="/jsp/layout/header.jsp"/>
<put-attribute name="footer" value="/jsp/layout/footer.jsp"/>
</tiles-definitions>
<definition name="homepage" extends="layout">
<put-attribute name="body" value="/jsp/ipp/Multiple.jsp"/>
</definition>
Finally we will display the result in the JSP page using tiles in struts2.
ipp.tiles.xml
<tiles-definitions>
<definitions name="layout" templet="/jsp/layout/logout.jsp">
<put-attribute name="header" value="/jsp/layout/header.jsp"/>
<put-attribute name="footer" value="/jsp/layout/footer.jsp"/>
</tiles-definitions>
<definition name="homepage" extends="layout">
<put-attribute name="body" value="/jsp/ipp/Multiple.jsp"/>
</definition>
Finally we will display the result in the JSP page using tiles in struts2.
No comments:
Post a Comment