Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package controlarticulos;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author eduardo.lafoz
- */
- public class ControlArticulos extends HttpServlet{
- public Connection conect(){
- Connection con=null;
- try {
- con=DriverManager.getConnection("jdbc:mysql://localhost/noticias","root","");
- } catch (SQLException ex) {
- Logger.getLogger(ControlArticulos.class.getName()).log(Level.SEVERE, null, ex);
- }
- return con;
- }
- public void init(ServletConfig conf) throws ServletException{
- super.init(conf);
- String driver=null;
- driver=conf.getInitParameter("cadenaDriver");
- //cargar el driver
- try{
- Class.forName(driver);
- }catch(ClassNotFoundException cne){
- }
- }
- public void service(HttpServletRequest peticion,HttpServletResponse respuesta)
- throws ServletException, IOException{
- service (peticion,respuesta);
- String boton;
- boton= peticion.getParameter("enviar");
- }
- public String autorizacion( HttpServletRequest req ) throws SQLException {
- Statement stmt = conect().createStatement();
- String consulta;
- ResultSet rs;
- String valido = "";
- String usuario = req.getParameter( "user" );
- String clave = req.getParameter( "pass" );
- String permiso="";
- consulta = "SELECT admitirEnvio FROM usuarios WHERE usuario = '" + usuario;
- consulta += "' AND clave = '"+clave+"'";
- rs = stmt.executeQuery( consulta );
- while( rs.next() ) {
- valido = rs.getString(1);
- }
- rs.close();
- stmt.close();
- if( valido.equals( "" ) ) {
- permiso = "ACCESO DENEGADO";
- } else {
- // Permiso sólo para lectura de artículos
- if( valido.equals( "N" ) ) {
- permiso = "GET";
- // Permiso para lectura y envío de artículos
- } else if( valido.equals( "S" ) ) {
- permiso = "POST";
- }
- }
- return permiso;
- }
- public void leerArticulos(HttpServletRequest peticion,HttpServletResponse respuesta)
- throws ServletException, IOException{
- String usuario,clave;
- usuario=peticion.getParameter("user");
- clave=peticion.getParameter("pass");
- respuesta.setContentType("text/html");
- PrintWriter pw=respuesta.getWriter();
- Statement st=null;
- ResultSet rs=null;
- //******************************************
- conect();
- try {
- st=conect().createStatement();
- } catch (Exception sql) {
- }
- String sql="SELECT articulos.Titulo, articulos.Cuerpo FROM articulos JOIN usuarios ON usuarios.CodUsuario=articulos.CodUsuario WHERE usuarios.Nombre='" + usuario +"' AND usuarios.Clave='" + clave+"'";
- try {
- rs=st.executeQuery(sql);
- if(rs.next()){
- String claveValida=rs.getString("clave");
- if(clave.equals(claveValida)){
- pw.println("<h2>USUARIO VALIDADO</h2>");
- }else{
- pw.println("<h2>LA CLAVE NO ES CORRECTA</h2>");
- }
- }else{
- pw.println("<h2>USUARIO NO VALIDADO</h2>");
- }
- } catch (Exception e) {
- }
- pw.println("<HTML>");
- pw.println("<BODY>");
- pw.println("<h1> Mi primer Servlet </h1>");
- //pw.println("<h2> Usuario : " + usuario + " </h2>");
- //pw.println("<h2> Usuario : " + clave + " </h2>");
- //pw.println("</BODY>");
- pw.println("</HTML>");
- }
- }
- /**
- * @param args the command line arguments
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement