aspose file tools*
The moose likes Struts and the fly likes Action Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Action Class" Watch "Action Class" New topic
Author

Action Class

Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
I can pass a value from an action class to a jsp

by doing

public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) {

String userID = "80"

request.setAttribute("userid", userId);

and using request.getAttribute("userid") to retrieve the value is a jsp file.

However, I need to use this value in normal java class, how do I get this value? I tried to create a new instance of the action class, but it doesn't work.

Thanks
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Sege,

can you post the code that you tried?

Herman
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Here is the code in my action class

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {




String header = request.getHeader("USer-Info");
try {
StringTokenizer st = new StringTokenizer(header, "=");
String accountID = st.nextToken();
String loginName = st.nextToken();
String login_ID = st.nextToken();
String userId = st.nextToken();
request.setAttribute("userid", userId);
}
catch(NullPointerException e)
{

}


The String userId contains the value I need in my java class....somewhere in my java helper class package, I have this java class

public class OwnerContext
{

private String userId= "flex_demo@test.com";


public void getContractNames() throws SQLException, ClassNotFoundException{
DatabaseConnection dataConn=new DatabaseConnection();
dataConn.openConn();
StringBuffer theQuery =new StringBuffer();
theQuery.append("select item_title, mi_partition_key from im_flex_unbilled_charges, uup_descriptions ");
theQuery.append("where im_flex_unbilled_charges.contract_id = uup_descriptions.contract_id and uup_descriptions.item_code = 1 ");
theQuery.append("and my_centre_01 in(select my_centre_1 from uup_tiering where user_id = 'flex_demo@test.com' group by cost_centre_1) ");
theQuery.append("and my_centre_02 in(select my_centre_2 from
theQuery.append("group by item_title, mi_partition_key");
String query=theQuery.toString();
System.out.println(query);
java.sql.Statement statement =dataConn.connection.createStatement();
dataConn.RS = statement.executeQuery(query);
while(dataConn.RS.next())
{
itemTitles.add(dataConn.RS.getString(1));
partitionKeys.add(dataConn.RS.getString(2));
}
dataConn.closeConn();
}

***I have not posted the whole chunk of the code, from here I hope you can see what i am trying to achieve.

As you can see, I have hardcoded the user ID in my java class. I need to get the user ID from the action class dynamically and pass it to the Ownercontext class. I am having trouble achieving that. I would really appreciate your help
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Sege,

that's easy: add the username as a parameter



Or if you don't want to add a String as an inputparameter:



I assume you set the userId's value somewhere?

If this is production code, please use PreparedStatement, Statements are unsafe to SQLinjection.
And you will not need those single and double quotes anymore.
And make sure you ALWAYS close resultset, statement and connection, that means: do it in a finally block.

Herman

[ October 15, 2007: Message edited by: Herman Scheltinga ]
[ October 15, 2007: Message edited by: Herman Scheltinga ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Action Class