本文主要是實(shí)現(xiàn)了jsp調(diào)用javascript代碼砰识。
內(nèi)容:使用jsp實(shí)現(xiàn)了一個簡單的登錄界面,點(diǎn)擊登錄調(diào)用javascript代碼佣渴,對用戶的輸入有效性進(jìn)行驗(yàn)證-判斷輸入是否為空辫狼;
代碼:
(1)login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<script type="text/javascript">
<!--
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{
alert(alerttxt);
return false
}
else {
return true
}
}
}
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(uname,"賬號不能為空")==false)
{
uname.focus();
return false;
}
if (validate_required(upass,"密碼不能為空")==false)
{
upass.focus();
return false;
}
}
}
-->
</script>
</head>
<body>
<form action="loginManager.jsp" onsubmit="return validate_form(this)" method="post">
賬 號: <input type="text" name="uname" size="30">
<br/>
密 碼: <input type="text" name="upass" size="30">
<br/>
<input type="submit" value="登 錄">
</form>
</body>
</html>
(2)loginManager.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登錄處理</title>
</head>
<body>
<%
String uname=request.getParameter("uname");
String upass=request.getParameter("upass");
if(uname==null || upass==null){
System.out.println("參數(shù)為空");
}else{
if(uname.equals("wzt") && upass.equals("wzt")){
response.sendRedirect("main.jsp");//路徑001
}else{
response.sendRedirect("login.jsp");
}
}
%>
</body>
</html>
(3)main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>歡迎來到主界面</title>
</head>
<body>
<%
out.println("Hello, welcome to Main jsp.");
%>
</body>
</html>