简体   繁体   English

读取html文件并替换jsp中的字符串

[英]Read html file and replace string in jsp

How to append some text in div with id="sw" and befor "<-- adding some thing here />" and write in response output in JSP ? 如何在带有id =“ sw”的div中附加一些文本,并在“ <-在此处添加一些内容/>”之前在JSP中编写响应输出?

"index_012.html content is: “ index_012.html的内容是:

    <html><head></head><body>
<div id="maincontent" class="maincontent">
    <section id="mainthecontent">
        <div id="swRight"></div>
        <div id="sw" >
            <!--adding some thing here-->
        </div>                                
        <div class="clear"></div>
    </section>
    <div class="clear"></div>
</div>
</body></html>

my jsp code is : 我的jsp代码是:

    FileReader fr = new FileReader("index_012.html");
    BufferedReader br = new BufferedReader(fr);     
    StringBulder addingContent= "<div>Main Content</div>";
    StringBulder result="";
    String line = null;
    // finding place to adding StringBulder
    int contentPlace = -1;
    while((line=br.readLine()) != null && contentPlace==-1){
        int swIndex = line.indexOf("\"sw\"");
        contentPlace = line.indexOf(">",swIndex);
    }
if(contentPlace>-1){//contentPlace is index of where i must append "addingContent"

--------i need help here ----------
result = br.append(addingContent , contentPlace);
--------i need help here ----------
}

and then this jsp tag 然后这个jsp标签

<%= result %>

I don't need any change in index_012.html or any other file 我不需要index_012.html或任何其他文件的任何更改

write in response output 写响应输出

in Other word how to read file as StringBulder --> replace somthing --> write to response ?? 换句话说,如何将文件读取为StringBulder->替换东西->写入响应?

You need a BufferedWriter for write in a file. 您需要BufferedWriter才能写入文件。

Is better if you open a temporary file in writing mode, then you can delete the original file and rename the temporary file. 如果以写入模式打开临时文件,则更好,然后可以删除原始文件并重命名该临时文件。

I think you need something like https://stackoverflow.com/a/8563446/3785263 我认为您需要类似https://stackoverflow.com/a/8563446/3785263的内容

<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% StringBulder addingContent= "<div>Main Content</div>"; %>
<% FileReader fr = new FileReader("G:\\index_jeymarble.html");%>
<% BufferedReader br = new BufferedReader(fr);%>
<% String line = null;%><% while ((line = br.readLine()) != null) {%>
<%= line%><%    if (line.indexOf("\"sw\"")>-1 ) { %><%= addingContent %>
<%}%>
<%}%>

If any one has any idea to use regexp comment And there is some problem in pageEncoding. 如果任何人有使用regexp注释的想法,则pageEncoding中存在一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM