mercredi 10 février 2016

Servlet WebServlet multiple urlPattern

I must write a servlet that should do multiple operations according to the button I press on the html page.

I have one button that is a Insert into the Db, another is a Delete, another is a Select.

So I would like my servlet will do all those three operations in the doGet method.

My clue was to put

@WebServlet("select", "insert", "delete")
public class MyServlet extends HttpServlet {
    ...

        @Override
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                String path = request.getServletPath()!=null?request.getServletPath():"";
                switch(path){
                case "/select":
                    ...
                break;
                switch(path){
                case "/insert":
                    ...
                break;
                switch(path){
                case "/delete":
                    ...
                break;

then, into the javascript file it would have been (for select)

var req={
        method: 'GET',
        url: 'http://localhost:8080/AngularDb/select',
    }
$http(req);

and into the web.xml

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

But all this doesn't work.

May you tell me what is wrong?

Aucun commentaire:

Enregistrer un commentaire