blog 用超链接来显示博文的列表,点击以后进入博文

来源:互联网 发布:珍宝猫粮怎么样 知乎 编辑:程序博客网 时间:2024/06/11 21:08


1 如何把一个文字变成一个连接
  在连接那个框框中添加一个“#”号


2 <a href="http://localhost:8088/liuwei/servlet/GetBlogListServlet">查看所有博客内容</a>


package cn.com.bean;包下面

package cn.com.bean;public class Blog {private int id;private String title;private String created_time;private String content;private int category_id;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getCreated_time() {return created_time;}public void setCreated_time(String createdTime) {created_time = createdTime;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public int getCategory_id() {return category_id;}public void setCategory_id(int categoryId) {category_id = categoryId;}public int getId() {return id;}public void setId(int id) {this.id = id;}}

package cn.com.blog;包下面

package cn.com.blog;import java.io.IOException;import java.io.PrintWriter;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.com.bean.Blog;public class GetBlogListServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");java.sql.Connection con;ResultSet resultSet = null;List<Blog> list=new ArrayList<Blog>();try {Class.forName("com.mysql.jdbc.Driver");con = DriverManager.getConnection("jdbc:mysql://localhost:3306/new_db", "root", "");java.sql.Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);String sql = "select * from blog order by id desc";resultSet = stmt.executeQuery(sql);while (resultSet.next()) {Blog blog=new Blog();blog.setId(resultSet.getInt(1));blog.setTitle(resultSet.getString(3));blog.setContent(resultSet.getString(4));blog.setCreated_time(resultSet.getString(5));list.add(blog);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}request.setAttribute("list", list);request.getRequestDispatcher("/displayBlogList.jsp").forward(request,response);}}


displayBlogList.java

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"  %><%@ page import="cn.com.bean.*"  %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><%List<Blog> list=null;list=(List)request.getAttribute("list");for(int i=0;i<list.size();i++){Blog blog=list.get(i); %><table width="750" height="320" border="0">  <tr>    <td height="52"><%=blog.getCreated_time() %></td>  </tr>  <tr>    <td height="69"><a href="http://localhost:8088/liuwei/servlet/GetBlogServlet?id=<%=blog.getId() %>"><%=blog.getTitle() %></a></td>  </tr>  <tr>    <td>    <%    String source=blog.getContent();    if(source.length()>200)    {    out.print(source.substring(0,200));    }    else{    out.print(source);    }        %>    </td>  </tr></table><p> </p><%} %><p> </p></body></html>