展开全部
没满意答案,我自己回答.我也只是初学者,水平非常有限,这未必正确,但希望给需要62616964757a686964616fe4b893e5b19e31333335326162这方面信息的人一个参考: 我写的是登录环节,输入密码,帐号,到java端的servlet,servlet再去数据库查询,返回信息到登录页面. 1.我上面的flex项目已经以xml格式发送到java端的servlet. 下面提供flex 下面写servlet的说明: 我这是用dom4j,没有的人先去网上下在这个包,然后导入, import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Connection; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import java.sql.DriverManager; import java.sql.Statement; //下面一个class本来好像是dom4j里面,当时什么情况忘记了,反正这样能实现效果 class Dom4jUtil1 { public static void writeDocToOut(Document doc, String sCharSet, OutputStream out) { if(null==doc)return ; OutputFormat fmt = new OutputFormat(" ", true, sCharSet); try { org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(out, fmt); doc.setXMLEncoding(sCharSet); xmlWriter.write(doc); xmlWriter.close(); } catch (Exception e) { e.printStackTrace(); }} public static Document getDocFromStream(InputStream in) { Document doc = null; try { doc = new SAXReader().read(in); } catch (Exception e) { } return doc; } } //以上可以复制不改,永恒不变,下面是自己写的servlet-->loginxiaow () public class loginxiaow extends HttpServlet { //构造方法,我抄的,有什么影响不知道. public loginxiaow() { super(); } //也是抄的 public void destroy() { super.destroy(); // Just puts "destroy" string in log } //下面自己的实际需要 private void xiaow(HttpServletRequest request, HttpServletResponse response) throws Exception{ Document rtdoc=null; //传输信息必须的部分 InputStream in; in = request.getInputStream(); //Dom4jUtil1,这就是上面那个Dom4jUtil1 class Document reqDoc=Dom4jUtil1.getDocFromStream(in); Element root=reqDoc.getRootElement();//Element是元素,xml里面的信息 //xml里面的,大概是这样,其中xxx就是你的是String action取得值 String action=root.valueOf("@ACTION"); System.out.println("action=="+action);//测试用,java端显示 if("loginxiaow".equalsIgnoreCase(action)){//注册请求用户信息 // System.out.println("username=="+root.valueOf("@USERNAME")); // System.out.println("password=="+root.valueOf("@PASSWORD")); } String username=root.valueOf("@USERNAME"); String password=root.valueOf("@PASSWORD"); if(username!=null&&password!=null) {//sql字符串,就是输入到数据库的语句,整句输出 String sql="select * from xiaow_user where [user]='"+username+"' and password='"+password+"'"; System.out.println(sql); SQL(sql); System.out.println(); } if(canGo==true)//canGo判断是否有权限进入下一个页面,不过这个写法很烂 { canGo=false; rtdoc=DocumentHelper.createDocument(); System.out.println("if(canGo==true):"+canGo); Element rtroot=rtdoc.addElement("DATAPACKET");//创建 元素/节点 rtroot.addAttribute("message", "登录成功");//属性message System.out.println("登录成功"); OutputStream out=response.getOutputStream(); Dom4jUtil1.writeDocToOut(rtdoc, "UTF-8", out); out.flush(); out.close(); }else{ rtdoc=DocumentHelper.createDocument(); Element rtroot=rtdoc.addElement("DATAPACKET"); rtroot.addAttribute("message", "密码错误"); System.out.println("密码错误"); System.out.println("if(canGo==false):"+canGo); OutputStream out=response.getOutputStream(); Dom4jUtil1.writeDocToOut(rtdoc, "UTF-8", out); out.flush(); out.close(); } OutputStream out=response.getOutputStream(); Dom4jUtil1.writeDocToOut(rtdoc, "UTF-8", out); out.flush(); out.close(); } boolean canGo=false; public void SQL(String sql) { String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动 String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=dscy9999"; //连接服务器和数据库,我的是这个dscy9999 String userName = "admin"; //数据库用户名 String userPwd = "admin"; //数据库密码 Connection dbConn; Statement stmt =null; ResultSet rt=null; try { Class.forName(driverName); dbConn = DriverManager.getConnection(dbURL, userName, userPwd); stmt=dbConn.createStatement(); System.out.println("连接成功,rt="+rt); //如果连接成功 控制台输出连接成功 rt=stmt.executeQuery(sql); //遍历结果集 while(rt.next()) { if(null!=rt.getString("user")&&null!=rt.getString("password")) { canGo=true; System.out.println("canGo=true;"+canGo); } else{System.out.println("if(null!=");} System.out.println("用户:"+rt.getString("user")+"密码:"+rt.getString("password")) ; } rt.close(); dbConn.close(); } catch (Exception e) { e.printStackTrace(); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { xiaow(request,response); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { doGet(request,response); }} 大概是这样,我也不太会,但是我这个功能实现了
本回答由提问者推荐
已赞过
已踩过<
你对这个回答的评价是?
评论
收起