1.jsp
<%@page import="com.model.td_test"%>
<%@page import="org.apache.jasper.tagplugins.jstl.core.ForEach"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
Insert title here
${message}
<% ArrayList list = (ArrayList)request.getAttribute("list2");
if(list == null){%>
<% }else{
for(int i = 0;i
<%=list.get(i).getName()%>
<%}
}%>
2.后端
delete:
package com.util;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.mysqlUtil;
import sun.misc.Perf.GetPerfAction;
/**
* Servlet implementation class DeleteDemo
*/
@WebServlet("/DeleteDemo")
public class DeleteDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteDemo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
String id =request.getParameter("id_one");
String sql ="delete from td_test where id="+id;
int r = mysqlUtil.del(sql);
if (r==1) {
request.setAttribute("message","成功删除");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}else {
request.setAttribute("message","失败删除");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}
}
}
insert
package com.util;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.mysqlUtil;
/**
* Servlet implementation class InsertDemo
*/
@WebServlet("/InsertDemo")
public class InsertDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public InsertDemo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name =request.getParameter("name_one");
String sql ="insert into td_test(name)values("+name+")";
int r =mysqlUtil.add(sql);
if (r==1) {
request.setAttribute("message","成功插入");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}else {
request.setAttribute("message","失败插入");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}
//doGet(request, response);
}
}
select
package com.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.td_test;
import com.mysql.mysqlUtil;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
/**
* Servlet implementation class SelectDemo
*/
@WebServlet("/SelectDemo")
public class SelectDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SelectDemo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// String id =request.getParameter("id_three");
String sql ="select name from td_test";
List
update:
package com.util;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.mysqlUtil;
/**
* Servlet implementation class UpdateDemo
*/
@WebServlet("/UpdateDemo")
public class UpdateDemo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateDemo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//doGet(request, response);
String id =request.getParameter("id_two");
String name =request.getParameter("name_two");
String sql ="update td_test set name ="+name+" where id ="+id;
int r =mysqlUtil.update(sql);
if (r==1) {
request.setAttribute("message","成功修改");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}else {
request.setAttribute("message","失败修改");
request.getAttribute("message");
RequestDispatcher dispatcher = request.getRequestDispatcher("/html/index.jsp");
dispatcher.forward(request, response);
}
}
}
百度云:链接:https://pan.baidu.com/s/1KQAhJSYrVnlni4gDguMfFA 密码:dtoz