Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package controlarticulos;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.Connection;
  11. import java.sql.DriverManager;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.servlet.ServletConfig;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.http.HttpServlet;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22.  
  23. /**
  24.  *
  25.  * @author eduardo.lafoz
  26.  */
  27. public class ControlArticulos extends HttpServlet{
  28.  
  29.     public Connection conect(){
  30.         Connection con=null;
  31.         try {
  32.             con=DriverManager.getConnection("jdbc:mysql://localhost/noticias","root","");
  33.         } catch (SQLException ex) {
  34.             Logger.getLogger(ControlArticulos.class.getName()).log(Level.SEVERE, null, ex);
  35.         }
  36.         return con;
  37.     }
  38.    
  39.     public void init(ServletConfig conf) throws ServletException{
  40.         super.init(conf);
  41.         String driver=null;
  42.         driver=conf.getInitParameter("cadenaDriver");
  43.         //cargar el driver
  44.         try{
  45.             Class.forName(driver);
  46.         }catch(ClassNotFoundException cne){
  47.  
  48.         }
  49.     }
  50.    
  51.       public void service(HttpServletRequest peticion,HttpServletResponse respuesta)
  52.                 throws ServletException, IOException{
  53.          service (peticion,respuesta);
  54.          
  55.          String boton;
  56.            boton= peticion.getParameter("enviar");
  57.          
  58.    
  59.      }
  60.      
  61.     public String autorizacion( HttpServletRequest req ) throws SQLException {
  62.        
  63.     Statement stmt = conect().createStatement();
  64.     String consulta;
  65.     ResultSet rs;
  66.     String valido = "";
  67.     String usuario = req.getParameter( "user" );
  68.     String clave = req.getParameter( "pass" );
  69.     String permiso="";
  70.  
  71.     consulta = "SELECT admitirEnvio FROM usuarios WHERE usuario = '" + usuario;
  72.     consulta += "' AND clave = '"+clave+"'";
  73.     rs = stmt.executeQuery( consulta );
  74.  
  75.     while( rs.next() ) {
  76.       valido = rs.getString(1);
  77.     }
  78.     rs.close();
  79.     stmt.close();
  80.  
  81.     if( valido.equals( "" ) ) {
  82.       permiso = "ACCESO DENEGADO";
  83.     } else {
  84.       // Permiso sólo para lectura de artículos
  85.       if( valido.equals( "N" ) ) {
  86.         permiso = "GET";
  87.       // Permiso para lectura y envío de artículos
  88.       } else if( valido.equals( "S" ) ) {
  89.         permiso = "POST";
  90.       }
  91.     }
  92.   return permiso;
  93.   }
  94.    
  95.    public void leerArticulos(HttpServletRequest peticion,HttpServletResponse respuesta)
  96.                 throws ServletException, IOException{
  97.          String usuario,clave;
  98.          usuario=peticion.getParameter("user");
  99.          clave=peticion.getParameter("pass");
  100.          
  101.          respuesta.setContentType("text/html");
  102.          PrintWriter pw=respuesta.getWriter();
  103.          
  104.          
  105.          Statement st=null;
  106.          ResultSet rs=null;
  107.          
  108.          //******************************************
  109.          
  110.          
  111.          
  112.          conect();
  113.          
  114.          
  115.          try {
  116.              st=conect().createStatement();
  117.          } catch (Exception sql) {
  118.          }
  119.          
  120.          String sql="SELECT articulos.Titulo, articulos.Cuerpo FROM articulos JOIN usuarios ON usuarios.CodUsuario=articulos.CodUsuario WHERE usuarios.Nombre='" + usuario +"' AND usuarios.Clave='" + clave+"'";
  121.          
  122.          try {
  123.              rs=st.executeQuery(sql);
  124.              
  125.              if(rs.next()){
  126.                  String claveValida=rs.getString("clave");
  127.                  if(clave.equals(claveValida)){
  128.                  pw.println("<h2>USUARIO VALIDADO</h2>");
  129.                  }else{
  130.                  pw.println("<h2>LA CLAVE NO ES CORRECTA</h2>");
  131.                  }
  132.                  
  133.              }else{
  134.                  pw.println("<h2>USUARIO NO VALIDADO</h2>");
  135.              }
  136.          } catch (Exception e) {
  137.          }
  138.          
  139.          pw.println("<HTML>");
  140.          pw.println("<BODY>");
  141.          pw.println("<h1> Mi primer Servlet </h1>");
  142.          //pw.println("<h2> Usuario : " + usuario + " </h2>");
  143.          //pw.println("<h2> Usuario : " + clave + " </h2>");
  144.          //pw.println("</BODY>");
  145.          pw.println("</HTML>");
  146.    
  147.      }
  148.    }
  149.    
  150.     /**
  151.      * @param args the command line arguments
  152.      */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement