用java实现一个亲戚称呼系统
时间: 2024-01-11 22:04:52 浏览: 72
response);
} else {
User user = new User();
user.setUsername(username);
user.setPassword(MD5Util.md5(password));
userDao.insert(user);
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
} catch (SQLException e) {
好的,针对您的需求,我可以提供一个简单的Java程序来实现一个基于交互的 e.printStackTrace();
}
}
}
```
7. 实现留言的Servlet
我们可以使用`MessageServlet`来亲戚称呼系统。具体实现过程如下:
1. 首先,您需要定义一个包含亲戚称实现留言功能。
```java
package com.example.servlet;
import com.example.bean.Message;
import com.example.bean.User;
import com呼关系的数据结构。在这个例子中,我会使用一个HashMap来存储亲戚关系,其中.example.dao.MessageDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
key代表称呼关系,value代表对应的称呼。例如:
```java
HashMap<String, String> relationship =import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet new HashMap<>();
relationship.put("father", "父亲");
relationship.put("mother", "母亲");
relationship.put("brother", "兄("/message")
public class MessageServlet extends HttpServlet {
private MessageDao messageDao;
public void init() {
messageDao = new弟");
relationship.put("sister", "姐妹");
relationship.put("grandfather", "祖父");
relationship.put("grandmother", " MessageDao();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
response.sendRedirect(request.getContextPath() + "/祖母");
relationship.put("son", "儿子");
relationship.put("daughter", "女儿");
relationship.put("husband", "丈login.jsp");
return;
}
try {
List<Message> list = messageDao.findAll();
request.setAttribute("list", list夫");
relationship.put("wife", "妻子");
```
2. 接下来,我们需要编写一个方法来处理用户输入);
request.getRequestDispatcher("/message.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
的关系并返回对应的称呼。我们可以使用Java的正则表达式来处理用户输入的字符串,从 }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
User user = (而得到对应的称呼关系。例如:
```java
public static String getRelationship(String input) {
Pattern p =User) session.getAttribute("user");
if (user == null) {
response.sendRedirect(request.getContextPath() + "/login.jsp");
return;
}
String content = request.getParameter("content");
Message message = new Message();
message.setUserId(user.getId());
Pattern.compile("(父|母|兄|姐|弟|妹|祖父|祖母|儿|女|夫|妻)");
message.setUsername(user.getUsername());
message.setContent(content);
try {
messageDao.insert(message);
response.sendRedirect(request.getContextPath() + "/ Matcher m = p.matcher(input);
if (m.find()) {
String key = "";
switch (m.group()) {
casemessage");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上就是一个简单的 "父":
key = "father";
break;
case "母":
key = "mother";
break;
caseJSP+Servlet+JDBC实现的留言板的示例代码。
阅读全文