JSP (Java Server Page) - Ayuda novato en jsp

 
Vista:
sin imagen de perfil
Val: 1
Ha mantenido su posición en JSP (Java Server Page) (en relación al último mes)
Gráfica de JSP (Java Server Page)

Ayuda novato en jsp

Publicado por Jhoan (1 intervención) el 17/05/2019 00:10:12
hola. He estado teniendo un gran problema al usar jsp, estoy enviando un request dentro del mismo jsp para obtener unos datos y ejecutar una clase que tengo llamada ahi mismo que usa selenium y chromedriver. pero el problema es si ejecuto la clase directamente no pasa nada, pero cuando la ejecuto desde el jsp se me cambia el path del driver
1
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
y me suelta error

codigo de la clase que al ejecutar funciona normalmente

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package lib;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
 
//https://peter.sh/experiments/chromium-command-line-switches/ pagina de comandos 
public class Bot {
 
    private WebDriver driver;
    private String numero, mensaje, perfil;
 
    File file = new File("chromedriver.exe");
 
    public String getNumero() {
        return numero;
    }
 
    public void setNumero(String numero) {
        this.numero = numero;
    }
 
    public String getMensaje() {
        return mensaje;
    }
 
    public void setMensaje(String mensaje) {
        this.mensaje = mensaje;
    }
 
    public String getPerfil() {
        return perfil;
    }
 
    public void setPerfil(String perfil) {
        this.perfil = perfil;
    }
 
 
    public void iniciar() {
 
 
 
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
 
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir=Chrome/" + getPerfil());
        System.out.println(getPerfil());
        options.setBinary("/Chrome/chrome.exe");
    }
 
    public void conectarCuenta() {
 
 
 
        System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
 
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir=Chrome/" + getPerfil());
        driver = new ChromeDriver(options);
 
        driver.navigate().to("https://web.whatsapp.com/");
 
    }
 
    public void mandarAlerta() {
 
        ChromeOptions options = new ChromeOptions();
        options.addArguments("user-data-dir=Chrome/" + getPerfil());
        System.out.println("Mandar Alerta");
        options.addArguments("--window-size=780,640"); //tamaño 
        options.addArguments("--window-position=-2000,-2000"); // mandar fuera de pantalla
 
        driver = new ChromeDriver(options);
        driver.navigate().to("https://wa.me/57" + getNumero() + "?text=" + getMensaje());
        driver.findElement(By.id("action-button")).click();
 
        try {
            Thread.sleep(8000);
            Thread.sleep(8000);
        } catch (InterruptedException e) {
        }
 
        driver.findElement(By.xpath("//*[@id=\"main\"]/footer/div[1]/div[3]/button/span")).click();

    }

    public void cerrarVentana() {

        driver.close();
    }
}

codigo jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<%@page import="java.io.File"%>
<%@page import="lib.Bot"%>
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
 
        <%
            Bot bot = new Bot();
        %>
 
        <h1>Hello World!</h1>
 
        <form method="POST" action="">
            <br/>
            conectar perfil
            <br/>
            Modulo: <input type="text" name="perfil">
            <br/>
            <input type="text" name="accion" value="conectar" style="visibility: hidden"></div>
            <br/>
            <input type="submit" value="conectar" name="boton">
 
        </form>
 
        <form method="POST" action="">
            <br/>
            enviar mensaje
            <br/>
            Modulo: <input type="text" name="perfil">
            <br/>
            numero <input type="text" name="numero" ></div>
            <br/>
            <input type="text" name="accion" value="mensaje" style="visibility: hidden"></div>
            <br/>
            <input type="submit" value="conectar" name="boton">
            <br/>
        </form>
 
        <%            if (request.getParameter("boton") != null) {
 
                String perfil = request.getParameter("perfil");
                String numero = request.getParameter("numero");
                String turno = request.getParameter("turno");
                String accion = request.getParameter("accion");
 
                if (accion.equalsIgnoreCase("conectar")) {
 
                    bot.setPerfil(perfil);
 
                    bot.iniciar();
                    bot.conectarCuenta();
 
 
                }
 
                if (accion.equalsIgnoreCase("mensaje")) {
 
                    bot.setPerfil(perfil);
                    bot.setNumero(numero);
                    bot.setMensaje("Su turno " + turno + "sera el proximo en ser atendido");
 
                    bot.iniciar();
                    bot.mandarAlerta();
 
                }
            }
 
        %>
 
 
    </body>
</html>

tambien habia creado servlet ( o eso creo que es ) pero no supe como conectarlo con el jsp y lo deje sin terminar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package Controller;
 
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lib.Bot;
 
/**
 *
 * @author jhoan
 */
@WebServlet(name = "ControlBot", urlPatterns = {"/ControlBot"})
public class ControlBot extends HttpServlet {
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            System.out.println("************************************************************************");
 
            String perfil = request.getParameter("perfil");
            String numero = request.getParameter("numero");
            String turno = request.getParameter("turno");
            String accion = request.getParameter("accion");
            boolean conexion = false;
 
            Bot bot = new Bot();
 
            if (accion.equalsIgnoreCase("conectar")) {
 
                bot.setPerfil(perfil);
                bot.conectarCuenta();
                conexion = true;
 
                request.getRequestDispatcher("vistas/turno/index.jsp");
            }
 
            if (accion.equalsIgnoreCase("mensaje") && conexion == true) {
 
                System.out.println("Controller.ControlBot.processRequest()");
 
                bot.setPerfil(perfil);
                bot.setNumero(numero);
                bot.setMensaje("Su turno " + turno + "sera el proximo en ser atendido");
 
                bot.iniciar();
                bot.mandarAlerta();
 
            }
        }
    }
 
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
 
}

si alguien me podria explicar como arreglar mi error estaria muy agradecido
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder