HFR1994
HFR1994

Reputation: 566

Modify HTML content before invocking request.getRequestDispatcher() JSP?

I just started with JSP, I want to use a HTTPServletResponse to return a template html file that I have saved in a folder, using request.getRequestDispatcher(link to html) but first I wanted to modify some content. So for example this is my test code

<!DOCTYPE html>
<html>
<head></head>
<body>
  <div id="container">
    <form method="post" action="./Registra">
      <div id="Menu-Top">
        <div id="lbltitulo">Registro de Transacción</div>
      </div>
      <div id="wrapper">
        <div id="campos">
          <div id="primera">
            <div class="holder espacio">
              <div class="formato" id="lbllittlekey">LittleKey</div>
              <br>
              <div id="text"></div>
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
</body>

                          

How do I insert inner HTML text in the div with the id ("text") from my JSP?

Thanks

Upvotes: 0

Views: 159

Answers (1)

MaVRoSCy
MaVRoSCy

Reputation: 17839

There is a way but the request.getRequestDispatcher() is not needed. You just need jSoup ... Here is how it goes...

Here is your project file structure:

enter image description here

And in the index.jsp you have this code:

<%@page import="org.jsoup.nodes.Document"%>
<%@page import="org.jsoup.Jsoup"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
String relativeWebPath = "newhtml.html"; //relative path to resource
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath); //absolute path
File input = new File(absoluteDiskPath); //get actual file 
Document doc = Jsoup.parse(input, "UTF-8", "/"); //parse it
doc.getElementById("text").html("<strong>Hey!!!</strong>"); //append required text
out.print(doc.html()); //output it!
%>

When you invoke the index.jsp you will get this output:

enter image description here

Upvotes: 1

Related Questions