Home > database >  How do I upload JSP file content to textarea
How do I upload JSP file content to textarea

Time:08-24

I want to edit an jsp file from textarea in browser. So i need to upload jsp content to textarea. For example i have a readMe.jsp file like that:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>Read Me</h1>
<p> This is read me content</p>

I want this edit like that:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<h1>Read Me</h1>
<p> This is read me content</p>
<hr>
<h2>Read Me Second Head</h2>
<p>This is read me content 2</p>

The way I think to be able to do this in the browser is to upload the jsp content to textarea and edit it from there. Does anyone know how I can do this? If I have a way to edit a jsp file differently from the browser, it can also be.
Thanks.

CodePudding user response:

This is just an example how a JSP page can be edited from a browser.

webapp/readme.jsp is a page to edit.

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Read Me</title>
</head>
<body>
<h1>Read Me</h1>
<p> This is read me content</p>
<p><a href="${pageContext.request.contextPath}/edit/readme">Edit this page</a></p>
</body>
</html>

webapp/edit/readme.jsp contains a form within that a content of a readme.jsp page can be edit.

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Edit | Read Me</title>
</head>
<body>
<h1>'Read Me' edit page</h1>
<form method="post" action="${pageContext.request.contextPath}/edit/readme">
    <label>
        <textarea name="newContent" cols="96" rows="16">${content}</textarea>
    </label>
    <br><br>
    <button type="submit">Save</button>
</form>
</body>
</html>

EditReadMe.java is a servlet.

The method doGet is triggered by the Edit this page link from the webapp/readme.jsp. It read content from the webapp/readme.jsp file and passes it to the webapp/edit/readme.jsp.

The method doPost is responsible for working with the form from the webapp/edit/readme.jsp and updating content for the webapp/readme.jsp file.

package com.example.editjsp;

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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@WebServlet(name = "readMeEditor", value = "/edit/readme")
public class EditReadMe extends HttpServlet {

    private static final String RESOURCE_PATH = "/readme.jsp";
    private Path pathToFile;

    @Override
    public void init() {
        pathToFile = Path.of(getServletContext().getRealPath(RESOURCE_PATH));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String contentToEdit = Files.readString(pathToFile);
        req.setAttribute("content", contentToEdit);
        req.getRequestDispatcher("/edit/readme.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Files.writeString(pathToFile, req.getParameter("newContent"));
        resp.sendRedirect(req.getContextPath()   RESOURCE_PATH);

    }

}
  • Related