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
1 Answer
Reset to default 3This 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
版权声明:本文标题:javascript inside javajsp code - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741499749a2382002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论