如何配置我的 servlet 以发送 Ajax 请求
How to configure my servlet for sending Ajax request
我正在使用 URL 名称通用的 servlet。我将一些表单参数发送到 servlet,我将在 Java 中处理它们。但是如何配置我的 servlet 以发送 Ajax 请求,如 getRecords.ajax
或 getAnother.ajax.
也就是说,我想将所有 *.ajax
发送到我的 servlet。
然后我想像这样在我的 servlet 中处理;
public getAnother (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO
return JSONObject
}
不在 doGet
中。
我的 servlet 也喜欢这样(一个 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
}
servlet 映射可用于特定服务器 URL。这是在 URL 模式
中完成的
@WebServlet(urlPatterns="/General/*.ajax")
现在您可以使用 /General/getRecords.ajax
或 /General/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.
我正在使用 URL 名称通用的 servlet。我将一些表单参数发送到 servlet,我将在 Java 中处理它们。但是如何配置我的 servlet 以发送 Ajax 请求,如 getRecords.ajax
或 getAnother.ajax.
也就是说,我想将所有 *.ajax
发送到我的 servlet。
然后我想像这样在我的 servlet 中处理;
public getAnother (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO
return JSONObject
}
不在 doGet
中。
我的 servlet 也喜欢这样(一个 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
}
servlet 映射可用于特定服务器 URL。这是在 URL 模式
中完成的@WebServlet(urlPatterns="/General/*.ajax")
现在您可以使用 /General/getRecords.ajax
或 /General/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.