Extjs與servlet交互

運用ajax中,在前端和後臺之間進行參數的傳遞。

在html中主要的代碼:

 <script type="text/javascript" >

Ext.onReady(function(){
function getMenuAjax(start,end) {
			Ext.Ajax.request({ 
			url : './ServletAjax', 
			method : 'post', 
			async: false,
			params : { 
					 m:start,
					 n:end
			}, 
			success : function(response, options) {
						alert(response.responseText);
										 
			}, 
			failure : function() {
						alert('獲取數據失敗');
			} 
			}); 
		} 
		getMenuAjax(0,10);		
		
	
}); 
</script>


在servlet中代碼:

/**
 * Servlet implementation class ServletAjax
 */
//說明這個Servlet是沒有序列號的    
@SuppressWarnings("serial")    
//說明這個Servlet的名稱是ServletAjax,其地址是/ServletAjax  
//這與在web.xml中設置是一樣的    
@WebServlet(name = "ServletAjax", urlPatterns = { "/ServletAjax" })   
public class ServletAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=utf-8");  
		PrintWriter pw = response.getWriter();  
		request.setCharacterEncoding("utf-8");  
		String param1=request.getParameter("m");  
		String param2=request.getParameter("n");         
		pw.print("前臺傳來了參數:m="+param1+",n="+param2);  
		pw.flush();  
		pw.close();  
	}

}


在整一個項目的框架:




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章