当前位置: 移动技术网 > IT编程>开发语言>Java > 写一个对搜索引擎友好的文章SEO分页类

写一个对搜索引擎友好的文章SEO分页类

2017年12月12日  | 移动技术网IT编程  | 我要评论

使用jsp/php/asp等动态程序生成的页面如何对搜索引擎友好呢?你可能想使用url_rewrite。不过,最好还是让同一个网址在任意时间对应的页面内容都是一样的或者相似的。因为搜索引擎不喜欢页面内容总是在变化的网址。

一般博客文章需要将新发表的文章显示在前面,所以会使用"order by id desc"类似的sql语句来查询一页包含的多篇文章。例如下面在java+mysql中:

public article[] getarticlearray(int from, int size){
article[] article = new article[0];
string query = "select * from blog order by desc id limit " + from + "," + size;
try{
resultset rs = st.executequery(query);
rs.last();
size = rs.getrow();
article = new article[size];
rs.beforefirst();
for(int i=0; rs.next(); i++){
article[i] = new article(
rs.getint("id"), rs.getstring("time"),
rs.getstring("name"), rs.getstring("blog")
);
}
rs.close();
}catch(exception e){
system.out.println(e);
}
return article;
}

这是我们的seo分页类myseopager中的一个方法。如果我们想显示第一页,我们使用getarticlearray(0,10)来查询最新发表的10篇文章。

这有什么问题呢?问题是当你添加一篇文章之后,原来的所有分页都改变了。为了让getarticlearray(0,10)每一次查询显示相同的文章,应 该让getarticlearray(0,10)显示新先发表的10篇文章。我们可以这样改造我们的分页类。删除与将影响页面的内容,你删除越新的文章, 生成的页面改变越大。

public article[] getarticlearray(int from, int size){
article[] article = new article[0];
string query = "select * from blog order by id limit " + from + "," + size;
try{
resultset rs = st.executequery(query);
rs.last();
size = rs.getrow();
article = new article[size];
rs.beforefirst();
for(int i=0; rs.next(); i++){
article[i] = new article(
rs.getint("id"), rs.getstring("time"),
rs.getstring("name"), rs.getstring("blog")
);
}
rs.close();
}catch(exception e){
system.out.println(e);
}
return article;
}

我们还需要得到数据库里到底有多少文章,所以再增加一个方法。

public int getarticlecount(){
int rowcount = 0;
string query = "select count(*) as rowcount from ideabook";
try{
resultset rs = st.executequery(query);
if(rs.next()){
rowcount = rs.getint("rowcount");
}
}catch(exception e){
system.out.println(e);
}
return rowcount;
}

现在我们在jsp页面中显示最新发表的10篇文章。

int start = -1;
myseopager pager = new myseopager();
int artcount = pager.getarticlecount();
try{
integer.parseint(request.getparameter("start"));
}catch(exception e){
start = artcount-10;
}
if(start > artcount - 10) start = artcount - 10;
if(start < 0) start = 0;

article art = pager.getarticlearray(start, 10);
// do something with art here.
int previous = start + 10; // 传到上一页的start值
int next = start - 10; // 传到下一页的start值

这样,生成的页面的内容的是否改变与你是否删除了先发表的文章有关系。只要你不删除文章,showblog.jsp?start=0带有这个参数的 网址对应的页面都不改变。只要你删除的是第n篇文章,那么start<(n-pagesize)对应的页面都不改变。你添加文章只影响第一页。

在我编写的使用了这种方法。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网