struts connecting database

I want to do this, When the user input is processed, it will be checked against the database to see whats the role associated with the user. If the user is in the admin role, it will show Hello Admin on the second page.

Using struts and mysql, I've got the jsp,Formbean ,

The problem is: The requested resource (Servlet action is not available) is not available.

part code

<html:form action="/login">

<table border="0" cellspacing="2" cellpadding="0">

<tr>

<td colspan="2">

</td>

</tr>

<tr>

<td width="15%">Enter your name:</td>

<td width="85%">

<html:text property="name" size="25" maxlength="50"

onfocus="this.select()"/>

</td>

</tr>

Formbean code

public class LoginForm extends org.apache.struts.validator.ValidatorForm{

private String name = null;

private String result ;

public void setName(String name) {

this.name = name;

}

public String getName() {

return name;

}

public void setResult(String result) {

this.result = result;

}

public String getResult() {

return result;

}

}

action code

-

public class DB extends Action {

/* forward name="success" path="" */

private final static String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

//casting a formbean

LoginForm bean =(LoginForm) form ;

//get form parameters

String name = bean.getName();

try{

//get connection from database

javax.sql.DataSource dataSource = null;

java.sql.Connection myConnection;

ServletContext context=servlet.getServletContext();

DataSource datasource;

myConnection = dataSource.getConnection();

PreparedStatement stmt=(PreparedStatement) myConnection.createStatement();

// Execute a query

String query = "SELECT * FROM `role` NATURAL JOIN `user` WHERE username = ?";

ResultSet RS=stmt.executeQuery(query);

// Set values to the parameters

bean.setName(RS.getString("username"));

// extract data from the ResultSet

while(RS.next()){

String role = RS.getString(1);

if(role.equalsIgnoreCase("admin")||(role.equalsIgnoreCase("user")) ){

return mapping.findForward(SUCCESS);

}else{

return mapping.findForward("fail");

}

}

}catch (SQLException sqle) {

}

return mapping.findForward(SUCCESS);

[2691 byte] By [suse69a] at [2007-11-27 10:44:06]
# 1

Please post the complete stacktrace and your struts-config.xml file

Manuel Leiria

manuel.leiriaa at 2007-7-28 20:04:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

the sturts config

<struts-config>

<data-sources>

<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">

<set-property

property="driverClassName"

value="com.mysql.jdbc.Driver" />

<set-property

property="url"

value="jdbc:mysql://localhost:3306/DBstruts" />

<set-property

property="username"

value="root" />

<set-property

property="password"

value="" />

<set-property

property="maxActive"

value="10" />

<set-property

property="maxWait"

value="5000" />

<set-property

property="defaultAutoCommit"

value="false" />

<set-property

property="defaultReadOnly"

value="false" />

<set-property

property="validationQuery"

value="SELECT COUNT(*) FROM role" />

</data-source>

</data-sources>

<form-beans>

<form-bean name="loginForm"

type="com.jjolt.jjlogin.LoginForm">

</form-bean>

</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>

<forward name="login" path="/login.jsp"/>

</global-forwards>

<action-mappings>

<action path="/login"

type="com.jjolt.jjlogin.DB"

name="loginForm"

scope="request"

validate="true"

input="/login.jsp">

<forwardname="success" path="/success.jsp"/>

<forwardname="fail"path="/fail.jsp"/>

</action>

</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

suse69a at 2007-7-28 20:04:51 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...