一个Servlet提出请求,而我们想知道这个请求是谁请求、端口、上下文环境、路径等信息,可以对这些信息作比较,再进行下一步的处理。
package org.ajava.example.servlet;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletUrlInformation extends HttpServlet implements Servlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String url = request.getRequestURL().toString();//取得请求的URL
String queryString = request.getQueryString();//取得请求字符串
String uri = request.getRequestURI();//取得请求URI
String scheme = request.getScheme();
String serverName = request.getServerName();//服务名
int portNumber = request.getServerPort();//端口
String contextPath = request.getContextPath();//上下文环境
String servletPath = request.getServletPath();//路径
String pathInfo = request.getPathInfo();//路径信息
String query = request.getQueryString();
}