What would be better for allowing host / port lookups
static image (e.g. car.png)
<table><tr><td><img src="http://<somehost>:<someport>/images/car.png" /></td></tr></table>
(e.g. lookup by id=123456 and fetched via a servlet from the database)
<table><tr><td><img src="http://<somehost>:<someport>/doc?id=123456"/></td></tr></table>
We generate fragments of HTML code (as mentioned above) and save them in the database, which is used to reconstruct the user page dynamically.
The problem in the above scenario is that somehost / someport is statically attached and stored in a database, which I would like to avoid, because if I need to switch to another machine with a different IP address, all the above calls will fail.
How to solve this in a general way so that I can bind at a later stage, as it concerns the host / port.
First of all, storing HTML in a database is not a good idea. But ala.
, HTML <base>, URL- .
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<head>
<c:set var="r" value="${pageContext.request}" />
<base href="${fn:replace(r.requestURL, r.requestURI, '')}${r.contextPath}/" />
</head>
<table><tr><td><img src="images/car.png" /></td></tr></table>
<table><tr><td><img src="doc?id=123456"/></td></tr></table>
.
, java.text.MessageFormat. {0}, {1}, {2} .. , , ..
<table><tr><td><img src="{0}/images/car.png" /></td></tr></table>
<table><tr><td><img src="{0}/doc?id=123456"/></td></tr></table>
/ ( !) HttpServletRequest :
HttpServletRequest r = getItSomehow();
String base = r.getRequestURL().toString().replace(r.getRequestURI(), "") + r.getContextPath();
HTML DB :
String html = getItSomehow();
String formatted = MessageFormat.format(html, base);
JSP. EL. , MVC, JSF, , MessageFormat . .
<h:outputFormat value="#{bean.html}" escape="false">
<f:param value="#{bean.base}" />
</h:outputFormat>
<img src="http://<%=request.getServerName()%>:request.getServerPort()/images/car.png" />
Java docs
,
<% @page import="java.net.InetAddress" %>
<%
InetAddress ia = InetAddress.getLocalHost();
String hostName = ia.getHostName();
%>
, , HTML- URI. , , HTML-, DNS. , , - , DNS .
, , ( , ). ( , ), . . , . . DNS. , , - DNS, -. DNS. . , :
<p>Why is it that all my baking ends up on <a href="http://cakewrecks.blogspot.com/p/faq.html">the internet</a></p>
cakewrecks.blogspot.com. , 11ag3. -, . 11ag3.content.mycompany.com
.
<p>Why is it that all my baking ends up on <a href="http://11ag3.content.mycompany.com/p/faq.html">the internet</a></p>
(, , ). , , - HTTP- ( , , ). ,
http://11ag3.content.mycompany.com/p/faq.html
11ag3.content.mycompany.com 11ag3. , , , , http://cakewrecks.blogspot.com/p/faq.html. HTTP 300 (, 307, ) URL-.
, , . , - , . , , .
, . , , - .
, , , , .
, , . , , [somehost] , . , , - , mass , Replacer ( ).
I suggest you store [somehost] and all your [...] parameters in your database as parameters such as: [x], [y], [z]. If you can put x, yz in the enumeration and save their serial number in the database, for example [1], [2], [3] in your database, then you will save a lot of space on db disks.
Later, create a Properties object that statically creates a fill static Replacer and starts a replacement with all the data that comes from your database.
public class Replacer {
private final Map<String, Object> replacements = new HashMap<String, Object>();
public Replacer () {
replacements.put("''", "\"");
}
public void addReplacement (String replaceWhat, Object replaceWith) {
replacements.put(replaceWhat, replaceWith);
}
public String replace ( Object contentToReplace ) {
String output = contentToReplace.toString();
for (String replacement : replacements.keySet() ) {
output = output.replace(replacement, replacements.get(replacement).toString() );
}
return output;
}
public static void main (String[] args) throws Exception {
testReplaceTwoSingleQuote();
}
public static void testReplaceTwoSingleQuote () throws Exception {
Replacer rep = new Replacer();
assert rep.replace( "And Mary said, ''Hello Bob''. ").equals("And Mary said, \"Hello Bob\".");
}
}