Link to home
Start Free TrialLog in
Avatar of SheppardDigital
SheppardDigital

asked on

Mapping a URL to a specific servlet method

I'm just starting out using Java after using PHP for the last 15 years or so.

What I'd like to know is, is there a way to map a url i.e. /myservlet/mymethod/ to a specific method in the servlet?

I know this is bordering on MVC framework functionality, but it's mainly for my own knowledge and experience.

If the above can't be configured, I guess it would be possible to create code in the doGet() method which tries to determine a method name from the last part of the URL, and if the method exists, call it?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can determine what method to call from a parameter passed into that url
I guess it would be possible to create code in the doGet() method which tries to determine a method name from the last part of the URL, and if the method exists, call it

Yes
Avatar of SheppardDigital
SheppardDigital

ASKER

Cool, based on that then I've come up with the following, am I on the right lines?

public HttpServletResponse myMethod2(HttpServletRequest request, HttpServletResponse response) {
    	// Do something with the response in this method
    	
    	return response;
    }
    
    public HttpServletResponse myMethod(HttpServletRequest request, HttpServletResponse response) {
    	// Do something with the response in this method
    	
    	return response;
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] pathParts = request.getPathInfo().split("/");
		String methodName = "defaultMethod";
		
		if (pathParts[1] != null) {
			methodName = pathParts[1];					
		}
		
		try {
			Method m = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
			response = (HttpServletResponse) m.invoke(this, request, response);	
		} catch (NoSuchMethodException e) {	
			System.out.println("No Such Method");
		} catch (IllegalAccessException e) {	
			System.out.println("Illegal Access");
		} catch (InvocationTargetException e) {
			System.out.println("Invocation Target");
		}	
	}

Open in new window


At the minute I'm only returning the response from the child methods, but I'm guessing I'll need to return some kind of array containing both the request and response.
SOLUTION
Avatar of gurpsbassi
gurpsbassi
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Working with neat URL's feels much better and more organised (my opinion).

http://www.mydomain.com/invoice/view/67/
vs
http://www.mydomain.com/invoice?m=view&id=67

This isn't for a production system, I'm simply trying to transfer some of my knowledge of PHP over the Java, having built my own PHP framework and CMS I'm simply thinking of things I can learn how to do in Java to help me understand the language more and get a feel for it.

I do however appreciate your comment, and will certainly consider your suggestion should I need to work on an actual project in Java.

Back to my original issue (which isn't really an issue now). I've started to create a new class 'BaseController'. My servlet will extend BaseController, and it will then extend HttpServlet. That way I can put login into BaseController to store the request and response so they're not being passed from method to method. It also allows me to put a method in BaseController to determine the method that should be called. That way, future servlets simply need to extend the BaseController and saves copying methods from class to class and having to change all classes should something need amending in future.

Like I say, it's simply a learning exercise to help me understand how things can be done.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Both responses helped, thanks