javascript inside java/jsp code

i've written the javascript code inside


<% try { String id=request.getParameter("id"); %> <script type="text/javascript" > alert("TRY"+ <%=id %>); document.getElementById("mytext").readonly="readonly"; </script> <% } catch (Exception e) { %> <script type="text/javascript" > alert("CATCH"+<%=e%>); </script> <% } %>

So, whenever value will be passed into the id it'll show the value of id into the alertbox and also it'll make the textbox whose id is <strong>mytext</strong> will become disable.

So, when i'm executing the <strong>myjsp.jsp</strong> page without passing the parameter it shows the output as <strong>TRYnull</strong>, but when i'm passing the value into id then it's not showing anything, not even alertbox. Any idea !!

My jsp page

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> <% try { String id=request.getParameter("id"); %> <script type="text/javascript" > alert("TRY"+ "<%=id %>"); alert(document.getElementById("mytext")); </script> <% } catch (Exception e) { %> <script type="text/javascript" > alert("CATCH"+<%=e%>); </script> <% } %> <input id="mytext" name="mytext" /> </body> </html>


This is happening because when you pass no request parameter of id it will, as you've mentioned, be null. In this case the following JavaScript will be produced:

alert("TRY" + null);

This is valid JavaScript so this is why you see TRYnull.

However, if you pass in an id request parameter the following JavaScript will be generated:

alert("TRY" + foo);

Although this is syntactically valid JavaScript it is almost certain to produce an error (and therefore not show the alert) because here foo is a variable which is highly likely to be undefined.

If you have a look in the JavaScript console you will probably see something like:

Uncaught ReferenceError: foo is not defined

What you need to do it put quotes around your scriptlet so that the value of id is treated as a String and not a variable, i.e.

alert("TRY <%= id %>");


  • JQuery: add class to element - cannot use this class for selector?
  • PhoneGap, iOS 6.1 shows weird alert on launch and hangs at load screen
  • c# How to detect and process a .URL file type on DragDrop event
  • Autohotkey script running program with command line arguments
  • In Akka, is ActorContext thread safe?
  • How to use the resource module to measure the running time of a function?
  • Disabling sound of embedded flash object with html
  • How to save dynamically created textboxes and their values
  • Smack 4.1.0 android Roster not displaying
  • extjs4 catching the scroll event on panel
  • C++ Single function pointer for all template instances
  • How to copy styled text in JTextPane
  • Why does it draw lines in the wrong place?
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • How to add a focus style to an editable ComboBox in WPF
  • How to assign byte[] as a pointer in C#
  • Code in Job's Script Block after Start-Process Does not Execute
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • Do I need to reset a Perl hash index?
  • Converting a WriteableBitmap image ToArray in UWP
  • Reading JSON from a file using C++ REST SDK (Casablanca)
  • Highlight one bar in a series in highcharts?
  • DomPDF {PAGE_NUM} not on first page
  • Why value captured by reference in lambda is broken? [duplicate]
  • Email format validation in mvc3 view
  • jQuery show() function is not executed in Safari if submit handler returns true
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Sending data from AppleScript to FileMaker records
  • WinForms: two way TextBox problem
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • How to disable jQuery.jplayer autoplay?
  • A cron job substitute?
  • Android Studio and gradle
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Load html files in TinyMce
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can i traverse a binary tree from right to left in java?