I am currently involved in a project where I am attempting to retrieve the username from a session that was created using the code below:
GetCurrentUserInfo.java
package servlet;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetCurrentUserInfo extends HttpServlet
{
ServletContext contx = null;
public void init(ServletConfig config) throws ServletException
{
contx = config.getServletContext();
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
try
{
OutputStream outer = res.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(outer);
String userName = (String) req.getSession().getAttribute("username");
oos.writeChars(userName);
oos.flush();
oos.close();
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
Calling.js
function getUserInfo()
{
var userInfoHttp;
if (window.XMLHttpRequest)
{
userInfoHttp = new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
userInfoHttp = new ActiveXObject("Microsoft.XMLHTTP")
}
userInfoHttp.open("POST", "GetCurrentUserInfo", false);
userInfoHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
userInfoHttp.onreadystatechange = function ()
{
if (userInfoHttp.readyState == 4)
{
if (userInfoHttp.status == 200)
{
var res = TrimString(userInfoHttp.responseText);
alert(res);
}
}
}
userInfoHttp.send(null);
isAnnotationUpdate = false;
}
In response, I am receiving the username with additional characters like this: "ws@s.com" But my actual username is [email protected].