页面之间数据传递,页面跳转,JSP,JS

来源:互联网 发布:淘宝海报尺寸1920 编辑:程序博客网 时间:2024/06/04 22:18
一、页面间传递数据
    1、使用URL重写
        (1)写数据:利用URL地址,在地址里面包含指定信息,可以包含jsp信息;读数据:利用JSP的request对象
<a  href="delete.jsp?id=<%=s.getVsid()%>">删除</a>
##########################跨页面
int id = new Integer(request.getParameter("id"));
new Dele_voteSubject().deleteByVis(id);
        (2)调用js函数,传递数据
<a href = "javascript:del(<%=s.getId()%>)"
###########################
function del(id){
    if(window.confim('确认删除?')){
            new Dele_voteSubject().deleteByVis(id);
     }
}
        (3)调用window.location.href
<A href="javascript:window.location.href('webpage.asp?a=1')"> 点击这里 </A>
#############
<A href="javascript:window.location.href=webpage.asp?a=1"> 点击这里 </A>
#############
function(){location.href='vote_list.jsp?currentpage=1;}
     
2、利用四大作用域:page,request,session,application
         (1)request,application写数据和读数据:
setAttribute(String key, Object obj);
###########
object getAttribute(String name);
         (2)pageContext写数据和读数据
//获取
getAttribute(String name)//在page内获取
getAttribute(String name,int scope)//在一定范围内获取1~4
//设置
setAttribute(String name,Object attribute,int scope)//在一定范围内有效1-4
setAttribute(String name,Object attribute)//在page范围内有效
        (3)session
 //设置session 
HttpSession session  = request.getSession();
session.setAttribute("name","哈哈哈哈");
//得到session
HttpSession session = request.getSession();
//HttpSession session = request.getSession(false);//只获取不创建
String str = (String)session.getAttribute("name");
3、form表单
        (1)两种方式设置
方式1:
<formaction="xx.jsp"id="myform"></form>
<inputtype="text"form="myform"name="username">
<inputtype="password"form="myform"name="password">
<inputtype="submit"form="myform">
方式2:
<form action="xx.jsp">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>

        (2)获取元素
在xx.jsp页面中获取
request.getParameter("username");//username指向表单的name属
4、AJAX。。。待补充。。。。
二、页面跳转

1、: <jsp:forward>
jsp:forward 是一种JSP动作,使用语法如下:           
<jsp:forward page="Relative URL">
在使用此动作时,主页不可以有任何的输出,它所起到的作用与SERVLET中使用的RequestDispatcher方法的作用是一致的.这种跳转是由服务器执行的,因此跳转的页面可以放在WEB-INF目录中,提高程序的安全性.
2、 response.sendRedirect()
用此方法做跳转其实是向浏览器发送一个特殊的HEADER,然后又浏览器来做转向,装到指定的页面.所以用此方法时,浏览器上的地址栏里可以明显看到地址的变化.这与方式1动作不同,它是由服务器来做转向的.因此,使用sendRedirect做转向时,转向的页面不能放在WEB-INF下.
3、 使用页面自动刷新
<meta http-equiv="Refresh" content="秒数";url="跳转的文件或者地址">
4、请求转发与重定向
jsp:forword 是转发请求,所以在转发过程中,请求作用域的参数在转发页面是有效的response.sendRedirect 和页面刷新实际上都是重定向,所以请求作用域的参数在转到下一页面时回失效.
请求转发:request.getRequestDispatcher("xx.jsp").forward(request,response);
重定向:response.sendRedirect("xx.jsp");
5、利用HTML和JS
    (1)利用JS事件,单独拿出来可以自行跳转
     <buttononclick="location.href='2.html'">地址2页面</button> 
     <!--indow不可以省略-->
     <buttononclick="window.open('2.html')">open2页面</button>
     <buttononclick="location.replace('2.html')">replace2界面</button>
     <buttononclick="location.reload()">重新加载</button>
     <!--javascript可以省略-->
     <buttononclick="javascrpt:history.forward()">前进</button>
     <!--javascript不可以省略-->
     <ahref="javascript:history.back()">后退</a>
     <ahref="javascript:history.forward()">前进</a>   
    (2)html页面跳转,需要点击
<a href="http://www.baidu.com">跳转</a>;
<a href="mailto:123456@qq.com">发邮件</a>
<a href="javascript:alert('触发js效果')">弹框</a>
<a href="javascript:void(0)">什么也不做</a>
<a href="">跳转当前页面</a>
<a href="#">跳转当前页面,占位符</a>
<a href="#id1">跳转到id1处</a>
<ahref="2.html">地址2界面</a>


//待补充。。。。。。。。。




原创粉丝点击