Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Personally I use reflection in my controllers(servlets) which basically let me achieve this.</p> <p>If I have a servlet called UserController</p> <p>The main url to call the servlet will be /user. Knowing this, I always pass my first parameter as ?action=add</p> <p>Then in my servlet I have a method called add or actionAdd. Whichever you prefer.</p> <p>Then I use the following code;</p> <pre><code>String str = String str = request.getParameter("action").toLowerCase(); Method method = getClass().getMethod(str, HttpServletRequest.class, HttpServletResponse.class); method.invoke(this, request, response); </code></pre> <p>Explanation:</p> <p>str will have the action parameters value, add in this case. Method method will be a reference to a method with the given name(str) and its expected parameter types. </p> <p>I then invoke the method, passing the context, request and response.</p> <p>The add method would look something like this;</p> <pre><code>public void add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { //do add stuff String url = "/user/index.jsp"; RequestDispatcher dispatcher = context.getRequestDispatcher(url); request.setAttribute("User", user); dispatcher.forward(request, response); } </code></pre> <p>I don't know about only passing back a string. But this should get you a basic idea.</p> <p><strong>Do note that reflection can cost you, altohugh it shouldnt really affect you much like this. And it is error prone as method names/signatures need to match perfectly.</strong></p> <p>So from jquery you would do an ajax request to the url:</p> <pre><code> localhost/projectname/user/add (if you use urlrewrite) or localhost/projectname/user?action=add (if you dont) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload