我有一个网站,提供新闻,每20分钟更新一次。我想动态地制作sitemap.xml。
如果我有一个静态文件,我应该定期更新它。创建jsp文件夹并将其映射为“sitemap_x.xml”是正确的方法吗?从我的DB获取数据并将其打印为XML格式。
我是说:
search_engine_bot -> sitemap.xml --> sitemap_x.xml --> xmlgenerator.jsp ->
out.println("<url><loc>myurl.com/content--42924</loc></url>");我很快就会有500000条新闻,所以我不想将它们同时保存为DB和xml文件。
知道吗?
发布于 2015-03-13 07:36:30
我也有同样的问题。我用servlet解决了它,它有注释@WebServlet("/sitemap.xml").。因此,当请求http://your-site.com/sitemap.xml时,servlet以XML格式生成响应。XML内部有来自数据库的链接。如下所示:
servlet:
RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/site_map.jsp");
links = getLinksFromDatabase();
request.setAttribute("baseUrl", baseUrl);
request.setAttribute("links", links);
dispatcher.forward(request, response);site_map.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<c:forEach items="${links}" var="link">
<url>
<loc><c:out value="${baseUrl}" />/<c:out value="${link}" /></loc>
</url>
</c:forEach>
</urlset>https://stackoverflow.com/questions/26951673
复制相似问题