企业管理系统定制开发制作表白墙,给TA一个惊喜吧

目录


展示

1. 创建Maven 项目

使用 IDEA 创建一个 Maven 项目

1) 菜单 -> 文件 -> 新建项目 -> Maven

 2) 企业管理系统定制开发项目创建完毕

企业管理系统定制开发创建好之后,企业管理系统定制开发会生成一些默认的目录

3)添加文件

企业管理系统定制开发我们需要手动添加6个文件,

一个是在java 企业管理系统定制开发路径下建一个ConfessionServlet 文件,企业管理系统定制开发用来存放服务器代码。

在Java路径下创建 文件,用来与MySQL之间的交互

然后再 main 路径下创建一个webapp文件夹

再在webapp文件夹下创建一个WEB_INF文件夹 、一个 .html文件(用来存放客户端代码)、 一个 .jpg 文件(存放背景图,可以根据自己的喜好来选择图片)

在WEB_INF文件夹下创建 web.xml 文件

 

 2.源代码

2.1 web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. </web-app>

2.2 pom.xml

在pom.xml文件下增加以下代码片段,如果代码出现报红出错状态的话,点击右上角Maven 的刷新图标,等待即可。

  1. <dependencies>
  2. <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
  3. <dependency>
  4. <groupId>javax.servlet</groupId>
  5. <artifactId>javax.servlet-api</artifactId>
  6. <version>3.1.0</version>
  7. <scope>provided</scope>
  8. </dependency>
  9. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  10. <dependency>
  11. <groupId>com.fasterxml.jackson.core</groupId>
  12. <artifactId>jackson-databind</artifactId>
  13. <version>2.12.6.1</version>
  14. </dependency>
  15. <!-- 引入 mysql 驱动包 -->
  16. <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
  17. <dependency>
  18. <groupId>mysql</groupId>
  19. <artifactId>mysql-connector-java</artifactId>
  20. <version>5.1.47</version>
  21. </dependency>
  22. </dependencies>

2.3 ConfessionServelet 类

 用来存在服务器代码。

其中 @WebServlet("/confession")  是客户端向服务器约定好的请求方式

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. class Confession {
  15. public String from;
  16. public String to;
  17. public String confession;
  18. }
  19. @WebServlet("/confession")
  20. public class ConfessionServlet extends HttpServlet {
  21. private ObjectMapper objectMapper = new ObjectMapper();
  22. @Override
  23. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  24. //处理提交消息请求
  25. Confession confession = objectMapper.readValue(req.getInputStream(),Confession.class);
  26. //保存到内存中
  27. // 通过 ContentType 来告知页面, 返回的数据是 json 格式.
  28. // 有了这样的声明, 此时 jquery ajax 就会自动的帮我们把字符串转成 js 对象.
  29. // 如果没有, jquery ajax 就只是当成字符串来处理的
  30. save(confession);
  31. resp.setContentType("application/json; charset=utf8");
  32. resp.getWriter().write("{\"ok\":true}");
  33. }
  34. @Override
  35. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  36. //获取到消息列表,只要把消息列表中的内容整个都返回给客户端即可
  37. //此处需要使用objectMapper 把 Java 对象,转换成json 格式的字符串
  38. List<Confession> confessions = load();
  39. String jsonString = objectMapper.writeValueAsString(confessions);
  40. System.out.println("jsonString: " + jsonString);
  41. resp.setContentType("application/json; charset=utf8");
  42. resp.getWriter().write(jsonString);
  43. }
  44. private void save(Confession confession) {
  45. //把第一条消息保存到数据库中
  46. Connection connection = null;
  47. PreparedStatement statement = null;
  48. //1.和数据库建立连接
  49. try {
  50. connection = DBUtil.getConnection();
  51. //2.构造 SQL
  52. String sql = "insert into confession values(?, ?, ?)";
  53. statement = connection.prepareStatement(sql);
  54. statement.setString(1,confession.from);
  55. statement.setString(2,confession.to);
  56. statement.setString(3,confession.confession);
  57. //3.执行SQL
  58. statement.executeUpdate();
  59. } catch (SQLException e) {
  60. e.printStackTrace();
  61. } finally {
  62. //关闭连接,释放资源
  63. DBUtil.close(connection,statement,null);
  64. }
  65. }
  66. private List<Confession> load() {
  67. //从数据库中获取到所有的消息
  68. List<Confession> confessions = new ArrayList<>();
  69. Connection connection = null;
  70. PreparedStatement statement = null;
  71. ResultSet resultSet = null;
  72. try {
  73. connection = DBUtil.getConnection();
  74. String sql = "select * from confession";
  75. statement = connection.prepareStatement(sql);
  76. resultSet = statement.executeQuery();
  77. while (resultSet.next()) {
  78. Confession confession = new Confession();
  79. confession.from = resultSet.getString("from");
  80. confession.from = resultSet.getString("to");
  81. confession.from = resultSet.getString("confession");
  82. confessions.add(confession);
  83. }
  84. } catch (SQLException throwables) {
  85. throwables.printStackTrace();
  86. } finally {
  87. DBUtil.close(connection, statement, resultSet);
  88. }
  89. return confessions;
  90. }
  91. }

2.4 DBUtil 类

这个类是用来与Mysql 建立连接,将表白的话存放到 Mysql 中,使用到的是JDBC编程。

使用JDBC基本流程:
1.创建数据源
2.和数据库建立连接
3.构造sq|语句
4.执行sq|语句
5.如果是查询语句,需要遍历结果集.如果是插入/删除/修改,则不需要
6.关闭连接,释放资源 

  1. import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
  2. import javax.sql.DataSource;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. public class DBUtil {
  8. //与数据库建立连接
  9. private static final String URL = "jdbc:mysql://127.0.0.1:3306/java?characterEncoding=utf8&useSSL=false";
  10. private static final String USERNAME = "root";
  11. private static final String PASSWORD = "123456";
  12. private volatile static DataSource dataSource = null;
  13. private static DataSource getDataSource() {
  14. if (dataSource == null) {
  15. synchronized (DBUtil.class) {
  16. if (dataSource == null) {
  17. dataSource = new MysqlDataSource();
  18. ((MysqlDataSource)dataSource).setUrl(URL);
  19. ((MysqlDataSource)dataSource).setUser(USERNAME);
  20. ((MysqlDataSource)dataSource).setPassword(PASSWORD);
  21. }
  22. }
  23. }
  24. return dataSource;
  25. }
  26. public static Connection getConnection() throws SQLException {
  27. return getDataSource().getConnection();
  28. }
  29. public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
  30. if (resultSet != null) {
  31. try {
  32. resultSet.close();
  33. } catch (SQLException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. if (statement != null) {
  38. try {
  39. statement.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. if (connection != null) {
  45. try {
  46. connection.close();
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }

在上述代码中

2.5 confessWall.html

这个代码是客户端代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>表白墙</title>
  8. </head>
  9. <body>
  10. <style>
  11. /* 整个页面放背景图的,html, 不可少*/
  12. html,
  13. body {
  14. height: 100%;
  15. }
  16. body {
  17. background-image: url(wall.jpg);
  18. background-repeat: no-repeat;
  19. background-position: center center;
  20. background-size: cover;
  21. }
  22. * {
  23. margin: 0;
  24. padding: 0;
  25. box-sizing: border-box;
  26. }
  27. .container {
  28. width: 100%;
  29. }
  30. h3 {
  31. text-align: center;
  32. padding: 30px 0;
  33. font-size: 24px;
  34. color: black;
  35. }
  36. p {
  37. text-align: center;
  38. color: rgb(241, 236, 35);
  39. padding: 10px 0;
  40. }
  41. .row {
  42. width: 400px;
  43. height: 50px;
  44. margin: 0 auto;
  45. display: flex;
  46. justify-content: center;
  47. align-items: center;
  48. }
  49. .row span {
  50. width: 60px;
  51. font-size: 20px;
  52. /* color: azure; */
  53. }
  54. .row input {
  55. width: 300px;
  56. height: 40px;
  57. line-height: 40px;
  58. font-size: 20px;
  59. text-indent: 0.5em;
  60. /* 去掉输入框的轮廓线 */
  61. outline: none;
  62. }
  63. .row #submit {
  64. width: 200px;
  65. height: 40px;
  66. font-size: 20px;
  67. line-height: 40px;
  68. margin: 0 auto;
  69. color: white;
  70. background-color: rgb(241, 238, 35);
  71. /* 去掉框的轮廓线 */
  72. border: none;
  73. border-radius: 10px;
  74. }
  75. .row #submit:active {
  76. background-color: gray;
  77. }
  78. </style>
  79. <div class="container">
  80. <h3>表白墙</h3>
  81. <p>欢迎来到表白墙,勇敢说出你心灵深处的爱吧!</p>
  82. <div class="row">
  83. <span>谁:</span>
  84. <input type="text">
  85. </div>
  86. <div class="row">
  87. <span>对谁:</span>
  88. <input type="text">
  89. </div>
  90. <div class="row">
  91. <span>说:</span>
  92. <input type="text">
  93. </div>
  94. <div class="row">
  95. <button id="submit">提交</button>
  96. </div>
  97. </div>
  98. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  99. <script>
  100. //加入Ajax的代码,此处加入的逻辑有两部分
  101. //1) 点击按钮提交的时候,Ajax 要构造数据发送给服务器,
  102. // 2) 页面加载的时候,从服务器获取消息列表,并在界面上显示
  103. function getMessages() {
  104. $.ajax({
  105. type: 'get',
  106. url: 'confession',
  107. success: function(body) {
  108. //当前body 已经是一个js 对象数组了,Ajax 会根据响应的 content type 来进行解析
  109. //如果服务器返回的content-type 已经是application/json 了,Ajax就会把body自动转换成js对象
  110. //如果客户端没有自动链接,也可以通过JSON.parse() 这个函数自动转换
  111. //依次来取数组转换的每个元素
  112. let container = document.querySelector('.container');
  113. for (let confession of body) {
  114. let div = document.createElement('div');
  115. div.innerHTML = from + ' 对 ' + to + ' 说: ' + msg;
  116. div.className = 'row';
  117. container.appendChild(div);
  118. }
  119. }
  120. });
  121. }
  122. //加上函数调用
  123. getMessages();
  124. // 当用户点击 submit, 就会获取到 input 中的内容, 从而把内容构造成一个 div, 插入到页面末尾
  125. let submitBtn = document.querySelector('#submit');
  126. submitBtn.onclick = function() {
  127. // 1.获取到3个input中的内容
  128. let inputs = document.querySelectorAll('input');
  129. let from = inputs[0].value;
  130. let to = inputs[1].value;
  131. let msg = inputs[2].value;
  132. if (from == '' || to == '' || msg == '') {
  133. //用户没填写完,暂时部提交数据
  134. ruturn;
  135. }
  136. // 2. 生成一个新的 div, 内容就是 input 里的内容. 把这个新的 div 加到页面中.
  137. let div = document.createElement('div');
  138. div.innerHTML = from + ' 对 ' + to + ' 说: ' + msg;
  139. div.className = 'row';
  140. let container = document.querySelector('.container');
  141. container.appendChild(div);
  142. // 3. 清空之前输入框的内容.
  143. for (let i = 0; i < inputs.length; i++) {
  144. inputs[i].value = '';
  145. }
  146. //4.把当前获取到的输入框内容,构造成一个HTTP post 请求,通过Ajax 发送给服务器
  147. let body = {
  148. from: from,
  149. to: to,
  150. confession: msg
  151. };
  152. $.ajax({
  153. type: "post",
  154. url: "confession",
  155. contentType: "application/json;charset=utf8",
  156. data: JSON.stringify(body),
  157. success: function(body) {
  158. alert("表白成功");
  159. },
  160. error: function() {
  161. alert("表白失败");
  162. }
  163. })
  164. }
  165. </script>
  166. </body>
  167. </html>

在上述代码中

  

3. Tomcat 部署 

4.创建库

在MySQL创建一个库,然后再库里创建一个表,来保存表白的数据。

其中 from 和 to  使用的是反引号。在键盘的左上角。

  1. create database if not exists confession_wall;
  2. use confession_wall;
  3. drop table if exists confession;
  4. create table confession (
  5. `from` varchar(1024),
  6. `to` varchar(1024),
  7. message varchar(4096)
  8. );

5. 部署到上

5.1 数据库的部署

将库和表创建到云服务器上,即 xshell 软件

 5.1 打包

1)先在pom.xml 中增加 war 包的名字

  1. <packaging>war</packaging>
  2. <build>
  3. <finalName>confession</finalName>
  4. </build>

2)DBUtil 类中的密码去掉,如下

  1. private static final String USERNAME = "root";
  2. private static final String PASSWORD = "";

 3) 打包

在右上角Maven处找到如下双击打包

 然后再左边目录栏就可以生成 confession.war 包,然后操作如下

 就可以打开  confession.war 所在的文件夹位置

 4)部署到云服务器

先在 xshell 中输入以下命令,即打开Tomcat

  1. //打开Tomcat所在的目录
  2. cd java/
  3. //打开安装的Tomcat
  4. cd apache-tomcat-8.5.78/
  5. //打开webapps
  6. cd webapps/

然后 将打包的 confession.war 包拖到 xshell 

这样子就部署完成

6.效果

因为我们已经约定好交互方式并且部署好了,所以就可以生成连接:(如果想让别人也放访问到,就需要与云服务器连接,云服务连接在开头。不然只能直接创建项目来部署,自己访问,自己访问连接如下)

 表白的数据存储在数据库

7.特别说明

在文中 1-4段 和第6 段是可以单独在idea上面部署的,不过结果只能自己看。第5段是在云服务器上面部署的,部署了别就可以通过连接看到。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发