有没有办法在 servlet 请求中获取用户数据?

Is there any way to get user data in a servlet request?


我是新来的,但已多次阅读此页面。我有一个复杂的问题,我不是英语母语所以首先请原谅我,如果我不明白。
我为我们的旧 Oracle Portal 10g 服务器编写了一个 portlet。在 portlet 中,javascript 函数 post 将一些数据 ajax 发送到 servlet。 我想在调用 servlet 时识别用户。我可以从 portlet 会话中传递一些东西吗?就像在 portlet 中一样,有一个 PortletRenderRequest,我可以获得该用户的用户名和权限。
我的理论 portlet 功能简而言之:它是一个表单,如果有人填写它,并将带有 javascript ajax 的数据发送到 servlet,我想检查一下,发件人有权将数据插入到数据库(什么是 her/his 名称)。现在它可以工作了,那个 portlet 获取用户数据并写入一个输入隐藏元素值,然后 javascript 读取它,并用 post 发送,但这很容易被破解,而且是糟糕的解决方案。

现在理论上是这样的:
portlet.jsp:

<%
    PortletRenderRequest pReq = (PortletRenderRequest)request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);  
    String userNev = pReq.getUser().getName();
    session.setAttribute("username",userNev);
    System.out.println("session ID in JSP: "+session.getId());
%>
<button class="btn" type="button">Upload data</button>
<script type="text/javascript">
    $(function() {
        $(".btn").click(function(){
            /*.....*/
            $.post( "/demo/servlet", poststring);
        });
    });
</script>

servlet:

public class servlet extends HttpServlet {
    public void doPost(HttpServletRequest request, 
                       HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = (request.getSession());
        System.out.println("Session ID in servlet: "+session.getId());
        /*....*/
    }
}


目前在 JDeveloper 10.1.3.5 中是 运行。
每次页面刷新在控制台中显示不同的 "session ID in JSP",但 "Session ID in servlet" 没有变化,所以它必须是正确的。
所以现在唯一的任务是将数据放在正确的 HttpSession 中,我只需要以某种方式访问​​ jsp 页面。 HttpSession Session = request.getSession(); 在 jsp 页面返回错误的会话(每次刷新都会改变)。

编辑:
毕竟@Jonathan 回答了这个问题。这会导致其他问题,例如如何在 jsp 页面上获取 HttpSession?
我发现,jsp 页面上的 session = HttpSession = ProviderSession,不等于 servlet.
中的 HttpSession (如果我将提供商注册页面上的登录频率设置为 "always",jsp 页面上的 ProviderSession 不会随着页面刷新而改变)
我真的不知道 ProviderSession 和 HttpSession 之间的区别,以及如何从一个访问到另一个。如果我可以从 servlet 到达 ProviderSession,从使用角度来看它会是一样的吗?

Application Session exists in both Servlet and Portlet containers. It is one of the ways of sharing data (crude Inter-Portlet Communication) from the render phase to the action phase (or any lower phases) in the portlet containers. what is the difference between a portlet and a servlet?

因此,当用户登录到您的应用程序时,您设置了一个会话变量,在本例中为 "loggedInUser"。 然后在 servlet 中,在执行任何数据库请求之前,检查会话变量是否为空。像这样:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       HttpSession session = (request.getSession());
         //check if there is a loggedInUser session variable
         if(null == session.getAttribute("loggedInUser")){              
            request.setAttribute("pleaselogin", "Not Logged In. Login or register to do this.");            
             RequestDispatcher rd=request.getRequestDispatcher("Error.html");   
               rd.forward(request,response);        
        }else{

   String username = (String) session.getAttribute("loggedInUser");


   //do database logic

}

}