Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6220755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:00:48+00:00 2026-05-24T08:00:48+00:00

Suppose I want to create a service layer for my web application which uses

  • 0

Suppose I want to create a service layer for my web application which uses servlets,How should I go about this?(I am not using a web app framework..So,please bear with me).Should I implement it as a listener?The service is meant to do database access.That is,I should be able to call from my servlet

class MyServlet{
...
    doPost(...){
    ...
        MyEntity entity = dbAccessService.getMyEntity(someId);
        ...
    }
}

Where the dbAccessService should deal with hibernate session,transactions etc.Previously I used to do all this inside dao methods, but I was advised that was not a good idea.

Any suggestions welcome

thanks

mark

Sample code snippet is given below

    class DBAccessServiceImpl{
    ...
        private MyEntity getMyEntity(Long id){
                Transaction tx = null;
                MyEntity me = null;
                Session session = HibernateUtil.getCurrentSession();
                try{
                    tx = session.beginTransaction();
                    return entitydao.findEntityById(id);
                }catch(RuntimeException e){
                    logger.info("problem occurred while calling findEntityById()");
                    throw e;
                }
            }
    ...
}

Then create a listener to instantiate DBAccessService

class MyAppListener implements ServletContextListener {

    @Override

    public void contextInitialized(ServletContextEvent ctxEvent) {

        ServletContext sc = ctxEvent.getServletContext();
        DBAccessService dbservice = new DBAccessServiceImpl();
        sc.setAttribute("dbAccessService",dbservice);

    }
}

In web.xml add listener

...
<listener>

    <listener-class>myapp.listeners.MyAppListener</listener-class>

 </listener>
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:00:48+00:00Added an answer on May 24, 2026 at 8:00 am

    Assuming you do not want to introduce a framework, two options make sense (in my opinion):

    1. define your service layer using stateless EJB session beans. You need an EJB container.
    2. do it as always in OO languages, create an interface and a corresponding implementation:

    Define an interface

    public interface BusinessService {
        abstract public BusinessObject performSomeOperation(SomeInput input);
    }
    

    And an implementation

    public class BusinessServiceImpl implements BusinessService {
        public BusinessObject performSomeOperation(SomeInput input) {
            // some logic here...
        }
    }
    

    You have several options for instantiating the service. If you start from scratch with a small application it may be sufficient to simply instantiate the service inside your web application:

    BusinessService service = new BusinessServiceImpl();
    service.performSomeOperation(...);
    

    BTW: At a later time you may want to refactor and implement some abstractions around the Service instantiation (Factory pattern, dependency injection, etc.). Furthermore, in large systems there is a chance that you have to host the service layer on it’s own infrastructure for scalability, so that your webapp communicates with the service layer via an open protocol, be it RESTful or Web Services.

    However the future looks like, having a well defined interface defining your business functions in place, allows you to “easily” move forward if the application grows.

    Response to your update:
    I would not implement the service itself as a listener, this does not make sense. Nevertheless, your sample code seems to be reasonable, but you must distinguish between the Service (in this case DBAccessService) and the way you instantiate/retrieve it (the listener). The listener you’ve implemented plays in fact the role of a ServiceLocator which is capable of finding a certain services. If you store the instance of your Service in the servlet context you have to remind that the service implementation must be thread safe.

    You have to be carefull to not over-engineer your design – keep it simple as long as you cannot foresee further, complex requirements. If it’s not yet complex I suggest to encapsulate the implementation using a simple static factory method:

    public final class ServiceFactory {
    
        public static DBAccessService getDBAccessService() {
            DBAccessService service = new DBAccessServiceImpl();
            return service;
        }
    }
    

    Complex alternatives are available to implement the ServiceFactory and nowadays some call it anti-pattern. But as long as you do not want to start with dependency injection (etc.) this one is still a valid solution. The service implementation DBAccessServiceImpl is accessed at one place only (the factory). As I mentioned before – keep an eye on multi-threading… hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create sub-domains using PHP on the fly. Suppose a user registers
Suppose I want to create a (stateless) WCF service with three methods exposed on
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose I want to create a set of observers based on type. That is
I want to create a very basic 3D modeling tool. The application is supposed
Suppose I want to open a file in an existing Emacs session using su
Suppose you want to add an extra layer of credentials on top of a
I'm curious about the best practice in creating web services which support both XML
I am currently trying to create a REST web service for my django site
Suppose I have an immutable NSArray and want to create several sub-arrays. I could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.