Embed javascript in JSP

hi
i want to store the query string of an URL in a flat file. but i want to append the name of the person who's passing the query string by prompting him to enter his/her name. i have a code something like this
FileWriter outputFile = new FileWriter("Libset1.txt", true);
BufferedWriter outputBuffer= new BufferedWriter(outputFile);
final PrintWriter outputWriter = new PrintWriter(outputBuffer);
//writes all data to the output file
for (int i=0; i<Selected_set1.length; i++)
outputWriter.println("some javascript to prompt the user goes here..." + Selected_set1);
outputWriter.close(); //closes output file
i want to embed some javascript in place "some javascript to prompt the user goes here..." prompting the user to enter his name..
is it possible to do that. if yes, how.?
sorry i'm just a beginner, it might look silly for you...please help...
thanks in advance..

First of all, you need to understand how Javascript and JSP work together....they don't. Javascript is a client side scripting language, which means that it executes and runs in the client browser after the page has been loaded. JSP is purely server side, it only runs while the request is being serviced, or while it's generating the HTML to send back to the client browser.
To do what you're asking for should be pretty easy using javascript. You should be able to use the Javascript onClick() event for the link the user is clicking on to execute a Javascript method that prompts the user for their name.
You might try something like this...
<script language="JavaScript">
function promptUser(newPage)
  var username = prompt("Please enter your username");
  if(username)
    document.location = newPage + "&username=" + username;
}</script>
Click Here Please!
This will result in a query string that looks something like mypage.jsp?id=23&view=whatever&username=john
Make sense?

Similar Messages

  • JavaScript within JSP

    Hi all !!
    I'm stuck up.
    Is there any way by which i can actually embed JavaScript within a JSP file that contains no JSP code. Actually it is file with .jsp extention but from within it holds only java script inside <script> tags.
    The prerequisite is there should be no performance implications... as this would be just a plain js file anyways.
    I need this coz i want to pick-up messages for alert from Resource bundle. For this i'am using <bean-el:message ...> tag. but using this in a plane .js file is not possible.
    How should i go about it.
    or
    Is there any better way to do this.

    You could put how much ever js that you need inside a jsp file. Theres no restrictions and given that you have to pick up messages using a tag, there's no other way out too.
    Other than the compilation time (which can be overcome by precompiling jsps), there's not much of a difference in the load time for a static html or html streamed from a jsp process. Ofcourse if you use the 'src' tag to load you js, each 'src' results in a round trip. However that's true of even static html pages.
    cheers,
    ram.

  • Associating two events with submit button using  javascript in jsp

    Hi
    How can i Associate two events with submit button using javascript in jsp. Firstly it should insert the data to database and secondly it should close the same pop-up window

    Have something like :
    <input type="submit" name="submitbtn" value="Click me" onClick="function1(); function2(); " />
    You just call both functions sequentially, it's that simple. Although using javascript to work with a database, that seems a bit tricky.

  • Help me!! How to use JavaScript with JSP ??

    I am using JDeveloper and I created a screen in JSP which uses a bean for database connectivity and retriving info onto the page.
    The page has a ListBox where list items are populated from the database.My requirement is
    whenever the list is changed the page shuold be refreshed with the selected item info.
    I tried to use 'JavaScript' for triggering the event with 'onChange' event of the ListBox.But the event is not getting invoked. I think JavaScript is not working with JSP.
    Please help me with how to Use javaScript with JSP or any other alternative where I can meet my requirement.
    I have one more question...I have gone through the JSP samples in OTN and I am trying do download the sample 'Travel servlet' which show list of countries...etc
    I have also gone through the 'readme' but I don't know how to extract .jar file.
    I would be great if you could help me in this.
    Thanks!!
    Geeta
    null

    We have a similar need. We have used Cold Fusion to display data from Our Oracle Database. We have a simple SElect Box in HTML populated with the oracle data. When someone selects say the State of Pennsylvania. then we have an On change event that runs a Javascript to go get all the cities in Pennsylvania.
    Proble we are having is that inorder for the Javascript to work , we currently have to send all the valid data.
    Do you know of any way to dynamically query the the Oracle database in Javascript

  • JSON from HTML embed Javascript to AS

    Hi,
    I know how to get JSON working if loading from external file
    by using LoadVars.
    But i don't know how to get JSON from the HTML embed
    Javascript and get to work in flash?
    I have tried different ways and search at google about
    related cases but nothing found yet.
    Can somebody help me?
    that's the code that i have used:

    Thanks for the suggestion.
    Well, as near as I can tell the "do javascript" will in no way, shape or form pass info back out to Applescript from Bridge, and I need to grab the long user name from system info there.
    To be honest, I was just trying to do a nice elegant dialog box with Bridge because Illustrator CS2 didn't know Script UI and I thought since everyone has CS3 Bridge on their machines already... But I've settled for 3 Applescript dialogs instead, so the fight is over.
    1.5 days tinkering with JS. 1.5 hours recoding in AS. Whee...

  • Embed Javascript into SSRS Report

    Hi
    There are similiar question on this forum, but they don't really anwser my question, in a way I understand.
    I'm new to SSRS and have a requirement to add a 'badge' to a report I am creating, this is dynamic for each row.
    Below is the code that works as a static page. What I am after is to be able to pass the 'data-doi' for each row and have the 'badge' displayed on the report for each row.
    Can this be done?
    Thanks
    <html>
    <head>
       <script type="text/javascript" src="URL is below"></script>
       </head>
    <body>
    <div data-badge-popover="top" data-badge-type="4" data-doi="110.1371/journal.pone.0003902" data-hide-no-mentions="true" class="altmetric-embed"></div>
    </body>
    </html>
    JS URL
    replace -
    https-d1bxh8uas1mnw7-cloudfront-net/assets/embed.js

    Hi CuriousCraig,
    If I understand correctly, you want to embed the javascript with the code which posted before into report. As per my understanding, we can embed JavaScript in reports using “Response.Write” in custom code, and then show a messagebox using JavaScript. For
    more details, please see the following steps:
    Add a reference that refer to “System.Web”. We can do this by navigating to Report -> Report Properties -> References.
    Add the following custom code to your report through Report -> Report Properties -> Code:
    Function WriteHTML (ByVal Msg AS String) AS String
    System.Web.HttpContext.Current.Response.Write(Msg)
    Return “”
    End Function
    In the report, use the custom function for the image column:
    =Code.WriteHTML("<script>..[your code]..</script>”)
    For more information about how to add code to a report (SSRS), please see:
    http://msdn.microsoft.com/en-us/library/ms156028.aspx
    Another similar thread:
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/bb9a913c-4082-4b5d-8c9f-7c1b73f4721c/audio-alerts-on-reports?forum=sqlreportingservices
    Hope this helps.
    Thanks,
    Katherine Xiong
    Katherine Xiong
    TechNet Community Support

  • Parameters b/w javascript and JSp

    how i can pass values between javascript and jsp .
    ( java script varaible in the JSP part & jsp object in the javascript code )

    Since Java script is on the client side , to get the value of a java script variable into jsp, u have to set some form field variable and submit it to the server again. So, it can be something like:
    <script>
    function submit()
         var scriptVar = 12;
         document.form1.formVariable.value=scriptVar;
         document.form1.submit();
    </script>
    <%
    System.out.println("Script variable value = " +request.getParameter("formVariable"));
    %>
    <form name="form1" method="post">
    <input type="hidden" name="formVariable" value=""/>
    <input type="button" name="bSubmit" value="Submit" onclick="javascript:submit()" />
    </form>
    The first time this jsp is called, the value of formVariable will be null.
    The next time, when u click on submit button, the value will be 12.
    I have not tested this. There might be some syntax errors. Just try it out. Also, make sure that u don't end up submitting the form in an infinite loop by using the submit() generally in the script. Hope this helps.

  • Mix JavaScript and JSP

    Hi.
    I have jsp and javascript mixed when I click a check call a function javascript for example:
    //JavaScript
    function Hola(numerop) {
              idRol = Number(numerop)
              <%nombreRol = roles.get(numerop)%> }
    //Form
    <input type="checkbox" name="<%=roles.get(sum)%>" value="<%=roles.get(sum)%>" onClick="Hola('<%=roles.get(sum)%>')">
    In the function numerop is a number but in the jsp code numerop is undefined.
    How I can mixed javaScript and jsp??
    Please I need help you.

    Remember that the JSP runs on the server and generates the HTML, including Javascript. The Javascript runs on the browser after the JSP is finished. So you cannot do whatever it is you expected that to do.

  • How to genarate javascript from jsp?

    how to genarate javascript from jsp?
    Edited by: coolsayan.2009 on May 12, 2009 6:21 AM

    coolsayan.2009 wrote:
    [got an example|http://www.ibm.com/developerworks/web/library/wa-aj-simplejava1/index.html] but can you say all this specifically because i am not familier with AJAX.and i want to generate a javascript random number and by jsp i want to send it to user email.once user got the number it will be checked wheather he got it or not in next page say check.jsp if it this entry is right he will be grant to access the his account....
    What does AJAX have to do with the rest of this question? And why do you want to generate a number in JavaScript at all? If you are trying to confirm a working email address, which is what I think you are trying to do then leave that all on the server side. Generate the number in the servlet, mail it, etc. Neither JavaScript nor AJAX are any part of that.

  • Can JavaScript access JSP variable?

    Could we do this? Or, how could we have JavaScript access JSP variable?
    <%
    String fooBar = request.getParameter(......
    %>
    <script language="JavaScript">
    <!--
    if (fooBar  == ...) {
    -->
    </script>

    To access the variable in Javascript you'll have to
    write it out from the JSP. You'll need to do something
    like:
    <%
    String fooBar = request.getParameter(......
    %>
    <script language="JavaScript">
    <!--
    var fooBar = '<%=encodeQuotes(fooBar)%>';
    if (fooBar  == ...) {
    -->
    </script>You'll need to either make sure fooBar doesn't contain
    any quotes or write a function to replace each ' with
    \'.Thanks!
    That works perfectly, without encodeQuote() method in my current case.
    Thanks again. You ar a saviour.

  • Problem with Javascript in JSP

    Hi Guys,
    I have this problem with my JSP page. I am using a javascript function
    which calls a function within a bean. The problem that I have is that I get an error when trying to call the following function :
    function refDataTypes_onchange()
    Vector empNames = DBQuery.getResult();
    I get a runtime error at the line Vector empNames = DBQuery.getResult;
    Any ideas would be much appreciated.
    Below is the whole code:
    Thanks in advance
    FRank
    <%@ page contentType="text/html;charset=WINDOWS-1252"%>
    <%@ page import="java.util.*, java.lang.*" %>
    <%-- Declare the java bean for the countries to be queried --%>
    <jsp:useBean id="DBQuery" class="DBQuery" scope="page" />
    <%-- Bean property searchCondition is not set to query countries --%>
    <jsp:setProperty name="DBQuery" property="searchCondition" />
    <%
    Vector empNames = DBQuery.getResult();
    %>
    <HTML>
    <BODY BGCOLOR="GREY">
    <SCRIPT LANGUAGE="JavaScript1.1">
    function refDataTypes_onchange()
    Vector empNames = DBQuery.getResult();
    </SCRIPT>
    <FORM NAME = form1>
    Please enter in the following details
    <p>
    Reference Type :::====
    <select name="refDataTypes" onchange="refDataTypes_onchange()">
    <option value ="1"></option>
    <% for(Enumeration enum = empNames.elements();enum.hasMoreElements();)
    String nextEmpName = (String)enum.nextElement();
    %>
    <option value =<%=nextEmpName%>><%=nextEmpName%></option>
    <%
    %>
    </select>
    <BR>
    <BR>
    Emp No
    <INPUT TYPE="text" NAME=txtAge >
    <BR>
    <BR>
    <INPUT TYPE="button" VALUE="Check Details" NAME=butCheckForm >
    </FORM>
    </BODY>
    </HTML>

    Thanks dimadoo
    I need my JSP to do the following :
    When a user selects a option from a select list. There is a textfield which I want to populate based on the option that thas been selected from the list.
    I tried the following :
    <% Vector empNames = DBQuery.getResult(); %>
    and it does not even go into the bean.
    Will this work if I refresh the page?

  • How to get the value of a variable defined in javascript in JSP

    how to get the value of a variable defined in javascript in/through JSP????

    In Javascript you can use the DOM to access the input element and set it's value before submitting the form. Then it's value will just be passed.

  • How to embed JavaScript in a DynPro View?

    Hello,
    I hava a standard DynPro application.
    I would like to add a link UI which by pressing it will activate a JavaScript function. Can someone please show me how to do it and ingeneral how to embed JavaScpript in a DynPro View?

    This (protocol javascript:) probably works in current releases but in future releases it will be disabled by default for security reasons.
    The set of allowed URL protocols will then be configurable in the J2EE server.
    Armin
    Message was edited by: Armin Reichert

  • Is there a way to embed JavaScript within JavaMails?

    I have a program that creates and sends e-mails. Is it possible to insert JavaScript inside the mail message so that when a user clicks on a link in the html e-mail a alert("Warning : blah balh "); may appear?
    I know it's possible to embed the JavaScript, but can't get it to work. Is that not possible with html e-mails?

    Yes. surely.
    What can be done is :
    1. define an ascii file with the necessary
    <script>
    <!-- javascript -->
    function funcName() {
    alert("Blahhha");
    </script>
    __YOUR-PAGE-CONTENTS-HERE__
    2.Read the above <script></script> block in a Str Buffer
    3.Also substitute the __YOUR-PAGE-CONTENTS-HERE__ with ur page contains along with onLoad = funcName();
    4.This will dynamically generate a page for u, which u need to send as a plain message or as an .html (attachment).
    I think u will easily do it.
    Best Lucks
    from:
    Dhiraj.

  • Javascript to JSP question...Can javascript function set session attributes

    hello,
    i have a web app that, on one of its pages, displays "tabbed pane" as an image map at the top (a la amazon.com). my problem is this: each "logical" page contains separate forms that all use the same javabean. in other words, imagine that the tabs represent an account maintenance web ui for an on-line record store. the first tab might be labeled "General," the second "Contact info," the third "Shipping Info." Each uses the same account bean and displays portions of its properties relevant to the tab at hand. what i want to do is allow a user to enter the account maintenance ui, update info on the first tab, click on tab two and have the request with the changes sent to a processing jsp. yet, since each "tab" is actually a separate URL to another page, how do i get the updated info on the first tabe without adding some sort of "SAVE" button on each tab. ive considered using javascript, but dont know how to get the request params out of the first tab whn i click on another tab. is it possible to include an "onClick" function in each URL that "grabs" the updated form fields off the preceeding tab? can a javacript function set session attributes in jsp?

    hello there,
    wow, you've created one big mammy-jammy tool.
    first, javascript cannot access, set values to the session, without having to post to another JSP. javascript is great for manipulating objects, layers, form values, etc.
    you have 2 issues [if i understand correctly]:
    1) you need to able to save user info for a specific tab without having to reloading the page.
    ---you can create a form for EACH of your tabs and POST all the information to a hidden IFRAME or LAYER for NN4. that hidden IFRAME / LAYER will load a JSP page which with all the parameters you posted to it. or you can build a FRAMESET and target that document["frame-name"].src with that same JSP.
    2) handling when the SAVE INFO action should happen: hence some javascript event handler: onMouseOver, onClick, etc
    ---i don't know the dynamics of your tabs, but if store which tab was clicked on last, then if the user clicks on some other tab, javascript can submit that FORM to a JSP [see condition above]
    you have an interesting tool. can i see?
    i hope i wasn't too confusing, but your problem is sooo interesting. =)
    -WJP

Maybe you are looking for

  • Changing Data Source for command objects

    Hi, My report (designed in CR 10) is running based on the Stored Procedure call using a command object; which is running successfully in the DEV environment. The report data source connectivity is a DSN. Now i want to switch it to QA environment with

  • We cannot load firefox due to settings changed by speedy pc company

    our computer had slowed a lot, and I found a company that promised to fix it. however, in cleaning up our computer they removed some setting that apparently is needed for firefox. we contacted them to get this reset, and they could not do it, for som

  • SAP PI- FTP Third party connection

    Hi Guys, We are in the process of connecting XI to Thrid part through FTP file adapter . I just want to confirm with you guys that what are all the information we need to ask to them . Is it FTP - IP address and Port number is enough or we need to as

  • Don't want gmail and phone contacts synced

    I have synced my s3 with my gmail account because I want to be able to check my email with my phone (like every other human). I don't, however, want my contacts synced because it puts many many contacts in my phone (everyone I have ever emailed). I j

  • I want to create this DVD in iDVD but...

    I created a project in iMovie and every time I try to share it by publishing it in Media Browser or iDVD during and after a few hrs of it's upload iMovie quits unexpectedly every time before it finishes. And I've been trying to create this DVD for mo