You are here

Using logged-in User in a Post action

Submitted by fontnest on Tue, 06/23/2015 - 17:27

I am NOT a jsp person, but I managed some time ago to find the jsp page in Luminis 4 where the Person manager grabs the logged-in user information to pass as needed.

I've used this in a few instances, customizing it as needed, but now I need to grab the ID, append some furnished items and Post it to a url for an SSO operation to a third party online application (Maven "Alcohol Training" - I never needed that in college-).

Anyway, the url constructs just fine, but the operation being carried out is a "GET" instead of "POST". Not being a JSP guy, I don't know how to end the Get operation that grabs the logged-in user, then POST the assembled url.

Any help from your JSP pros or even amateurs would be appreciated.

Here's my jsp page, edited to remove actual urls and ID's.

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.pipeline.bom.Person_3" %>
<%@ page import="com.pipeline.bom.PersonManager_3" %>
<%@ page import="com.pipeline.bom.BomManagerFactory" %>
<%@ page import="com.pipeline.web.WebUtil" %>

Some Login

<%
String redirectUrl = "http://www.someplace.net/sso?api_token=976b01356a90d&school_id=098597";
String username = "";

if (WebUtil.checkRequest(request, response)) {
byte[] sessionId = WebUtil.getUserSession( request, response );
PersonManager_3 cPersonManager = (PersonManager_3)BomManagerFactory.getManager( PersonManager_3.ID );
Person_3 person = cPersonManager.getCurrentPerson();
username = person.getLogin();
}

if (username != "")
{
redirectUrl = redirectUrl + "&student_id=" + username + "&curriculum_id=5" + "&first_name=" + username + "&last_name= " + "&email_address=" + username + "@email.edu";
response.sendRedirect( redirectUrl );
}

else

out.println("There has been a serious error!!");

%>

Thanks,
David Fontenot

Luminis Version:

There are lots of ways to accomplish this, but one way would be to create a function, then call it, like so:

<%@page import="java.net.*" %>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%!
public String postData(String urlToRead, String data) {
URL url;
HttpURLConnection conn;
BufferedReader rd;
String line;
String result = "";
try {
url = new URL(urlToRead);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", data.length() + "");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
os.flush();
rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
result += line;
}
rd.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
%>

<%
postData(someUrl,someData);
%>

In this case, I'm sending json data. More jsp/iPerson info here: https://www.lumdev.net/node/9176