0

I need a little advise as to how to structure classes in my web application.I have a webstore where items can be added,edited,listed and deleted.When I tried to write servlets for this,it came out like

    ItemAddServlet ,ItemEditServlet,ItemsListServlet,ItemDeleteServlet,
ManufacturerAddServlet,ManufacturerEditServlet,ManufacturerListServlet

ItemAddServlet is

public class ItemAddServlet extends HttpServlet {
    private ItemDaoFactory bfactory = ItemDaoFactory.getInstance();
    private ItemDaoImpl itemdaoimpl = (ItemDaoImpl) bfactory.getDao();
    private ManufacturerDaoFactory mfactory = ManufacturerDaoFactory.getInstance();
    private ManufacturerDaoImpl mandaoimpl = (ManufacturerDaoImpl) mfactory.getDao();

    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        List<Manufacturer> manufacturers = mandaoimpl.findAllManufacturers();
        request.setAttribute("manufacturers",manufacturers);
        RequestDispatcher dispatcher = request.getRequestDispatcher("itemaddedit.jsp");     
        dispatcher.forward(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{   
        String name = request.getParameter("name");     
        String price = request.getParameter("price");
        String manId = request.getParameter("manufacturer");
        Manufacturer manufacturer = mandaoimpl.findManufacturerById(Long.parseLong(manufacturerId));    
        Item item = new Item();
        item.setName(name);     
        item.setPrice(Float.parseFloat(price));
        item.setManufacturer(manufacturer);     
        itemdaoimpl.saveOrUpdateItem(item);     
        response.sendRedirect("listitems");
    }
}

The other servlets are similar,with changes according to the logic involved..

I have mapped the url-patterns to servlets as below

...
<servlet-mapping>
  <servlet-name>itemaddservlet</servlet-name>
  <url-pattern>/createitem</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
  <servlet-name>itemeditservlet</servlet-name>
  <url-pattern>/edititem</url-pattern>
 </servlet-mapping>
  <servlet-mapping>
  <servlet-name>itemlistservlet</servlet-name>
  <url-pattern>/listitems</url-pattern>
 </servlet-mapping>
...

I am aware that there are too many servlets here ..It doesn't look like a good design..Can I do the add/edit /list/delete functionalities using just one servlet?That would shrink my servlets to

ItemServlet,ManufacturerServlet

Is that the correct way of doing this?How would I map the url-patterns in this case? /createitem /edititem /listitems all will map to ItemServlet?

I would be grateful if someone can point out better ways of structuring the code..

thanks

mark

markjason72
  • 1,653
  • 8
  • 27
  • 47
  • You need the Front Controller pattern. See also http://stackoverflow.com/questions/3541077/design-patterns-web-based-applications/ Better is of course to throw in some MVC framework with a front controller like JSF, Spring-MVC, Struts2, etc so that you end up with defining just a view and a model yourself without the need to repeat the controller for every view and model. – BalusC Jun 28 '11 at 21:07

1 Answers1

1

There are several frameworks for helping out with this pattern. If you're wanting to use pure servlets, the easiest thing would be to pass a parameter to the servlet indicating the operation you're trying to perform (i.e. create, edit, list).

A couple frameworks that you might find helpful are below

  • Spring
  • MVC Struts
tjg184
  • 4,508
  • 1
  • 27
  • 54