我在html中有一个名为“messages”的表。 列“消息”和“类型”。 我想从java servlet中添加此表中的数据。 我怎么能这样做?
答案 0 :(得分:0)
假设您在阵列中有消息,可以这样做。只需复制并粘贴即可。干杯! :d
Servlet1.java
package com.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "Servlet1", urlPatterns = {"/"})
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String[][] messages = {
{"Message 1", "Type 1"},
{"Message 2", "Type 2"},
{"Message 3", "Type 3"},
};
request.setAttribute("messageList", messages);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
的index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table id="messages" border="1">
<tr>
<th>Message</th>
<th>Type</th>
</tr>
<c:forEach var="msg" items="${requestScope.messageList}">
<tr>
<td>${msg[0]}</td>
<td>${msg[1]}</td>
</tr>
</c:forEach>
</table>
</body>
</html>