I am doing a university project which simulates the behavior of a computer memory management and compilation of programs, I have run into the problem that I need that at the time of executing a program, in addition to loading the lines of code in a textarea that (it already works previously) also at the same time I need to load information about the name of the program, number of instructions, etc, in a different JSP than the textarea, since it is loaded in a specific iframe through a target. How can I update the information at the time. Thank you very much for your help, I accept solutions that consist of modifying the structure of the program, I program in Java but I am new to Java EE and not to mention Front-end.
Here the code.
servlets:
package Servlets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author GEORGE
*/
@WebServlet(name = "LeerArchivo", urlPatterns = {"/LeerArchivo"})
public class LeerArchivo extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
// out.println("<!DOCTYPE html>");
// out.println("<html>");
// out.println("<head>");
// out.println("<title>Servlet LeerArchivo</title>");
// out.println("</head>");
// out.println("<body>");
String ruta = "C:\\Users\\GEORGE\\Documents\\NetBeansProjects\\ChMaquina\\web\\ArchivosCh\\";
File dir = new File(ruta);
String[] nombreArchivos = dir.list();
// out.print(request.getParameter("cargar"));
try {
FileReader archivo = new FileReader(ruta + request.getParameter("cargar"));
BufferedReader filtro = new BufferedReader(archivo);
String li = filtro.readLine();
ArrayList<String> lista = new ArrayList<String>();
while (li != null) {
out.println("<h4>" + li + "</h4>");
// out.println("<h4>"+filtro.readLine()+"</h4>");
lista.add(li);
li = filtro.readLine();
}
filtro.close();
if (request.getParameter("cargar") != "") {
request.setAttribute("nombre", request.getParameter("cargar"));
request.setAttribute("lista", lista);
request.getRequestDispatcher("dirPantalla/Texto.jsp").forward(request, response);
} else {
out.print("no encontrado");
}
} catch (Exception e) {
out.print("Excepcion: " + e.getMessage());
}
// for (String re : nombreArchivos) {
// out.print(re + "<br>");
//// }
// out.println("</body>");
// out.println("</html>");
}
}
// <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>
}
JSP Text:
<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<h3><%= request.getAttribute("nombre")%> </h3>
<% ArrayList<String> lista = (ArrayList<String>) request.getAttribute("lista"); %>
<table style="width: 300px; height: 200px;">
<tr>
<td>
<textarea style="width: 30px; height: 2200px; align-items: left; resize: none" disable="true"><%
for (int i = 1; i <= lista.size(); i++) {
out.print(i + "\n");
}
%>
</textarea>
</td>
<td>
<textarea style="width: 300px; height: 2200px; align-items: left; resize: none"><%
for (String list : lista) {
out.print(list + "\n");
}
%>
</textarea>
</td>
</tr>
</table>
JSP Program: The corresponding information must go here, replacing the i variables of the for with the real information.
The idea is to be able to send the ArrayList to these two JSPs to carry out the respective Treatment.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Programa</title>
<link rel="icon" type="image/png" href="img/img_default/index.png" />
<link rel="stylesheet" mdia="screen" href="../css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" mdia="screen" href="../css/bootstrap.css.map" type="text/css">
<link rel="stylesheet" mdia="screen" href="../css/bootstrap.css" type="text/css">
<link rel="stylesheet" mdia="screen" href="../css/bootstrap-theme.css" type="text/css">
<link rel="stylesheet" mdia="screen" href="../css/bootstrap-theme.css.map" type="text/css">
<link rel="stylesheet" mdia="screen" href="../css/bootstrap-theme.min.css" type="text/css">
<script src="../js/jquery-2.1.4.min.js" type="text/javascript"></script>
<script src="../js/bootstrap.js" type="text/javascript"></script>
<script src="../js/npm.js" type="text/javascript"></script>
</head>
<body>
<table class="table table-responsive label-default small">
<tr>
<td>
ID
</td>
<td>
Programa
</td>
<td>
Ints
</td>
<td>
TLL
</td>
<td>
RLC
</td>
<td>
RLP
</td>
</tr>
<% for(int i=0;i<10;i++){
out.print("<tr><td>000"+i+"</td><td>Programa.ch"+i+"</td><td>"+(i+77)+"</td><td>"+(i+50)+"</td><td>"+(i+50)+"</td><td>"+(i+50)+"</td></tr>");
}
%>
</table>
</body>
</html>
Thanks a lot for your help. :)
As you present the problem I tell you that it cannot be done. From the client (jsp) a request is sent to the server (servlet), which processes it and redirects to another view. There's no more. You can't redirect to two sites.
What you can do is having the data in your first page, at the end of the load (onload or simply execute js at the end of the page), by javascript loading content in the other page. But this will give you crossframe scripting problems. If your second page was in a div (you could load it by ajax) then you wouldn't have this problem.
I think you should rethink your app design.