2014年5月14日星期三

Beginner , if handled ajax and servlet relations

beginner java using ajax request data receiving servlet, then call the servlet class so I equivalent when a multi- layer concept.

functions to achieve , but now there is a problem add edit delete it so requests every request I have to build a new servlet

everyone is doing it? Simplify it? I mean without using spring struts framework and so on .


Of course I do not want to get parameter to distinguish the way requse.getParameter execution method . .

is how we deal with. beginner on coaching
------ Solution --------------------------------- -----------
should be able to use the same servlet, and then distribute the landlord can investigate it
------ Solution ----------- ---------------------------------
same servlet, pass the value when the method name passed to the background doing compared to determine which one to call .
For example : url:? xxxx / xxx m = method_name, get the value of m in the background , and compare your method names .
------ Solution ---------------------------------------- ----
from the top two floors. . Some of it you can operate inside wrote method can be invoked inside dopost, doget, the second floor of the method used to call , or if esle switch to call the
------ Solution --- more -----------------------------------------
approach.
1. you can be equipped with multiple servlet (@ WebServlet ({servlet1, servlet2 ...})) annotation configuration is also very convenient.
2 you pass a parameter when accessing the servlet , such as :.? xxxx.do method = add, and then you get your servlet by this parameter , then the request is distributed on OK , a simple code to write about , you can read on line:
doPost (request, response) {
String method = request.getParameter ("method");
if (method.equals "add") {
this.add (request, response);
}
if (method.equals "delete") {
this.delete (request, response);
}
}

private void add (request, response) {
....
}

private void delete (request, response) {
....
}
------ Solution ----------------------------------- ---------
4 floor switch I think it is better
------ Solution -------------------- ------------------------


String uri = request.getRequestURI ();
String action = uri.substring (uri.lastIndexOf ("/"), uri.lastIndexOf () ".");
if (action.equals ("/ add")) {
block ......
} else if (action.equals ("/ edit")) {
block ......
} else if (action.equals ("/ delete")) {
block ......
}

------ For reference only ---------------------------------- -----
Oh, I do not want to use a method of regional services parameters do

没有评论:

发表评论