jsp的一个重要特性就是可以用javabean实现功能的扩展。将大部分功能放在javabean中完成,以使jsp页面程序更干净简洁、利于维护。javabean可以很方便的用来捕获页面表单的输入并完成各种业务逻辑的处理。如下就是一个hello示例:
testa.jsp页面:
<%@ page contenttype="text/html;charset=gbk" %>
<html>
<head>
<title>示例</title>
</head>
<body scroll=no>
<form name="frma" method="post" action="testb.jsp" >
<p>
你的姓名:
<input type="text" size="15" name="yourname" value="" id=yourname>
<input type="button" align="center" name="subbtn" value="提交" onclick="f_check()" id=subbtn>
</p>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
<!--
function f_check(){
if(document.forms(0).yourname.value==""){
alert("请输入姓名");
}else{
document.forms(0).submit();
}
}
-->
</script>
testb.jsp页面
<%@ page contenttype="text/html;charset=gbk" %>
<html>
<head>
<title>示例</title>
</head>
<jsp:usebean id="tbean" scope="page" class="bean.testbean" >
<jsp:setproperty name="tbean" property="*" />
</jsp:usebean>
<body scroll=no>
<form name="frmb" method="post" action="" >
<p>
<%=tbean.hello()%>
</p>
</form>
</body>
</html>
testbean.java 代码:
package bean;
public class testbean{
private string yourname = "";
public void setyourname(string yourname){
this.yourname = convertgbk(yourname);
}
public string hello(){
string strhello = "hello:"+yourname;
return strhello;
}
//汉字转换方法
public string convertgbk(string str){
string strreturn="";
try{
strreturn=new string(str.getbytes("iso-8859-1"),"gbk");
}catch(exception ex){
system.out.println("testbean.convertgbk():ex="+ex.tostring());
}
finally{
return strreturn;
}
}
}
public class testvaluebean{
private string yourname = "";
public void setyourname(string yourname){
this.yourname = convertgbk(yourname);
}
//汉字转换方法
public string convertgbk(string str){
string strreturn="";
try{
strreturn=new string(str.getbytes("iso-8859-1"),"gbk");
}catch(exception ex){
system.out.println("testbean.convertgbk():ex="+ex.tostring());
}
finally{
return strreturn;
}
}
}
工具bean
package bean;
public class testutilitybean{
public string hello(testvaluebean tvbean){
string strhello = "hello:"+tvbean.getname();
return strhello;
}
public string hello(string yourname){
string strhello = "hello:"+yourname;
return strhello;
}
}Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器