This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Unable to Call Action Servlet when datasource elememt in kept struts-config.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Unable to Call Action Servlet when datasource elememt in kept struts-config.xml" Watch "Unable to Call Action Servlet when datasource elememt in kept struts-config.xml" New topic
Author

Unable to Call Action Servlet when datasource elememt in kept struts-config.xml

sreenivas jeenor
Ranch Hand

Joined: Jan 09, 2005
Posts: 125
Hi
I m using Mysql 5.0,Tomcat 5.5.1.I v placed a Data Source element in struts-config.xml .if i place DataSource elememt I m unable to call action Servlet.Help me out.......

Here is the code.

Login.jsp
---------
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html><head><title>Sign in, Please!</title></head>
<body>
<html:errors/>
<html:form action="/LogonSubmit" focus="username">
<table border="0" width="100%">
<tr>
<th align="right">Username: </th>
<td align="left"><html:text property="username"/></td>
</tr>
<tr><th align="right">Password: </th>
<td align="left"><html assword property="password"/></td>
</tr>
<tr>
<td align="right"><html:submit property="submit" value="Submit"/></td>
<td align="left"><input type=reset name=reset value=reset></td>
</tr>
</table>
</html:form>
</body>
</html>

---------------------------
Here is Struts-config.xml file
-------------------------------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<!--
This is a blank Struts configuration file with an example
welcome action/page and other commented sample elements.

Tiles and the Struts Validator are configured using the factory defaults
and are ready-to-use.

NOTE: If you have a generator tool to create the corresponding Java classes
for you, you could include the details in the "form-bean" declarations.
Otherwise, you would only define the "form-bean" element itself, with the
corresponding "name" and "type" attributes, as shown here.
-->


<struts-config>

<data-sources>


<data-source type="org.apache.commons.dbcp.BasicDataSource" key="Action.DATA_SOURCE_KEY"
<set-property property="autoCommit"
value="true"/>
<set-property property="description"
value="Example Data Source Configuration"/>
<set-property property="driverClass"
value="com.mysql.jdbc.Driver"/>
<set-property property="maxCount"
value="4"/>
<set-property property="minCount"
value="2"/>
<set-property property="password"
value=""/>
<set-property property="url"
value="jdbc:mysql://localhost:3306/emrxp4"/>
<set-property property="user"
value="root"/>
</data-source>




</data-sources>

<!-- ================================================ Form Bean Definitions -->

<form-beans>

<form-bean
name="LogonForm"
type="app.LogonForm"/>


</form-beans>


<!-- ========================================= Global Exception Definitions -->

<global-exceptions>
<!-- sample exception handler
<exception
key="expired.password"
type="app.ExpiredPasswordException"
path="/changePassword.jsp"/>
end sample -->
</global-exceptions>


<!-- =========================================== Global Forward Definitions -->

<global-forwards>
<!-- Default forward to "Welcome" action -->
<!-- Demonstrates using index.jsp to forward -->
<forward
name="welcome"
path="Welcome.jsp"/>
<forward
name="Logon"
path="logon.jsp"/>
</global-forwards>


<!-- =========================================== Action Mapping Definitions -->

<action-mappings>
<action
path="/LogonSubmit"
type="app.LogonAction"
name="LogonForm"
scope="request"
validate="true"
input="/logon.jsp">
<forward
name="success" path="/sucess.jsp"/>
</action>
</action-mappings>

<!-- resources -->
<message-resources
parameter="/resources/ApplicationResources"/>

</struts-config>

-----------------------------

Here is my LoginAction.java file
-------------------------------
package app;
import app.LogonForm;
import java.io.*;
import java.sql.*;
import javax.sql.*;
//import app.commonutilities.connectionmanager.ConnectionManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;

public class LogonAction extends Action {
public static String SUCCESS = "success";
//private ConnectionManager connectionmanager = null;
javax.sql.DataSource dataSource;
java.sql.Connection myConnection;
Statement stmt = null;
ResultSet rs = null;


public ActionForward execute(ActionMapping mapping,
ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
System.out.println("HERR IS MY FILE");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("inside perform method");
LogonForm lform =(LogonForm) form;
// Obtain username and password from web tier
String username = lform.getUsername();
String password = lform.getPassword();
// Validate credentials
//out.println("username is.. "+username);
//out.println("password is...."+password);
boolean validated = false;

try {
dataSource = (DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
//dataSource = getDataSource(request);

// dataSource = (DataSource)servlet.getServletContext().getAttribute("REFDB");
myConnection = dataSource.getConnection();



stmt = myConnection.createStatement();
rs = stmt.executeQuery("slect username,password from user ");
// do what you wish with myConnection
while(rs.next())
{
if (username.equals(rs.getString(1)) && (password.equals(rs.getString(2))))
{
validated=true;
return mapping.findForward(SUCCESS);
}
rs.close();
stmt.close();
myConnection.commit();
myConnection.close();
}
} catch (SQLException sqle)
{
getServlet().log("Connection.process", sqle);
} finally
{
//enclose this in a finally block to make
//sure the connection is closed
try {
myConnection.close();
} catch (SQLException e)
{
getServlet().log("Connection.close", e);
}
}//End of finally


/* while(rs.next())
{
if (username.equals(rs.getString(1)) && (password.equals(rs.getString(2))))
{
validated=true;
return mapping.findForward(SUCCESS);
*/

if(validated==false)
{
ActionErrors errors= new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionError("error.logon.incorrect"));
saveErrors(request,errors);
return mapping.getInputForward();
}
else{

ActionErrors errors= new ActionErrors();
errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionError("error.logon.incorrect"));
saveErrors(request,errors);
return mapping.getInputForward();}
} // end perform
} // end LogonAction


Thanks In Advance
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

hey any reply on this thread.. I also got stuck with the same problem...

Can anyone please throw some light on this.


The Best way to predict your future is to create it - Every great individual common man
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There is only one way to solve any problem with the Struts Data Source utility, and that is to stop using it. It's deprecated in later versions, and it never worked all that well to begin with. For more information on what you should use instead, see question 7 of the JavaRanch Struts FAQ


Merrill
Consultant, Sima Solutions
 
Consider Paul's rocket mass heater.
 
subject: Unable to Call Action Servlet when datasource elememt in kept struts-config.xml
 
Similar Threads
Facing problem in struts application
help with login program
Struts 1.1 problem with form validation
logic:iterate pls any body help me
Struts Error