如何在jsp中链接外部js文件 [英] how to link external js file in jsp
本文介绍了如何在jsp中链接外部js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SO上遇到其他类似的问题,但无法解决我的问题。我在脚本
标签内写了 src
路径。
Went through other similar question on SO, but unable to fix my issue. I have written src
path inside script
tag.
以下是该程序:
function validate()
{
var fname = document.myForm.fname.value;
var lname = document.myForm.lname.value;
if(fname=="" && lname=="")
{
alert("First and last name cannot be emtpy");
return false;
}
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="/js/validation.js"></script>
<title>Registration Form</title>
</head>
<body>
<form name="myForm" action="RegistrationServlet.do" method="POST" onsubmit="return validate()">
First name:<input type="text" name="fname" /> <br/>
Last name:<input type="text" name="lname" /><br/>
Gender: Male<input type="radio" name="male" /> Female<input type="radio" name="male" /><br/>
Mobile:<input type="tel" name="mobile" /><br/>
Email:<input type="email" name="email" /><br/>
Address:<textarea name="tarea"></textarea><br/>
Street:<input type="text" name="street" /><br/>
Area:<input type="text" name="area" /><br/>
City:<select>
<option value="hyderabad">hyderabad</option>
<option value="secunderabad">secunderabad</option>
<option value="delhi">delhi</option>
<option value="mumbai">mumbai</option>
</select>
State: <input type="text" name="state" /><br/>
Country: <input type="text" name="country" /><br/>
Pin: <input type="text" name="pin" /><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
我的js文件位于名为的文件夹中webapp目录中的js
My js file is in the folder called js inside the webapp directory
推荐答案
以下行解决了我的问题:
The below line fixed my problem:
<script src="<%request.getContextPath()%>/js/validation.js"></script>
这篇关于如何在jsp中链接外部js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文