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

本文介绍了一个简单的Java Web应用,该应用使用Servlet从数据库中获取博客列表并展示出来。其中包括了如何通过连接到MySQL数据库来读取博客文章的基本信息,并将这些信息呈现给用户的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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 block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值