Same variable on every JSP page

Hi All,
I'm just a beginner with JSP so excuse me if this is a 'stupid' question.
I'm building an Application that uses MySQL database everything works ok, but
I would like to know if there is a way to have a variable that I can use on every page.
The reason is, is because I have lots of pages (and growing) where I have the
database connect string with username password, but due to security reason
the user/pass for the database will change every now and then.
I'm familiar with Coldfusion and in Coldfusion you can setup an Application.cfm file. This file is called before every other .cfm file. Is there something like that
in JSP?
The solution I thought of is, putting the connect string into a session variable.
Is this a could idea?
If not, are there any other better ways of achiving this?
Thanks for any help!
Steven.

Are you using Tomcat?
Because Tomcat supports things like Application.cfm did with CF. They go in web.xml, I believe (I don't use tomcat myself).
Your database connect (and interaction) should not be handled by individual pages. It should be in a bean, connection pooling (tomcat), JSTL, or other more scalable method (or combination of techniques that works for you). If you put it all in scriptlets, you really limit yourself later.
I am assuming from your post that the username/pass is the same for all users. If it isn't, use a session-scoped bean to hold that info.

Similar Messages

  • How to pass variables between 2 jsp pages-help

    Hi,
    I have a question like this.
    As an example consider I have 3 .jsp pages.
    page1.jsp - In this html form it has some textboxes to enter employee details.
    page2.jsp - In this page it has a "PRINT" button which allows the user to click.
    page3.jsp - A new popup window which having the same content as page2.jsp, but without the PRINT button.
    But my problem is this. In this page3.jsp,it should be display the entered textbox values as page2.jsp. But these values are not displaying here. They display as NULL.
    I use as follows, but it still not working.
    <%= request.getParameter("effDate")%>.
    Could you please tell me how to hold these values when I came from page2 to page3.
    I'm using JSP 1.2 with javaBeans.
    Thanks.

    Well,
    If you are popping up a new window, and you insisting on pulling the data out of the request's parameter map. Then you will need to add the values to the query string of your url.
    window.open("http:localhost/page3.jsp?itemOne=itemOneValue&itemTwo=ItemTwoValue, '', '');
    Alternative Option:
    When posting from page 1 to page two, store your information into session scope
    request.getSession().setAttribute("valeuOne", "valueOneValue");
    Then instead of pulling from the paramter map, use request.getSession().getAttribute("valueOne") in your third page.
    Hope that helps.
    Thanx
    Cardwell

  • POST variables in a JSP page

    I have a JSP page which recieves a number of arguments.
    One of those arguments in a desination URL. The JSP page picks this up and then uses a response.sendRedirect(URL) call to forward the user to this page.
    Is there anyway of passing these other arguments across to this destination page as hidden variables.
    I dont want to append these variables to the existing URL as they should remain hidden.
    What other methods can I use for these variables to accompany the sendRedirect call or is there a different approach thats required for this case ??
    Thanks in advance,
    Richard

    You can try this. It works.
    out.println("<html>");
    out.println("<head>");
    out.println("<title></title>");
    out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
    out.println("</head>");
    out.println("<body bgcolor=\"#FFFFFF\" text=\"#000000\" onLoad=\"javascript:document.forms[0].submit();\">");
    out.println("<form name=\"formulario\" method=\"post\" action=\"http://PutHereTheURLOfsendRedirect\">");
    // Put here your hidden variables.
    out.println("<input type=\"hidden\" name=\"hiddenField1\" value=\"hiddenvalue1\">");
    out.println("<input type=\"hidden\" name=\"hiddenField2\" value=\"hiddenvalue2\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");

  • How can i pass the same parameter from 1 jsp page to many jsp page?

    hello.....
    how can i actually pass a parameter from 1 jsp file to many other jsp files? pls help......
    thanx

    Hi
    Save the information which u want to pass to various JSP's inside Session Object.
    There is an Object called HttpSession and u can store information inside which other jsp pages within the same domain and same browser instance can use it.
    commands which are for ur interests are
    <Session Object>.setAttribute("<object name>", <value>);
    Example
    HttpSession objsession =request.getSession(true); String strName="NEED_HELP":
    objSession.setAttribute("Name", strName);
    And then u can retreive that information in your n pages.
    Read any JSP/Servlet Books and u will know about them
    Bye.

  • Need help passing variables to another jsp page

    I am working in a shopping cart with multiple lines and I am wanting to pass 2 variables to another page which is accessed from a link.
    What I have so far is the following:
    This is on the shopping cart JSP page with the link
    <input type="hidden" name="shopCartReqDate1<%=i%>" id="shopCartReqDate1<%=i%>" value="<%= retVals[9] %>">
    <input type="hidden" name="shopCartExpDate1<%=i%>" id="shopCartExpDate1<%=i%>" value="<%= retVals[10] %>">
    Need it Earlier?
    I am wanting to pass it to the HAC_Help_Text_Need_it_Earlier.jsp page
    Right now on the HAC_Help_Text_Need_it_Earlier.jsp page I have the following code:
    String shopCartReqDate1 = IBEUtil.nonNull(request.getParameter("shopCartReqDate1"));
    String shopCartExpDate1 = IBEUtil.nonNull(request.getParameter("shopCartExpDate1"));
    Do I need to create a function and do a document.getElementById?
    Thanks for the help!

    As far as I understand your question You don't have to use document.getElementById. Just submit the form and then on the next page You'll just pull the variables out from the request object.

  • Updating value of a variable on a JSP page

    Hya!
    I am very new to JSP and I am struggling to make a homepage work according to the specifications my group has decided to use. I would be grateful if you could help me in this, as it's really urgent. Let me describe the issue:
    My web site contains a set of six JavaBeans that keep information that is more a less unchangeable (it works as a table). The information that is kept by the JavaBeans can be viewed by the user once he/she makes a selection. This information then will help the user in deciding to buy a certain number of tickets. That's the point where the problem appears.
    I need the variable 'totalTickets' to vary depending on each user's purchase. I've set the original value to 100 on JavaScript but that means that in each visit this number would be reset to 100 again, as JavaScript is a client-side technology. On the other hand if I set this variable in JSP the same problem would happen. Would there be a way to allow this variable to be reset after each visit? What would I need to make this possible?
    I am looking forward to hearing from all of you.
    Yours sincerely,
    Saulo

    Is this correct,
    1. User login to the system for the first time. Then TotalTicket count will be set to 100.
    2. User purchase a ticket and the count will become one less (i.e 99).
    3. For every purchase of a ticket, count will be reduced by one.
    4. User logs out. Now let us say, the count is 95. (i.e user has purchased 5 tickets).
    5. Now again user login to the syatem. The ticket count must be initialized to 95.
    and so on.
    If this is what you want, then you must either store the TicketCount in the database or atleast in the application context.
    int totalTicket=100;
    Hashtable ticketCounterMap=(Hashtable)application.getAttribute("ticketCounterMap")
    if(ticketCounterMap == null)
         ticketCounterMap = new Hashtable();
         ticketCounterMap.add("login_user_name",new Integer(totalTicket));
         application.setAttribute("ticketCounterMap");
    else
         totalTicket = ((Integer)ticketCounterMap.get("login_user_name")).intValue();
    and in case of purchasings.
    if(purchase.....)
         totalTicket--;
         ticketCounterMap.add("login_user_name",new Integer(totalTicket));
         application.setAttribute("ticketCounterMap");
    Hope this helps.
    Sudha
    P.S:- There may be errors in the above code. This is just to give you an idea.

  • How to pass a java script variable to a JSP page

    How to assign a javascript variable value to a jsp variabe
    <script>
    var scroll="100"
    </script>
    <%
    String age=[ here i need to set the value of "scroll" ]
    %>
    is it possible ,
    thanks

    assign this scroll to a hidden field in a form
    and pass via form to server this may be one way or
    set attribute scroll and then access to server side
    may this help you
    --yogeshb                                                                                                                                                                                                                                                                                                                                                                           

  • How to use jstl variable in a jsp page

    Hi all,
    I am new to JSTL and i want to access the value of jstl in jsp.
    when i use the tag like below it is displaying the value ""
    <c:out value="${ack}"/>
    But as ack is string, i want to convert it into int. and i have written like
    <c:set var="ackvalue" value="${ack}"/>
    and int ack = Integer.parseInt(ackvalue);
    But it is showing error like varialbe can not be resolved: ackvalue
    can anybody please help me regarding this?
    Waiting for your warm response.
    Thanks in advance

    Hi,
    Thanks for your immediate reply.
    I am able to display the ackvalue when i am using
    <c:set var="ackvalue" value="${ack}"/>
    and <c:out value="${ackvalue}"/>
    But my actual requirement is to use the ack value in
    switch statement. if i can assign the value to a
    String variable, then it si easy for me to proceed.
    Please help me in this regard.
    Thanks,You can use the c:choose, c:when and c:otherwise tags
    <c:choose>
        <c:when test = "${ackvalue == 1}">
               //do stuff
        </c:when>
        <c:when test = "${ackvalue == 2}">
               //do some other stuff
        </c:when>   
       <c:otherwise>
              //stuff
        </c:otherwise>
    </c:choose>ram.

  • How does a JSP page remember a user and prints in every page name of user?

    Hello, i'm working with JSPs and mysql.
    I have a Login page where sm inserts data in the two fields:
    name, username.
    i check if that user exists in the database.
    if the user exists i would like in every jsp page to mention/appear his name.
    As happens in this site [forums.sun.com] where it says "Welcome user" and
    when you click on it you are transferred in your profile page.
    How am i supposed to do that using JSP?
    May sm post me an example or a link to study that particular matter?
    thanks, in advance!

    This is covered by every decent JSP book/tutorial.
    Recommended book: [Head First Servlets & JSP|http://www.amazon.com/Head-First-Servlets-JSP-Certified/dp/0596005407] of Bates and Sierra.
    Recommended tutorial: [http://java.sun.com/javaee/5/docs/tutorial/doc/] (just Part II is enough).
    Alternative tutorials: [http://courses.coreservlets.com/Course-Materials/csajsp2.html].

  • Including JavaScript into a JSP Page

    Hi,
    How do i include a piece of JavaScript <SCRIPT> stuff into a JSP Page?
    Right now i have an HTML Page that includes some <SCRIPT> code. Now many other HTML files use this piece of <SCRIPT> stuff. How do i remove it , put it in one place(I mean where to put it and what would that file be called) and in a JSP page call this <SCRIPT> code?
    Please help . i'm pretty new to JSP and JavaScript.
    Thanks in Advance.
    Phani
    /**** CODE
    <SCRIPT language=javascript>
    <!--
         if ((document.images) && ("Netscape" != navigator.appName) && (navigator.appVersion.indexOf('Mac') == -1))
         document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/1.css\">");
         else if ((document.images) && ("Netscape" != navigator.appName) && (navigator.appVersion.indexOf('Mac') > 1))
         document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/5.css\">");
         else if (("Netscape" == navigator.appName) && (parseInt(navigator.appVersion) == 4) && (navigator.appVersion.indexOf('Mac') == -1))
         document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/2.css\">");
         else if (("Netscape" == navigator.appName) && (parseInt(navigator.appVersion) == 4) && (navigator.appVersion.indexOf('Mac') > 1))
         document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/3.css\">");
         else if (("Netscape" != navigator.appName) && ("Microsoft Internet Explorer" != navigator.appName))
         document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/6.css\">");
    // -->
    </SCRIPT>

    Ok, first let us make clear the demarcation between java and javascript.
    Java runs on the server, and produces an HTML page which is sent to the client.
    That HTML page may contain javascript code on it to run on the client side.
    As the page is loaded, javascript may be compiled/run on the client.
    So you have some javascript code which you want to include on every page as template text?
    I would do this in JSP by using the include directive to add that bit of template text to every single JSP page.
    ie
    header.jspf
    <SCRIPT language=javascript>
    <!--
    if ((document.images) && ("Netscape" != navigator.appName) && (navigator.appVersion.indexOf('Mac') == -1))
    document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/1.css\">");
    else if ((document.images) && ("Netscape" != navigator.appName) && (navigator.appVersion.indexOf('Mac') > 1))
    document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/5.css\">");
    else if (("Netscape" == navigator.appName) && (parseInt(navigator.appVersion) == 4) && (navigator.appVersion.indexOf('Mac') == -1))
    document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/2.css\">");
    else if (("Netscape" == navigator.appName) && (parseInt(navigator.appVersion) == 4) && (navigator.appVersion.indexOf('Mac') > 1))
    document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/3.css\">");
    else if (("Netscape" != navigator.appName) && ("Microsoft Internet Explorer" != navigator.appName))
    document.write("<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"http://www.telia.se/tews/css/6.css\">");
    // -->
    </SCRIPT>
    <noscript>
      // stuff here for when the browser does not support javascript
    </noscript>And then in every jsp page
    <%@ page include="header.jspf" %>
    // rest of the JSP pageThe file "header.jspf" will be included with every jsp page served, and you only need to update that page in one place.
    If you have a JSP2.0 container you can define a jsp prelude which will be the same as having the <%@ page include="header.jspf" %> on every page.
    You do this in the web.xml file:
    Something like this:
      <jsp-property-group>
         <url-pattern>*.jsp</url-pattern>
         <include-prelude>/WEB-INF/jspf/prelude1.jspf</include-prelude>
      </jsp-property-group>The specified file will be loaded into every JSP page without the JSP page having to define the import itself.
    Hope this helps,
    evnafets

  • Capturing xml data returned from a url post in a jsp page

    Hi,
    We are writing a interface which will capture data returned from an other website. The data returned will be in XML form.
    http://www.ecrm.staging.twii.net/ecrmfeed/ecrmfeedservices.sps?eCRMFeedInputXml=<twii><ecrmfeedinput><data%20method="Login"><username>[email protected]</username><password>password</password></data></ecrmfeedinput></twii>
    When the above url is executed in a browser, it required NT authentication. The username and password is getcouponed. Once the username and password is fed, we can see the output in the form of a xml. We require the xml in a String variable in a jsp page. We need to know the steps on how to execute the url in a jsp page. We used the url object to do the same, but we get a error saying "java.net.UnknownHostException: www.ecrm.staging.twii.net".
    Can anyone help?
    Regards,
    Gopinath.

    Hi,
    I would like to know if I can use the java.net package to get anything out of a website which requires authentication. In this case NT authentication.
    Thanks in advance,
    Gopinath.

  • Problem with bean in a JSP page

    I have developped a JSP pages in wich I call a bean with
    <jsp:useBean id="db" scope="request" class="SQLBean.class" />
    The bean SQLBean.class is in the same directory as the JSP page.
    When I execute the JSP, I have the following error:
    SQLBean.class db = (SQLBean.class) JSPRuntime.instantiateBean("db", "SQLBean.class", "request", __db_was_created, pageContext, request, session, application);
    Syntax error: [expected after this token
    Syntax error: "]" inserted to complete ArrayAccess
    Syntax error: "AssignmentOperator AssignmentExpression" inserted to complete StatementExpression
    I use JRUN with IIS
    Thanks for the help

    I am assuming your class name is SQLBean.
    use:
    <jsp:useBean id="db" scope="request" class="SQLBean" />
    instead of:
    <jsp:useBean id="db" scope="request" class="SQLBean.class" />

  • How to use value returned from a bean in jsp page

    Hi All,
    I have a string array value being returned from a javabean to a jsp page. I want to be able to assign the value to a variable in the jsp page.
    How do I do this?
    Many thanks :)

    thanks for your response.
    I actually used something like this:
    <% String loggedin = log.User(user,password);
    out.println(loggedin);
    %>i am not yet as much of an expert on beans as i would like to be, but i thought it is not really a bean if it has a getter method which takes arguments?

  • Problem with printing debug statements in JSP page

    Hi all,
    I am debugging an application with System.out.println() statements - my debug statements in the *.java class printed just fine, but when I do the same thing in my JSP page, nothing gets printed out.
    I am already wrapping my System.out.println() statements inside the <% %> tag.
    Does anyone know what may be causing this? Any help will be greatly appreciated!
    - JL

    Where do you expect to see the output?
    System.out.println will write to the console/logs
    They WON'T write to the JSP page.
    If you check your error/output logs you will probably find the statements there.
    If you want to print things into the JSP page, just use out.println.
    "out" is an implicit reference to the current JSPWriter.
    Cheers,
    evnafets

  • How to pass applet variable to a jsp

    I just want to pass applet variable to a jsp page to access database. But how .
    Help me please.

    you can use showDocument() to show a page which could have values in the query string.
    you can use URLConnection to do GET or POST requests to a page.

Maybe you are looking for

  • Family sharing not working for music on Apple TV

    We have Family Sharing set up and while we can see each other's purchases in Movies and TV shows we can not see our shared music. Does anyone know how to view each other's music purchases on the Apple TV? Thanks

  • Xcode 4 crashes after I open my project

    After I waste a day searching why Xcode consumes 100% of CPU (it seems a problem with SVN repository) this application begin to crash systematically when I open my project. I clean the build, I deleted DerivedData contents and Xcode cache... no luck.

  • File sizes growing by magic?!?!?!?!?!?!

    I create DVD's for a wedding business. My file sizes are "growing" and i don't know why. This is the first time I've noticed it. I've designed and burned over 100 wedding DVD's using this method. I use a template in DVD studio pro and I drag and drop

  • TEM: when attendee is in Waiting list status, workflow should triger notifi

    Dear Professionals, In TEM, we have a scenario where, if an employee/attendee has booked for an event and in waiting list status, when his waiting list status changes to normal booking (bacause of the cancellations in normal booked) a notification ma

  • MDB listening to a Oracle AQ, holding ADT messages...

    Hi ! I'm trying to set a MDB to listen for incoming messages on an Oracle AQ. When I created the queue, I specified that the queue should contain payload of type ADT. I'm using Bea Weblogic server 8.1 sp4 and Oracle 10.2.0. The creation of the connec