我试图将java类与JSP结合使用。 当服务器启动时,用户转到JSP,它将它们重定向到第三方的身份验证表单。 在身份验证流程之后,他们可以填写他们的信息,按提交,并将该信息发送到HTTP帖子中的服务器。 我遇到的问题是将变量从我的JSP传递给我的java类。 我怀疑我可能需要使用一个servlet,但是我想在将它们全部移动到servlet之前看看是否有办法只使用Java类/ bean。 我知道我可能要调用变量,但我不知道该怎么做。 任何答案或建议都会非常有帮助! 我是一个编码的初学者,但我真的想让这个小应用程序运行起来。 谢谢!
serverFlow.java
public class serverFlow {
public StringBuffer getAccessToken(String redirectUri, String clientId, String clientSecret, String authCode)
throws IOException {
// Define base URL
String baseUrl = "https://idfed.constantcontact.com/as/token.oauth2";
// Build URL
String fullUrl = baseUrl + "?code=" + authCode + "&redirect_uri=" + redirectUri
+ "&grant_type=authorization_code&scope=contact_data";
URL authorizeUrl = new URL(fullUrl);
// Open connection
HttpURLConnection con = (HttpURLConnection) authorizeUrl.openConnection();
// Encode Auth Info
String credentials = clientId + ":" + clientSecret;
String auth = "Basic " + Base64.getEncoder().encodeToString(credentials.getBytes());
// Post Method for CTCT request
con.setRequestMethod("POST");
// Add Headers
con.setRequestProperty("Authorization", auth);
// Open input steam
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer result = new StringBuffer();
// Append each line
while ((inputLine = in.readLine()) != null) {
result.append(inputLine);
}
// Close the stream
in.close();
return result;
}
public static void main(String[] args){
serverFlow server = new serverFlow();
// Print string buffered response of access token, refresh token and authentication type
// Attempted to use a reqeust but unable to utilize 'reqeust.' is there a way to get the authCode in this Java class from my JSP?
System.out.println(server.getAccessToken("http%3A%2F%2Flocalhost%3A8080%2Fsignup", clientId, clientSecret, authCode));
}
}
signup.jsp
My JmmlContact Information
%>
// This is where the authCode is generated, how do I get this back over to my serverFlow.jsp?
String authCode = "";
try {
authCode = request.getParameter("code");
request.setAttribute("authCode", authCode);
// Verifies it in console
System.out.println("Here is the auth code: " + authCode);
}catch(Exception a){
System.out.println("Could not get Auth Code: " + a);
}
}%>
Email Address
}%>