简体   繁体   English

检索servlet中的上一个页面路径?

[英]Retrieve previous page path in servlet?

I am trying to keep the user trace if he has no privileges then return him back to page from where he request (previous page path) with message. 我试图保留用户的权限(如果他没有特权),然后将其返回到他所请求的页面(先前的页面路径)并显示消息。 In doGet() when I try to get request url by using request.getPathInfo() it gives null in jboss console. doGet()当我尝试通过使用request.getPathInfo()获取请求URL时,它在jboss控制台中为null

My doGet source is following from servlet: 我的doGet源代码来自servlet:

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    if (!validateUserSession(request, response)) {
        return;
    }

    String pathTrace = request.getPathInfo();
    System.out.println("Request is comming from : " + pathTrace); // null

    loadNavigation(request, response);

    ServletContext context = getServletContext();
    RequestDispatcher rd = context
            .getRequestDispatcher("/jsp/admin_account/InviteUser.jsp");
    rd.forward(request, response);
}

Get it from the request headers. 从请求标头获取它。

String pathTrace = request.getHeader("referer");
System.out.println("Request is comming from : " + pathTrace);

From JavaDocs , getPathInfo may return NULL JavaDocs ,getPathInfo可能返回NULL

public java.lang.String getPathInfo() 公共java.lang.String getPathInfo()

Returns any extra path information associated with the URL the client sent when it made this request. 返回与客户端发出此请求时发送的URL关联的任何其他路径信息。 The extra path information follows the servlet path but precedes the query string. 额外的路径信息在servlet路径之后,但在查询字符串之前。 This method returns null if there was no extra path information. 如果没有多余的路径信息,则此方法返回null。

Use below API 使用以下API

public java.lang.String getServletPath() 公共java.lang.String getServletPath()

Returns the part of this request's URL that calls the servlet. 返回此请求的URL中调用servlet的部分。 This includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. 这包括servlet名称或servlet的路径,但不包括任何额外的路径信息或查询字符串。 Same as the value of the CGI variable SCRIPT_NAME. 与CGI变量SCRIPT_NAME的值相同。 Returns: a String containing the name or path of the servlet being called, as specified in the request URL, decoded. 返回:一个字符串,其中包含在请求URL中指定的,被解码的servlet的名称或路径。

EDIT: Since you need passing servlet URL 编辑:由于您需要传递servlet URL

Add referrer_url as one more param in the passing servlet and pass the value to other servlet where you are trying to get the path 将Referrer_url作为传递的servlet中的另一个参数添加,并将值传递给您试图获取路径的其他servlet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM