基于Java+MySQL+Tomcat+maven+JavaScript+Servlet的个人博客系统
由于这个查询涉及到的内容较多且不具体,我将提供一个基于这些技术的简单的个人博客系统的框架代码示例。
- 创建数据库和表:
CREATE DATABASE myblog;
USE myblog;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
email VARCHAR(100)
);
CREATE TABLE posts (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
title VARCHAR(100) NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
- Maven项目的
pom.xml
配置:
<dependencies>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
- 一个简单的博客发表页面(
post_form.jsp
):
<!DOCTYPE html>
<html>
<head>
<title>发表博客</title>
</head>
<body>
<form action="PostServlet" method="POST">
<label for="title">标题:</label><br>
<input type="text" id="title" name="title"><br>
<label for="content">内容:</label><br>
<textarea id="content" name="content" rows="10" cols="50"></textarea><br>
<input type="submit" value="发表">
</form>
</body>
</html>
- Servlet处理发表请求(
PostServlet.java
):
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@WebServlet("/PostServlet")
public class PostServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String title = request.getParameter("title");
String content = request.getParameter("content");
try {
Class.forNam
评论已关闭