增删改查代码
AddServlet.java
package com.servlet;
import com.dao.UserDao;
import com.domain.User;
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 java.io.IOException;
@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
User u = new User();
u.setUsername(username);
u.setPassword(password);
/*u.setUsername(new String(username.getBytes("ISO-8859-1"),"UTF-8"));
u.setPassword(new String(password.getBytes("ISO-8859-1"),"UTF-8"));*/
UserDao ud = new UserDao();
ud.addUser(u);
request.getRequestDispatcher("showServlet").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
DeleteServlet.java
package com.servlet;
import com.dao.UserDao;
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 java.io.IOException;
@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
String idStr = request.getParameter("id");
int id = Integer.valueOf(idStr);
UserDao ud = new UserDao();
if(ud.deleteUser(id)){
request.setAttribute("deleted", "删除成功!");
request.getRequestDispatcher("showServlet").forward(request,response);
}else{
response.sendRedirect("showServlet");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
UpdateServlet.java
package com.servlet;
import com.dao.UserDao;
import com.domain.User;
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 java.io.IOException;
@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
String idStr = request.getParameter("id");
if (idStr != null && !idStr.equals("")) {
int id = Integer.valueOf(idStr);
UserDao dao = new UserDao();
User user = dao.findUserById(id);
request.setAttribute("user", user);
}
request.getRequestDispatcher("update.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String idStr = request.getParameter("id");
User u = new User();
u.setId(Integer.valueOf(idStr));
u.setUsername(username);
u.setPassword(password);
UserDao ud = new UserDao();
ud.updateUser(u);
request.getRequestDispatcher("showServlet").forward(request, response);
}
}
ShowServlet.java
package com.servlet;
import com.dao.UserDao;
import com.domain.User;
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 java.io.IOException;
import java.util.List;
@WebServlet("/showServlet")
public class ShowServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("utf-8");
UserDao ud = new UserDao();
List<User> list = ud.getAllUser();
request.setAttribute("list", list);
request.getRequestDispatcher("index.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}