------------------------fast.jsp----------------------------
/*
fast.jsp 放于ROOT目录下
*/
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title> 我的F测试网页</title>
</head>
<body>
<p><b>我的F测试网页</b></p>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="Hello1" scope="session" class="Test.Hello" />
<%
String str = "Hello,欢迎学习JSP! 我的F测试网页 ";
Hello1.setHello(str);
out.println(Hello1.getHello() + "<br>");
%>
<jsp:useBean id="mybean" scope="session" class="fast.FastJson" />
<%
//ResultSet RS = mybean.executeQuery("SELECT * FROM work_log");
String s="";
s=mybean.getS();
out.println(s);
%>
</body>
</html>
-------------------------- Hello.java------------------
/*
javac Hello.java 生成Hello.class 放于 WEB-INF/classes/Test目录下
*/
//java bean
//file name: faq.java
package Test;
public class Hello {
String h="test";
public void setHello(String s)
{
h=s;
}
public String getHello()
{
return h;
}
public static void main (String[] args)
{
Hello ho= new Hello();
System.out.println(ho.h);
}
}
-------------------User.java-----------------
/*
javac User.java 产生 User.class 放于WEB-INF/classes/fast目录下
*/
package fast;
public class User {
public String name;
public int age;
public User() {}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return name + ": " + age;
}
}
----------------------------FastJson.java
/*
javac FastJson.java 产生FastJson.class 放于 WEB-INF/classes/fast目录下
fast目录下同时有com和META-INF目录 放fastjson等class
*/
package fast;
import com.alibaba.fastjson.JSON;
public class FastJson {
String s;
public static void main(String[] args) {
//User u = new User("Biao", 23);
//System.out.println(JSON.toJSONString(u));
//String js = "{\"name\":\"Bi\\\"ao\",\"age\":23}";
//u = JSON.parseObject(js, User.class);
//System.out.println(u);
FastJson fj= new FastJson();
System.out.println(fj.getS() );
}
public String getS()
{
User u = new User("liuyz2006", 39);
return JSON.toJSONString(u);
}
}
============================
注意:C:\Tomcat\webapps\ROOT\WEB-INF\classes目录下放com和META-INF目录,如此两目录放于classes\fast目录下是
出现“Exception in thread "main" java.lang.NoClassDefFoundError: FastJson/java”错误的
http://127.0.0.1:8080/fast.jsp
我的F测试网页
Hello,欢迎学习JSP! 我的F测试网页
{"age":39,"name":"liuyz2006"}