• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Action Class

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Sege,

can you post the code that you tried?

Herman
 
Sege Stephen
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic