servletsurl-pattern

How to configure my servlet to listen on URL pattern of *.ajax


I am using a servlet which URL name is general. I send some form parameters to servlet and i will handle them in Java. But how to configure my servlet to send Ajax request like getRecords.ajax or getAnother.ajax. So namely, I want to send all *.ajax to my servlet.

And then i want to handle in my servlet like this;

public getAnother (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO
return JSONObject
}

not in doGet.

And my servlet also like this (a sample servlet):

package com.domain;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class General
 */
@WebServlet("/General")
public class General extends HttpServlet {
    private static final long serialVersionUID = 1L;

     private String message;
    /**
     * Default constructor. 
     */
    public General() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
         message = "Hello World";
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         // Set response content type
          response.setContentType("text/html");
          String colorName=request.getParameter("color");
          // Actual logic goes here.
          PrintWriter out = response.getWriter();
          out.println("<h1>" + message + "</h1>");
          out.println("<br><h2>"+colorName+"</h2>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

Solution

  • The servlet mapping can be used to server particular URL. This is done in URL patterns

    @WebServlet(urlPatterns="*.ajax")
    

    Now you can use getRecords.ajax or getAnother.ajax.

    Use the @WebServlet annotation to define a servlet component in a web application. This annotation is specified on a class and contains metadata about the servlet being declared. The annotated servlet must specify at least one URL pattern. This is done by using the urlPatterns or value attribute on the annotation. All other attributes are optional, with default settings. Use the value attribute when the only attribute on the annotation is the URL pattern; otherwise use the urlPatterns attribute when other attributes are also used.