0

I'm using the design pattern found here. It has a FrontController, Command -> Service, and Dao.

If I load this URL in my browser (viewprofile is the name of the jsp):

http://localhost:8080/MyApplication/FrontController/viewprofile

I don't know how to automatically invoke ViewProfileCommand to then display the details of a user.

Currently the way I have it is like this, in order to invoke ViewProfileCommand I have to concatenate it onto the URL like: http://localhost:8080/MyApplication/FrontController/viewprofile?action=ViewProfile.

How do I map a jsp to a command without having to concatenate it onto the URL? Thanks

FrontController:

package com.secret.bookstore.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.secret.bookstore.command.Command;
import com.secret.bookstore.command.CommandFactory;
import com.secret.bookstore.exceptions.CommandCreationException;
import com.sun.net.httpserver.Filter.Chain;

/*
 * Front Controller (Mediator Pattern)
 */
@WebServlet(urlPatterns={"/FrontController"})
public class FrontController extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public FrontController(){
        super();
    }

    protected void service(HttpServletRequest request, HttpServletResponse response){
        String action = request.getParameter("action");

        CommandFactory commandFactory = CommandFactory.getInstance();
        Command command = null;
        String view = null;

        view = request.getPathInfo().substring(1);

        if(action != null){
            try {
                command = commandFactory.createCommand(action);
                view = command.execute(request, response);
            } catch(CommandCreationException e) {
                e.printStackTrace();
            }
        }

        forwardToPage(request, response, view);
    }

    /**
     * Forward to server to the supplied page
     */
    private void forwardToPage(HttpServletRequest request, HttpServletResponse response, String view){

        //Get the request dispatcher object and forward the request to the appropriate JSP page...
        if(view.equals(request.getPathInfo().substring(1))){
                RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/" + view + ".jsp");

                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        } else {
            try {
                response.sendRedirect(view);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

}
Community
  • 1
  • 1
Jonathan
  • 3,016
  • 9
  • 43
  • 74

1 Answers1

2

You have designed your front controller to determine actions based on request parameters instead of request path info. So you really have to supply a request parameter in order to invoke an action.

The front controller example in the linked answer determines the action based on request method and request path info which is also a more sensible way. You'd need to do the same in order to achieve your functional requirement of being able to preprocess GET requests.

Basically:

command = commandFactory.createCommand(request.getMethod(), request.getPathInfo());

or just as in the given example:

command = commandFactory.createCommand(request);
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555