Jsp session variable in javascript

hi,
i want use a jsp session variable inside a javascript code, for this i am using the following function,
function checkList()
<% String ecode1=(String)session.getAttribute("e_numb"); %>
var code=<%=ecode1%>
alert (code)
this is showing an error can any one please help me,
thanks
saurabh

You have ',%String ecode1' within the javascript. JSP will not recognize 'String' as a java class unless you import the String class. Something like <%@ page import = "java.util.* " %> Incorrect. String is part of java.lang, which is always imported into a java class.
Most probably what is missing in the javascript is
- missing quotes ? Not sure if this value is meant to be interpreted as a number or not.
- missing semicolon
function checkList()
<% String ecode1=(String)session.getAttribute("e_numb"); %>
var code="<%=ecode1%>";
alert (code);
}Lets assume that the value of e_numb is "42"
after running this as a jsp, it should produce the following on the resulting html page. You can see it by viewing the source.
function checkList()
var code="42";
alert (code);
}Check that to see it has produced valid javascript
Cheers,
evnafets

Similar Messages

  • Urgent help required...for replacing JSP session variables

    I am trying to enhance the performance of a j2ee based webapp which use session variables to store the data...
    Is there any alternative for session variables..The project is based on j2ee-Struts frame work..But the amount of code maintainance should be minimum ....so i can't create beanforms for all these data...
    Is there any XML based methods available...??

    ok... thanks... i created a fla file and inthe action script
    pasted the following code
    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://flash.ispstream.com/ondemand/unikron/my_video_dj/");
    var ns:NetStream = new NetStream(nc);
    contentMain.myVideo.attachVideo(ns);
    ns.setBufferTime(5);
    ns.play("dvd_r");
    nothing is working if i tried to load... can u help me out

  • Jsp session variable to call in  java class.

    here i am facing a problem in calling a jsp session varibale to java class.
    in my code.. i have seperate files for jsp pages and packaged java classes.
    as i am developing a web tire application. the data entered in the front end i am getting to jsp page and putting the mandatory in session to use in other pages. now here the same session vaiable i have to use/call in java class file.
    Please refer me the best method and any references on this to call the session vaiable to java class file.
    in jsp page i decleared like this:
    session.setAttribute("stuIdentification",stuid);
    this id i have to use in java class.file.
    how to call it?
    Thanks
    Kurra

    Gimbal2's got the right idea.
    The reasons for choosing point 1) are also valid. Whenever possible try and do as much code that is not specifically tied to your business objects inside your dynamic element (the jsp page) instead of your static one (the compiled class)
    this allows you to very easily update your jsp to make any changes without having to recompile class code. Recompiling seems like a little thing when you are in development but imagine if the class is executed by a business critical application that faces forward to customers, recompiling will mean shutting down your servers and that may not be an option.
    Regards,
    David

  • JSP Session in a Javascript Window

    When I open a javascript window (with window.open) and use session cookies everything works. In the new javascript window I have no problems to navigate. After disabling cookies I tried to open the javascript window again and get the following errormessage:
    "Redirection limit for this URL exceeded. Unable to load the requestet Page. This may be caused by cookies that are blocked."Obviously I lose the Session in the javascript window. Does anybody know, how to open a new javascript window without loosing the session?
    Link to open the window (using struts framework):
    <html:link href="javascript:openWindow('message.do?action=show&folder=inbox','700','430','')">

    Thanks for your quick answer. Now I get the session-number in the javascript-link. But I get the same error-message again.
    Do I have to change something in my javascript method?
    function openWindow(docURL, width, height, alt) {
      var imageWidth = width;
      var imageHeight = height;
      var myLeft = (screen.width-imageWidth)/2;
      var myTop = (screen.height-imageHeight)/2;
      newWindow = window.open(docURL,"newWindow","width="+imageWidth+",height="+imageHeight+",scrollbars=yes,resizable=yes,left="+myLeft+",top="+myTop);
      newWindow.focus();

  • Inablility to retrieve values of session variables in irpt page

    We currently are using xMII 11.5.  We are having difficulty retrieving values assigned to our created session variables. 
    We have defined 2 users, each associated with session variables whose values differ by user.  Each user is associated with a different irpt page which retrieves information based on the values passed by the session variables.  Both pages return correct information in the grid using the as the param.1 value on the opening of the page. 
    On one page, we are able to retrieve the values of the session variables in Javascript by placing hidden fields on the document and using the document.getelementbyID("hiddenValue1").value.  The other irpt will only bring back the name of the of the session variable in the curly braces, .
    Both users are set up the same in System Security and Data Access.  If we switch the pages opened for the user in the Portal Navigation, one page will return the values no matter which user and the other page will always bring back the session variable name in the braces.  This makes us believe that it is not a problem with the user setup or the definition of the session variables.
    Would anyone be able to help us understand what needs to be done to correct this issue?

    Michelle,
    Have you tried URL encoding your irpt pass through parameters in your Navigation link?  Perhaps the commas and parenthesis are causing some issues ( http://help.sap.com/saphelp_xmii115/helpdata/en/Getting_Started/Template_Editor/Query_Construction.htm )
    Just to clarify, the Role/User attributes defined in LHSecurity are truly session properties, and the session properties are shown by the PropertyAccessServlet results.  Adding the name/value pairs to the URL are only a one-shot pass through to the ReportServlet that processes web pages with .irpt extenstions, so they don't actually set the session properties and are non-retentive beyond the one page.  As you are presently doing with the navigation links, we typically recommend URL passthroughs for page to page activities and using the session itself for name/value pairs that you want to set once and use anywhere throughout your application.
    document.APPLET.getPropertyValue(NAME); will also give you access to the session through javascript, but if you do a view...source in the browser after your TeamLeaderView web page has loaded you should see the results of the search/replace efforts of the ReportServlet, and the javascript you are using to get values out of hidden form field elements should simply reflect the text you see in the view source window.
    If they work inside APPLET tags like you mentioned for Param.1 you could also use document.APPLET.getQueryObject().getParam(1); instead of the hidden form field elements, and remember that you have 32 params and if they are not used in the underlying query can provide a very nice place to 'catch' the results like you are are doing with hidden form field elements that could potentially get reset by a form button.
    Regards,
    Jeremy

  • JSP Session in JavaScript variable problem

    Hi,
    I have a session variable msgSize that is being set by a Java file in the session.
    aRequest.getSession().setAttribute("msgSize", msgSize);It's value is variable depends on record count of recordset.
    In JSP page where I have to assign it's value to a javascript variable. I am doing this a s follows. I have done this in the JSP file.
    <script type="text/javascript" >
    <!--
    function getVal()
    m=${msgSize};
    -->
    </script>First Problem is I am unable to get this value inside a External JS file. It throws error but inside jsp it is working. How can i use this inside External JS?
    Second problem is once the above code executed value never refreshed to new value?
    Means first time it executed it get value 200 next time again i executed the code the real recordset is 105 but still displaying 200. But when I restart the server next time it takes value 105 by itself....
    How I tackle this problem.?
    Regards,

    problem is I am not refreshing the whole JSP page everytime, only refreshing the partial page using ajax call that's why not getting the session new value...
    any suggestions what to do to get new value?

  • Access session variable in Java Function in JSP

    Hi Experts !!
    I am developing an application using STRUTS MVC...
    Very sorry if u have problem understanding my question, i ll try to improve... and sorry if i can't post codes here
    Basic question is ....
    I want to open a word document on pageLoad of JSP, the word document is not a single document, there is a form in which there is a "name" and "template" whenever user clicks on a button there is one action attached to it which creates a copy of that template in a different folder.. on the next page OnLoad i want to open that particular document. for that i have created a variable and also have set in the session, just want to access it in the below code.
    I have a formbean in which i have a variable, the scope is session, that variable i have put it in session also. but i want to access that variable in a "java function" in JSP so that "onLoad" page that function should work.
    JSP---
    <script type="text/javascript">
    function openDocument() {
    var w = new ActiveXObject("Word.Application");
    var docText;
    var obj;
    var a;
    if (w != null) {
    w.Visible = true;
    obj = w.Documents.Open(I HAVE TO ACCESS THAT VARIABLE HERE);
    </script>
    FORMBEAN----
    public class CreateSOWFormBean extends ActionForm {
    private String workflowName;
    private String comment;
    private String sowTemplate;
    private String sowFileCreated;
    public String getSowFileCreated() {
    return sowFileCreated;
    public void setSowFileCreated(String sowFileCreated) {
    this.sowFileCreated = sowFileCreated;
    sowFileCreated is the variable that i have accessed in session and that value i have to pass in that function in JSP....
    I am aware of something like
    obj = w.Documents.Open(<%'sowFileCreated'%>);
    but i m not sure how to write....
    Plz help.....

    If you're working under a framework like struts you should definitely be using JSTL tags rather than scriptlet code to access variables within the page. With JSTL code <% codes can be almost entirely avoided.
    To transfer a value from a Servlet to a JSP don't use a session variable, use a request attribute. Session variables should only be used when values have to survive from one transaction to another.
    You can write something like:
    obj = w.Documents.open('<c:out value="${openURL}"/>');in the Javascript portion of your JSP.
    Just beware of potential problems with quotes. The coresponding code in the Servlet would be like:
    request.setAttribute("openURL", openUrl);

  • 'session' is undefined - javascript error in my JSF/JSP page

    I have the following JSF/JSP page. In onLoad() I invoke a javascript function func(). In this method, I attempt to lookup an attribute from the Http Session. When I run my application, my page is loaded with but the icon on the lower left corner of the browser indicated that there was an error. When I viewed the error, it read : 'session' is undefined.
    The 'session' variable is available in a JSP page.
    So why does my code not work from within a JSF application.
    <?xml version="1.0" encoding="UTF-8"?>
    <jsp:root version="1.2" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:ui="http://www.sun.com/web/ui">
        <jsp:directive.page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"/>   
        <f:view>
            <ui:page binding="#{bean.page1}" id="page1">
                <ui:html binding="#{bean.html1}" id="html1">
                    <ui:head binding="#{bean.head1}" id="head1">
                        </ui:head>
                    <ui:body binding="#{bean.body1}" onLoad="func()" id="body1" style="-rave-layout: grid">
                        <ui:form binding="#{bean.form1}" id="form1">                       
                            <h:inputText binding="#{bean.inputText}" id="inputText"/>
                        </ui:form>
                    </ui:body>
                </ui:html>
            </ui:page>
        </f:view>
        <script type="text/javascript">
            function func(){                  
                    alert("in func()");
                    var y = session.getAttribute("test");
                    alert(y);
        </script>
    </jsp:root>thanks,
    tsc

    Because :
    Session.getAtrribute is java-code that can only run on the server
    and
    var y = session.getAttribute is javascript-code run on the client.

  • Code to set and destroy session variables in Java Server Pages(JSP)

    code to set and destroy session variables in Java Server Pages(JSP)
    we have use following statement to set session variable
    session.setAttribute("userClient",id);
    we have use following statement to destroy session variable
    session.setAttribute("userClient","");
    and
    the session.invalidate() is not working
    Plz. solve this probem

    code to set and destroy session variables in Java
    Server Pages(JSP)
    we have use following statement to set session
    variable
    session.setAttribute("userClient",id);
    we have use following statement to destroy session
    variable
    session.setAttribute("userClient","");Perhaps if you tried using
    session.setAttribute("userClient", null);
    or
    session.removeAttribute("userClient");
    and
    the session.invalidate() is not workingNot working how?
    >
    Plz. solve this probem

  • How to use session variable in JSP function  & How to use both JSP  Servlet

    Hi,
    I am new to JSP and servlets
    Still I am devloping a website in JSP. I am not mixing JSP with servlets, but I do create Java files for bean, logic and database works.
    I try to keep the hard coding part out of JSP.
    I dont how to use both JSP and Servlets in combination.
    Hence If needed I write some functions in JSP.
    but it gives me error
    +<%! public void abc()+
    +{+
    int intUserId = Integer.valueOf((Integer) session.getAttribute("MySession_UserID"));
    +}+
    +%>+
    Saying cannot find symbol session
    1) So can u please tell how can I access session variables within JSP function
    2) And also give me some links/tutorials about useing both JSP and Servlets in combination.
    Thanks
    Venkat

    The application architecture when you use Servlets and JSP in a standard MVC pattern is explained here (under the heading "Integrating Servlets and JSP Pages") and here ...

  • How to pass a jsp variable into javascript??

    <p><%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
    <p><%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <p><html>
    <p><c:set var="binrmapi" value="http://localhost/paas-api/bingorooms.action;jsessionid=XXXXXX?userId=TEST2&sessionId=1&key=1" />
    <p><c:import var="testbinrm" url="${fn:replace(binrmapi, 'XXXXXX', jsessionid)}"/>
    <p><c:set var="tuples" value="${fn:split(testbinrm, '><')}" />
    <p>Time until next game
    <p><c:forEach var="tuple" items="${tuples}">
    <p><c:if test="${fn:contains(tuple, 'row ')}">
    <p> <p><code>
    <p> <c:set var="values" value="${fn:split(tuple, '=\"')}" />
    <p> <font color="blue">
    <p> <c:out value="${values[17]}" />
    <p><c:set var="remainingtime" value="${values[17]}" />
    <p> </font>
    <p> </code>
    <p></c:if>
    <p></c:forEach>
    <p><form name="counter"><input type="text" size="8" name="d2"></form>
    <p><script>
    <p>var milisec=0
    <p>var seconds=eval("document.myForm.remaining").value;
    <p>function display(){
    <p> if (milisec<=0){
    <p> milisec=9
    <p> seconds-=1
    <p>}
    <p>if (seconds<=-1){
    <p> milisec=0
    <p> seconds+=1
    <p> }
    <br>else
    <p> milisec-=1
    <p> document.counter.d2.value=seconds+"."+milisec
    setTimeout("display()",100)
    <p>}
    <p>display()
    <p></script>
    <p></body>
    <p></html>
    <p>This is my code that i was working on, in the jsp part of the script, i get a api call and save a value of time in the variable remainingtime.. and in the javascript, i try to have a countdown clock counting down the remaining time.. but i guess it doesnt work.. how can i get that working? thanks alot
    Message was edited by:
    hamham3001
    Message was edited by:
    hamham3001
    Message was edited by:
    hamham3001
    Message was edited by:
    hamham3001

    Re: How to pass a jsp variable into javascript??Here is the sameple one, hope it will solves your problem.
    <html>
    <body>
    <form name=f>
    <%!
    String str = "A String"
    %>
    <script>
    var avariable = <%=str%>
    <script>
    </form>
    </body>
    </html>
    Let me know if you face any problem

  • Problem accessing session variables in JSP

              Hi all,
              i am retrieving the value of session variables in a JSP page. the session varibale
              is set at the
              server side. sometimes the jsp is able to retrieve the value and sometimes it
              doesn't.
              Thanks & Regards
              Tarun
              

    if getAttribute return null so there is no such session attribute,anyway
              check your browser if cookies are enabled.
              "Tarun Mangal" <[email protected]> wrote in message
              news:400759e9$[email protected]..
              >
              > Hi all,
              > i am retrieving the value of session variables in a JSP page. the session
              varibale
              > is set at the
              > server side. sometimes the jsp is able to retrieve the value and sometimes
              it
              > doesn't.
              >
              > Thanks & Regards
              > Tarun
              

  • Problem accessing session variable from jsp

    Hi!
    I get an error retrieving a session variable from inside a JSP declaration scriptlet....
    Something like this code in a JSP page:
    <%!
    public void funtion1() {
    String parameter1 = session.getAttribute("parameter1");
    %>I have seen that I have available getContextServer() method and from there I could call getAttribute("parameter1") . But the thing is that the attribute I want to retrieve was stored in session by another Jsp page.
    Any solutions are welcome
    Cheers
    Victor

    Appropriately the above is one the reasons also You cannot use JSP implicit objects from the declaration tag. However we can think of a small workaround by passing the reference like the one discussed below
    <%!public void funtion1(HttpSession session) {
    String parameter1 = session.getAttribute("parameter1").toString();
    }%>
    <% funtion1(session);%>Hope that helps :)
    REGARDS,
    RaHuL

  • How can we  use java variable in javascript code on JSP page?

    How can we use java variable in javascript code on JSP page?
    Pls help

    Think about it:
    JSP/Java is executed on the Server and produces HTML+JavaScript.
    Then that HTML+JavaScript is transfered to the client. The client now interpretes the HTML+JavaScript.
    Obviously there's no way to access a Java variable directly from JavaScript.
    What you can do, however, is write out some JavaScript that creates a JavaScript variable containing the value of your Java variable.

  • Can I set session variables from inside my Javascript?

    hi all,
    i want to set a couple of session variable while i am inside my javascript.
    can I do that?
    m_asu

    No. JavaScript runs on the client and the session is on the server. You can use JavaScript to set parameters that get passed back in the request and can then be put in the session.

Maybe you are looking for