1.首先我們先用Flex拉一個表單如下圖,接著編輯MXML加入HttpService與將物件標上id
以下是code的部分,看的方法用顏色一樣的表示有關聯~~紅色部分表示您要傳資料至url的設定,其他就對照顏色看嚕
-------------------------------MXML---------------------------------
//基本上這個httpEncoding是為了傳網頁中文參數所以需先encoding
private function httpEncoding(param:String):String{
return encodeURIComponent(param);
}
private function insert_data():void{
var params:URLVariables = new URLVariables();
//塞你要傳的值給params變數
params.member_name = httpEncoding(this.member_name.text);
params.member_sex= httpEncoding(this.member_sex.selectedValue.toString());
params.member_birthday= httpEncoding(this.member_birthday.text);
//post至servlet
add_to_servlet.send(params);
}
2.撰寫Servlet端
---------------------------------------------JAVA Servlet-----------------------------------------
public class FlexServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/xml;charset=utf-8");
resp.setHeader("Cache-Control", "no-cache");
String action = req.getParameter("action");
System.out.println("action:"+action);
if(action.equals("member_list")){
doQueryMemberList(req,resp);
}else if(action.equals("member_add")){
doInsertMember(req,resp);
}
}
private void doInsertMember(HttpServletRequest req,HttpServletResponse resp){
String member_name=URLDecoder.decode(req.getParameter("member_name"));
String member_sex=URLDecoder.decode(req.getParameter("member_sex"));
String member_birthday=URLDecoder.decode(req.getParameter("member_birthday"));
}
---------------------------------------------JAVA Servlet-----------------------------------------
這樣您就可以在java上進行db的新增與修改資料嚕....db那段有需要我在補上嚕.....
沒有留言:
張貼留言