package com.test.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.test.StudentService.StudentService; import com.test.StudentService.StudentServiceImpl; import com.test.domain.Student; /** * 处理学生添加请求 */ public class StudentAddServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String sex = request.getParameter("sex"); String phone = request.getParameter("phone"); String birthday = request.getParameter("birthday"); String [] h = request.getParameterValues("hobby"); String hobby = Arrays.toString(h); hobby = hobby.substring(1, hobby.length()-1); String info = request.getParameter("info"); Date date = new SimpleDateFormat("yyyy-MM-dd").parse(birthday); Student student = new Student(name,sex,phone,info,hobby,date); StudentService service = new StudentServiceImpl(); service.insert(student); }catch (Exception e) { e.printStackTrace(); } request.getRequestDispatcher("StudentListServlet").forward(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }根据这段代码编写数据库
时间: 2024-02-29 12:54:19 浏览: 129
抱歉,根据这段代码无法编写出完整的数据库。此段代码只处理学生添加请求,将前端传来的学生信息插入到数据库中。但是,我们可以根据代码中的StudentService和StudentServiceImpl来猜测数据库表的结构和字段名。可能会有一个名为student的表,包含以下字段:name、sex、phone、birthday、hobby、info。其中,birthday字段的数据类型为date,hobby字段的数据类型为varchar。具体的表结构和字段名应该根据实际需求而定。
相关问题
使用过滤器完成添加留言模块 insert.jsp:包含表单及非法文字提示 InsertMessage.java:接受表单参数,jdbc操作数据库 要求: 进行中文字符处理 如果提交内容中有非法字符串,将该字符串保存在request对象发送回insert.jsp,并显示““xxx”是非法文字”。(设定多个非法字符串) 如果登录非法,转发回insert.jsp。
下面是一个使用过滤器完成添加留言模块的示例代码:
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页面,同时显示提示信息。如果提交内容合法,则继续执行后续操作。
阅读全文