• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

connecting struts with database

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I 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);
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem appears to be that the Struts ActionServlet didn't initialize properly. See this link for more information.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you specified the DataSource in the struts-config.xml file??
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic