例1:jsp页面间通过request/session传值
代码
session1.jsp(从表单输入usernmae,将该参数传递到session2.jsp)
<meta http-equiv="Content-Type"content="text/html;charset=gbk"/>
<html>
<body>
<form method="post" action="session2.jsp">
name:
<input type="text" name="username">
<input type="submit" value="submit"">
</form>
</body>
</html>
session2.jsp(接收session1.jsp传递过来的参数username,存到session中,然后在将参数place通过表单提交到session3.jsp中)
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<body>
<%
String username = request.getParameter("username");
session.setAttribute("username",username); %>
hello,
<%=username%>
!the place you want to go is
<p>
<form method="post" action="session3.jsp">
<input type=text name="placei">
<p>
<input type=submit value="submit">
</form>
</body>
</html>
session3.jsp(接收session2.jsp传递过来的参数place,并从session中读取参数username),输出
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<body>
<%
String username = (String) session.getAttribute("username");
String place = request.getParameter("place"); %>
<%= username %>
<br>
the place you want to go is
<br>
<%=place %></body>
</html>
例2:使用JavaBean传值
NoticeBean.java
package com.sunday.beans;
public class NoticeBean {
int id;
String title;
String content;
public NoticeBean(){}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}}
beantest.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>Notice Bean Demo</title>
</head>
<body>
<!--第一种传值方式:调用bean的set/get函数传值-->
<jsp:useBean id="noticebean" scope="application" class="com.sunday.beans.NoticeBean"/>
<%
noticebean.setTitle("今天星期几");
noticebean.setContent("星期一");%>
the first question:
<%=noticebean.getTitle()%>
<p>
answer:
<%=noticebean.getContent() %>
<%
noticebean.setTitle("明天呢");
noticebean.setContent("星期二");%>
<p>
the second question:
<!--第二种传值方式 使用 jsp:getProperty 读取值-->
<jsp:getProperty name="noticebean" property="title" />
<jsp:getProperty name="noticebean" property="content"/>
</body>
</html>
附:Jsp:JavaBean笔记
(转自http://hi.baidu.com/hiei213/blog/item/d0740587bf1edc2dc65cc3ba.html)
编写javabean原则:
1、所有javabean放在一个包中
2、javabean必须声明成public class,文件名与类名一致
3、所有属性必须封装
4、设置和取得可以通过set ,get
5、使用jsp标签语法去取得javabean的必须有一个无参构造方法
命名规则:
1、包命名全部小写
2、类名首字母大写
3、属性名称:第一个单词首字母小写,之后每个单词首字母大写: sayHello
4、方法命名:与属性名相同:public void sayHello();
5、常量命名:全部大写 final String DBDRIVER="mlaafa";
打包编译 javac -d . SimpleBean.java
修改javabean后,需两种方法启用:
1、重启tomcat
2、打开tomcat/conf/server.xml
<Context reload="true">
如果设置后,会影响服务器性能。在开发时为方便加入此参数。
调用javabean,有两种:
1、使用import方式调用
<%@page contentType="text/html;charset=gb2312"%>
<%@page import="cn.midn.txh.*" %>
class是一个classpath路径,import直接打路径
2、使用jsp标签指令调用
<jsp:useBean id="MyFirstBean" scope="page" class="包.TestBean" />
如果使用此标签,javabean必须有一个无参构造方法,此为明确规定。
使用此方法好处:必须结合用户参数提交才能发现使用javabean的好处。
使用下列代码,可以替代一切的set方法通用
<jsp:setProperty name="sb" property="*"/>
name:已经声明过的javabean对象的id
property:表示需设置的属性,如果为"*",则表示自动匹配。
原则
使用javabean自动设置的同时,一定要让参数名称与Bean中的属性名称一致,这样通过java的命名规范,可以达到统一。
javabean的属性的设置方法,有四种:
1、自动匹配
<jsp:setProperty name="mybean" property="*"/>
使用javabean自动设置的同时,一定要让参数名称与Bean中的属性名称一致,这样通过java的命名规范,可以达到统一。
2、指定匹配
<jsp:setProperty name="mybean" property="myProperty"/>
制定某个指定属性设定
3、指定传递值和参数的关系
<jsp:setProperty name="mybean" property="myProperty" param="ParamName"/>
把ParamName的值给myProperty
4、指定值
<jsp:setProperty name="mybean" property="myProperty" value="ParamName"/>
把myProperty参数的值定为"ParamName"
如果输入的值是变量,需用表达式输出。
<jsp:setProperty name="sb" property="password" value="<%=password%>"/>
javabean的属性的取得输出方法
<jsp:getProperty name="myBean" property="myProperty"/> 值会自动转换类型,将数字的字符串,变为整型
jsp的属性的保存范围,四种作用域范围
<jsp:useBean id="MyFirstBean" scope="page" class="包.TestBean" />
scope:
page 当前页面有效,跳转到其他页面,使用于,jsp页面操作资源时使用。
request 属性保存在一次服务器跳转中
前提:使用<jsp:forward跳转才行
session 属性保存在一次会话之中,可以使用任意方式连接到其他页面,因此对象只实例化一次
application 属性共有,此对象在整个服务器上,只实例化一次,尽量少用,占服务器资源