• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with method

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing the following code in each and every class but want to create a method that do the same thing to get me rid off of writting each time in every class. I tried a lot but failed



Can anybody please help me?

Thanks in anticipation

 
Sheriff
Posts: 28322
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why don't we start with something you tried and thought would work?
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or look up the concept of servlet filters.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And JSPs.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your favorable replies. Appreciated!

I tried a lot but invain. Kindly help me to correct this method

 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct it by getting rid of it. Authentication checking of this type should be done in a filter that you write and declare once.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Correct it by getting rid of it. Authentication checking of this type should be done in a filter that you write and declare once.



Thanks for your prompt reply

I am totally not aware of filters and use to with methods.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now would be the perfect time to learn all about servlet filters.
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Filters can work on the data coming into your web application, or on the data leaving your web application. As such they can be used for such things that are common to all your code such as security on the way in, and setting the correct header information on the way out (although there are often better ways to do the later).

I would recommend a good book on developing for the web, such as Head First Servlets and JSP. Failing that, you might want to look at the Sun Java Servlet Technology tutorial, or a specific article on The Essentials of Filters.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrew Monkhouse wrote:Filters can work on the data coming into your web application, or on the data leaving your web application. As such they can be used for such things that are common to all your code such as security on the way in, and setting the correct header information on the way out (although there are often better ways to do the later).

I would recommend a good book on developing for the web, such as Head First Servlets and JSP. Failing that, you might want to look at the Sun Java Servlet Technology tutorial, or a specific article on The Essentials of Filters.



Thanks for recomending tutorials and books but can you please solve my real problem right now as the project is in my hand and I have to deliver it within a few days. I believe the methods are still workable in java servlet but certainly will start with recomended books/tutorials

Thanks again & best regards
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:I tried a lot but invain. Kindly help me to correct this method


 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:

Farakh khan wrote:I tried a lot but invain. Kindly help me to correct this method




Wow! great! Appreciated!!

Thanks from the bottom of my heart and yes Thanks also to recommend some tutorials regarding filters. I started to studied them

Best Regards
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Farakh khan wrote:

David Newton wrote:

Farakh khan wrote:I tried a lot but invain. Kindly help me to correct this method




BUMP!!

Error:
Cannot find symbol:
-- Attributes
-- StringUtils

Shall I have to import some packages?

I tested by imorting java.util.* but not works

Please help me again. copy paste what am doing



Best Regards

 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...

Obviously you'd need to adapt it to your own environment.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:...

Obviously you'd need to adapt it to your own environment.



I not understand completely. Can you please be more specific?

Thanks again & best regards
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nobody here is going to write your code for you. I showed you one way to fix your method, including using a constant to define the attribute name, and using a third-party library to do the null/empty check. To be honest, I expected the code I wrote to be self-explanatory--I'm kind of at a loss to explain it any further without doing *all* your work for you, which is something we really try not to do here.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Nobody here is going to write your code for you. I showed you one way to fix your method, including using a constant to define the attribute name, and using a third-party library to do the null/empty check. To be honest, I expected the code I wrote to be self-explanatory--I'm kind of at a loss to explain it any further without doing *all* your work for you, which is something we really try not to do here.


I am really thankful for you as you at least tried to help. Appreciated!

Its throwing compile time error not runtime or logical errors

Anyhow working with it and if I succeeded will let you know

Thanks again
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the method the way *I* would have written it (well, with the caveat that this isn't how I'd do it). Obviously since I'm writing it the way *I* would write it I used the techniques and libraries *I* would use. You're not using them. So replace the stuff that you *don't* have with the stuff you *do*. It's really clear what the intent of the code I used is, or at least it *should* be.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:I wrote the method the way *I* would have written it (well, with the caveat that this isn't how I'd do it). Obviously since I'm writing it the way *I* would write it I used the techniques and libraries *I* would use. You're not using them. So replace the stuff that you *don't* have with the stuff you *do*. It's really clear what the intent of the code I used is, or at least it *should* be.



Thanks again for guidance. Will let you know about the progress soon

Appreciated
 
Nothing? Or something? Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic