admin管理员组

文章数量:1291007

i've written the javascript code inside

myjsp.jsp

<%
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 mytext will bee disable.

So, when i'm executing the myjsp.jsp page without passing the parameter it shows the output as TRYnull, 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>

i've written the javascript code inside

myjsp.jsp

<%
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 mytext will bee disable.

So, when i'm executing the myjsp.jsp page without passing the parameter it shows the output as TRYnull, 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>
Share Improve this question edited Mar 8, 2012 at 13:05 asked Mar 8, 2012 at 12:28 user918477user918477 2
  • JSP and JavaScript does not run in sync. JSP produces HTML/JavaScript. Open page in browser, rightclick it and View Source to see yourself. – BalusC Commented Mar 8, 2012 at 13:15
  • FYI if the exception is for example unparsable date and has doub quotes it will give error in browser console ! – shareef Commented Dec 28, 2015 at 22:01
Add a ment  | 

1 Answer 1

Reset to default 3

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 %>");

本文标签: javascript inside javajsp codeStack Overflow