struts connecting database
843859Jul 16 2007 — edited Jul 20 2007I want to do this, When the user input is processed, it will be checked against the database to see what�s 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 width="100%" 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);