日期:2014-05-16  浏览次数:20377 次

JSP+JavaBean简单示例

?StandardBean.java 源代码如下:

package std;? 
import java.io.*;? 
????? 
public class StandardBean implements Serializable {? 
??? private String user = null;? 
??? private String city = null;? 
????????? 
??? //the none method? 
??? public void StandardBean() {? 
??? }? 
??? public String getUser() {? 
??????? return user;? 
??? }? 
??? public void setUser(String user) {? 
??????? this.user = user;? 
??? }? 
??? public String getCity() {? 
??????? return city;? 
??? }? 
??? public void setCity(String city) {? 
??????? this.city = city;? 
??? }? 
}? 

?

StandardBean.jsp 源代码如下:

<%@ page contentType="text/html" pageEncoding="GBK"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
   "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
    <head>  
        <title>JSP Beans Page</title>  
        <jsp:useBean id="lin" class = "std.StandardBean"/>  
        <jsp:setProperty name="lin" property="user" value="林"/>  
        <jsp:setProperty name="lin" property="city" value="烟台"/>  
    </head>  
    <body>  
        <%  
            out.print(lin.getUser());  
            out.print(lin.getCity());  
        %>  
    </body>  
</html> 

?

?

部署环境为Apache Tomcat/6.0.32
具体目录为
webapps/bean/WEB-INF/classes/std下有StandardBean.class

webapps/bean/WEB-INF/src/std下有StandardBean.java?

webapps/bean/StandardBean.jsp
访问的地址为:http://localhost:8080/bean/StandardBean.jsp?

?

注意:当编码格式为UTF-8时无法显示汉字,但是当编码格式为GBK或者GB2312时都可以正常显示。

1 楼 huangyunbin 2011-05-07  
原文提到  webapps/bean/WEB-INF/classes/std下有StandardBean.class下有StandardBean.class
请问一下:这个class是自动生成的还是编译得到的?因为我们写的都是.java文件啊
2 楼 yteye 2011-05-11  
webapps/bean/WEB-INF/classes/std下的StandardBean.class是webapps/bean/WEB-INF/src/std下的StandardBean.java的编译文件,通过命令行javac StandardBean.java编译后得到StandardBean.class,然后在剪切到webapps/bean/WEB-INF/classes/std目录下即可。