页面之间传值的方法

来源:互联网 发布:3d设计软件 编辑:程序博客网 时间:2024/06/08 19:04

1:使用URL

http://localhost:8080/javafaq/hello.jsp?name=tom&age=11

获取URL信息的方法:String n = request.getParameter("name");

                                        String n  = request.getParameter("age");

 

2:使用request

当多个页面之间使用forward方式跳转时;可以使用requeset方法
//往request对象中设置信息

request.setAttribute("name","tom");

request.setAttribute("age","20");

//获取request对象中的信息

String n = request.getAttribute(“name”);

Integer n = (Integer)request.getAttribute("age");

注:存入request中的对象都会自动转换成object类型,所以用getAttribute获取对象时需要强制转换

 

3:使用session

使用session可以在多个页面之间传值;

//往session对象中设置信息

session.setAttribute("name","tom");

session.setAttribute("age","20");

//获取session对象中的信息

String n = session.getAttribute(“name”);

Integer n = (Integer)session.getAttribute("age");

注:存入session中的对象都会自动转换成object类型,所以用getAttribute获取对象时需要强制转换

 

4:使用application

如果在多个页面,需要多个用户共享传值,则使用application;使用application时,需要考虑并发的问题,可以使用synchronized关键字

synchronized(application){

    //使用application对象的代码

总结:如果相邻页面传值,可以考虑使用URL和request;如果是单个用户跨多个页面传值,可以考试使用session;如果是多个用户跨多个页面,可以使用application

 

原创粉丝点击