当 jsp 调用 servlet 时,上下文路径未设置在 url 中
contextpath is not setting in url when jsp to servelt is calling
我有一个名为
gitlab.configuration
的 maven web 项目
所以项目路径应该是http://localhost:8080/gitlab.configuration/
有 jsp 个文件,它包含 "a" html 标签。单击它时,我将被重定向到预定义的回调 url。这是
http://localhost:8080/gitlaboslcadaptor/oauth/callback
但正确的 url 应该是
http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback
. 而不是上面的 url
我的问题是为什么它不设置这个上下文路径?
这里我也附上了一些图片。
如果你不清楚我在说什么,请看图片。
entryPointForOauth2caller.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Oauth2caller</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container d-flex h-100">
<div class="row align-self-center w-100">
<div class="col-6 mx-auto">
<div class="jumbotron text-center">
<!-- <form action="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&response_type=code&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email" method="get">
<button class="btn btn-primary" type="submit">MakeAuthorize</button>
</form> -->
<a href="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&response_type=code&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email">Authorize</a>
</div>
</div>
</div>
</div>
</body>
</html>
Oauth2ProviderServlet.java(servlet class)
package com.persistent.unite.oslc4j.gitlab.security.oauth2;
import java.io.IOException;
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 Oauth2ProviderServlet
*/
@WebServlet("/gitlaboslcadaptor/oauth2/callback/*")
public class Oauth2ProviderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Oauth2ProviderServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init mehtod in provider");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code");
String state = request.getParameter("state");
System.out.println("call back url's code for access token: " + code);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("dopost mehtod called.");
doGet(request, response);
}
}
如果回调 url 被调用,那么我的 servlet doget 方法至少应该打印一些东西。但是我的 serlvet 似乎根本不会调用。
这里我附上了我得到的图片。
点击 "authorize" link 之后我得到以下输出。
经过几个小时的调试,我发现了这个问题。
回调 url 未正确配置。
最初是
http://localhost:8080/gitlaboslcadaptor/oauth/callback
但正确的应该是 http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback
这个url也应该正确地写在它自己的gitlab帐户中。
我有一个名为
gitlab.configuration的 maven web 项目 所以项目路径应该是
http://localhost:8080/gitlab.configuration/
有 jsp 个文件,它包含 "a" html 标签。单击它时,我将被重定向到预定义的回调 url。这是
http://localhost:8080/gitlaboslcadaptor/oauth/callback
但正确的 url 应该是
http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback. 而不是上面的 url
我的问题是为什么它不设置这个上下文路径?
这里我也附上了一些图片。
如果你不清楚我在说什么,请看图片。
entryPointForOauth2caller.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Oauth2caller</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<div class="container d-flex h-100">
<div class="row align-self-center w-100">
<div class="col-6 mx-auto">
<div class="jumbotron text-center">
<!-- <form action="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&response_type=code&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email" method="get">
<button class="btn btn-primary" type="submit">MakeAuthorize</button>
</form> -->
<a href="https://gitlab.com/oauth/authorize?client_id=153c017c0f33d32abfb8b950983dcfce0cbe7ba49751033c0626c4f00f51ac72&response_type=code&redirect_uri=http://localhost:8080/gitlaboslcadaptor/oauth2/callback&state=941891abc&scope=api+read_user+read_registry+sudo+openid+profile+email">Authorize</a>
</div>
</div>
</div>
</div>
</body>
</html>
Oauth2ProviderServlet.java(servlet class)
package com.persistent.unite.oslc4j.gitlab.security.oauth2;
import java.io.IOException;
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 Oauth2ProviderServlet
*/
@WebServlet("/gitlaboslcadaptor/oauth2/callback/*")
public class Oauth2ProviderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Oauth2ProviderServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
System.out.println("init mehtod in provider");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String code = request.getParameter("code");
String state = request.getParameter("state");
System.out.println("call back url's code for access token: " + code);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("dopost mehtod called.");
doGet(request, response);
}
}
如果回调 url 被调用,那么我的 servlet doget 方法至少应该打印一些东西。但是我的 serlvet 似乎根本不会调用。
这里我附上了我得到的图片。
点击 "authorize" link 之后我得到以下输出。
经过几个小时的调试,我发现了这个问题。 回调 url 未正确配置。 最初是
http://localhost:8080/gitlaboslcadaptor/oauth/callback但正确的应该是
http://localhost:8080/gitlab.configuration/gitlaboslcadaptor/oauth/callback
这个url也应该正确地写在它自己的gitlab帐户中。