0

I have set the user ID and password values from a login page for a session using the following code in a .jsp file:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <script>alert("Hey")</script>
        <%
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            session.setAttribute("username", userName);
            session.setAttribute("password", password);
            response.sendRedirect("somefile.jsp");
        %>
    </body>

I want to access the username in a .js file, to send it to REST API. Tried this:

var uName = <%=session.getAttribute("username")%>;

But that doesn't work, since <% won't get parsed as needed in a .js file. Any suggestions?

user3377812
  • 33
  • 2
  • 8

1 Answers1

1

You have four options.

  • Read the data from the URL directly with JS and don't involve JSP at all (but you shouldn't be passing passwords about in the query string in the first place)
  • Generate your JS from JSP
  • Inject the data into the HTML (e.g. using a data-* attribute) when the HTML document is generated from JSP and then read it from there with JS
  • Create a web service that supplies the data and make an Ajax request to it
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335