使用过滤器完成添加留言模块 insert.jsp:包含表单及非法文字提示 InsertMessage.java:接受表单参数,jdbc操作数据库 要求: 进行中文字符处理 如果提交内容中有非法字符串,将该字符串保存在request对象发送回insert.jsp,并显示““xxx”是非法文字”。(设定多个非法字符串) 如果登录非法,转发回insert.jsp。
时间: 2024-03-05 08:55:09 浏览: 112
Website-java.rar_mysql java 新闻
下面是一个使用过滤器完成添加留言模块的示例代码:
InsertMessage.java:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InsertMessage extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String content = request.getParameter("content");
String ip = request.getRemoteAddr();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(date);
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO messages(name, content, time, ip) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setString(2, content);
stmt.setString(3, time);
stmt.setString(4, ip);
stmt.executeUpdate();
response.sendRedirect("list.jsp");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
```
InsertFilter.java:
```java
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
public class InsertFilter implements Filter {
private List<String> illegalWords;
public void init(FilterConfig config) throws ServletException {
String illegalWordsStr = config.getInitParameter("illegalWords");
illegalWords = Arrays.asList(illegalWordsStr.split(","));
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
String name = req.getParameter("name");
String content = req.getParameter("content");
for (String word : illegalWords) {
if (name.contains(word) || content.contains(word)) {
request.setAttribute("illegalWord", word);
req.getRequestDispatcher("insert.jsp").forward(request, response);
return;
}
}
chain.doFilter(request, response);
}
public void destroy() {
illegalWords = null;
}
}
```
在web.xml文件中配置过滤器:
```xml
<filter>
<filter-name>InsertFilter</filter-name>
<filter-class>InsertFilter</filter-class>
<init-param>
<param-name>illegalWords</param-name>
<param-value>非法1,非法2,非法3</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>InsertFilter</filter-name>
<url-pattern>/insert.jsp</url-pattern>
</filter-mapping>
```
在insert.jsp中添加表单和提示信息:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加留言</title>
</head>
<body>
<h1>添加留言</h1>
<form action="InsertMessage" method="post">
姓名:<input type="text" name="name"><br>
内容:<textarea name="content"></textarea><br>
<input type="submit" value="提交">
</form>
<%
String illegalWord = (String) request.getAttribute("illegalWord");
if (illegalWord != null) {
out.println("<p>" + illegalWord + "是非法文字</p>");
}
%>
</body>
</html>
```
在上面的代码中,`InsertFilter`过滤器用于过滤表单提交的内容,如果提交内容中包含非法字符串,则将该字符串保存在request对象中,并转发回insert.jsp页面,同时显示提示信息。如果提交内容合法,则继续执行后续操作。
阅读全文