json+jsp+JQuery 的一个小例子

这几天学习json写的一个简单的小例子,把代码发一下上来,大家可以看看,有不足请随意指出,菜鸟受教

jar包:

commons-beanutils.jar是对bean操作的类,可以访问类的属性及GET和SET方法.但它是用java的反射机制实现的,如果是大数据量时,使用起来会慢一些.
commons-lang.jar 数据类型的工具包,commons项目中的lang包
ezmorph.jar 是用的是josn 必须的包。
json-lib.Jar

以下网上说不是必备但缺少了出错,下面例子是添加的:
commons-collections.jar Apache Commons包中的一个,包含了一些Apache开发的集合类,功能比java.util.*强大
Commons-logging.jar是输出LOG用的,我一般都会和LOG4J连起来用.单独的我没有用过.


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
      <title>login</title>
  <script src="jquery-1.8.2.js"></script>
    <script>
	        function check() {
	            var name = $.trim($('#username').attr('value'));
	            var pwd = $.trim($('#password').attr('value'));
                if ("" == name || "" == pwd) {
	                alert('用户名或者密码不能为空');
                } 
                else
                {
//                  document.write(name);
                 //使用$.ajax方法获取数据
                 $.ajax({
                    //传输类型
                    type : 'post',
                    //服务器的 URL(文件名)
	                 url : 'Login',
	                 //传输数据
	                 data: {'name':name,'pwd':pwd},
	                 //回调函数
	                  success : function(msg) {
	                  msg.users
	                        var json=$.parseJSON(msg);
	                        //判空
	                        if(typeof(json)!="object")
	                        {
	                          alert("kong");
	                        }
	                        //不刷新情况下<%=(String)session.getAttribute("jin")%>"是获不到数据的
	                       document.getElementById("i").innerHTML=json.name+"  <%=(String)session.getAttribute("jin")%>";
	                    }
                  });
                /*
                  //使用Post(get也可)获取,这种方法简单
                  $.post('Login',{name:name,pwd:pwd},function(msg)
                  {document.getElementById("i").innerHTML=msg+" <%=(String)session.getAttribute("jin")%>";}
                  );
                  */
                }
	        }	 
            
    </script>
      
  </head>
  
  <body>
     用户名:<input type="text" name="username" id="username"/><br>
         密      码:<input type="password" name="password" id="password"/><br>
         <input type="button" value="提交" οnclick="check()"/>
         <div id="i"></div>
         <%String s=(String)session.getAttribute("jin"); %>
  </body>
</html>
后台的servlet

Login.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;


public class Login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Student stu=new Student(11,"jin");
		String name = request.getParameter("name") ;
        String pwd = request.getParameter("pwd") ;
//      System.out.println(name+" "+pwd);
        System.out.println(stu.id+" "+stu.name);
        response.setCharacterEncoding("utf-8");
      //注意编码类型
       response.setContentType("text/html");
        PrintWriter out = response.getWriter() ;
        HttpSession ssn = request.getSession() ;
        ssn.setAttribute("jin", name);
        ssn.setAttribute("jin1", pwd);
        //将对象转换为json
        JSONObject json=JSONObject.fromObject(stu);
        //{[jin:name],[jin1:pwd]}
        //判断json是否为空
        System.out.println(json.isEmpty());
         out.print(json);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
Student的一个类

public class Student {
	public Student(){};
	public Student(int id,String name)
	{
		this.id=id;
		this.name=name;
	}
     int id;
     String name;
     
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
     
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值