客户端请求新页面的14种方法

来源:互联网 发布:mac允许安装未知来源 编辑:程序博客网 时间:2024/06/05 03:37

为了便于演示:先列出包结构:


现在以page1 跳转到page2 ,为例。

先看下page2.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>Insert title here</title>
</head>
<body>
page2    <br>
<% out.print(request.getParameter("uid"));
 %>
</body>
</html>

---------------------------------------------------------

接下来,最重要的看下page1的内容(包含4大类方法。14种具体方法):

<%@ 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>Insert title here</title>
<script type="text/javascript">
function fun1(){
   window.location="page2.jsp";
}
function fun2(){
   window.location="page2.jsp?uid=admin";
}
function fun3(){
   window.open("page2.jsp?uid=admin");
}
function fun(){
   var uid=document.getElementById("uid").value;
   if(uid==""){
    alert("请输入用户名");
      return false;
   }else{
      return true;
   }

}

</script>
</head>
<body>

<!--第一大类: 使用超链接-->
<!-- 连接到page2 -->
<a href="page2.jsp">连接到page2</a><br>
<!-- 连接到page2,弹出新窗口 -->
<a href="page2.jsp " target="_blank">连接到page2</a><br>
<!-- 使用相对路径链接到page2 -->    
<a href="./page2.jsp">连接到page2</a><br><!-- .表示当前所在文件夹 -->
<a href="../demo/page2.jsp">连接到page2</a><br><!-- 当前文件夹的上级文件夹 -->
<!-- 使用绝对路径链接到page2 -->
<a href="http://localhost:8080/fuxi/demo/page2.jsp">连接到page2</a><br>
<a href="<%=request.getContextPath()%>/demo/page2.jsp">连接到page2</a><br>
<!-- 连接到page2,同时传递参数 -->
<a href="page2.jsp?uid=admin">连接到page2</a><br>



<!--第二大类: 使用超链接+js-->
<!-- 使用js连接到page2 -->
<a href='javascript:window.location="page2.jsp";'>连接到page2</a><br>
<a href='javascript:fun1();'>连接到page2</a><br>
<!-- 使用js连接到page2,并传参 -->
<a href='javascript:fun2();'>连接到page2</a><br>
<!-- 使用js连接到page2,弹出新窗口,并传参 -->    
<a href='javascript:fun3();'>连接到page2</a><br>     


<!---第三大类:使用submit按钮提交表单来请求新页面->
<!-- submit提交表单 -->
<form action="page2.jsp" method="post">
  <input type="text"  name="uid">
  <input type="submit" value="提交表单">
</form>
<!-- 验证后提交表单 -->
<form action="page2.jsp" method="post">
<input type="text" name="uid" id="uid"/>
<input type="submit" value="提交表单" onclick="return fun();">
</form>
<!-- 使用url方式提交数据 -->
<form action="page2.jsp?uid=admin" method="post">
<input type="submit" value="提交表单" >
</form>
<!-- 使用hidden方式提交数据 -->
<form action="page2.jsp" method="post">
  <input type="hidden"  name="uid"  id="uid2" >
  <input type="submit" value="提交表单">
   <script type="text/javascript">
           var u=document.getElementById("uid2").value="admin2";           
   </script>
</form>


<!--第四大类:使用html元素和javascript来提交表单-->
<!-- 使用button+js来提交表单 -->
<form id="form1" action="page2.jsp" method="post">
  <input type="text"  name="uid">
  <input type="button" value="提交表单" onclick='document.getElementById("form1").submit();'>
</form>
<!-- <a>+js提交表单 -->
<form id="form2" action="page2.jsp" method="post">
  <input type="text"  name="uid">  
   <a href='javascript:document.getElementById("form2").submit();'>提交表单</a>
</form>
</body>
</html>


0 0