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?
SheppardDigitalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You can determine what method to call from a parameter passed into that url
0
gurpsbassiCommented:
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
0
SheppardDigitalAuthor Commented:
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

gurpsbassiCommented:
Yes you're on the right track.
Be careful someone doesn't hjiack the URL and send in a method name of something like 'service'. You might end up in a infinite loop.
0
SheppardDigitalAuthor Commented:
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.
0
CEHJCommented:
I've revised my opinion ;) What you're doing is OK - as long as you bear in mind you're reinventing the wheel (as you said)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SheppardDigitalAuthor Commented:
Both responses helped, thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.