Java Web 实验项目 初步实现maven和idea的整合
立即下载
资源介绍:
Java Web 实验项目 初步实现maven和idea的整合
package com.controller;
import com.dao.UserDao;
import com.dao.UserDaoImpl;
import com.service.UserServiceImpl;
import javax.servlet.http.HttpServlet;
import com.entity.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/user")
public class UserController extends HttpServlet {
UserDao userDao = new UserDaoImpl();
UserServiceImpl userService = new UserServiceImpl(userDao);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
try{
if("query".equals(action)){
queryUsers(request, response);
} else if ("add".equals(action)) {
addUser(request, response);
} else if ("delete".equals(action)) {
deleteUser(request, response);
} else if ("update".equals(action)) {
updateUser(request, response);
}
} catch (SQLException e) {
e.printStackTrace();
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,"Database error");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
private void addUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
int id= Integer.parseInt(request.getParameter("id"));
String username=request.getParameter("username");
String pwd=request.getParameter("pwd");
userService.addUser(id,username,pwd);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
private void queryUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
List users = userService.getAllUsers();
request.setAttribute("users", users);
request.getRequestDispatcher("/user.jsp").forward(request, response);
}
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
String id = request.getParameter("id");
userService.deleteUser(id);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, ServletException {
int id = Integer.parseInt(request.getParameter("id"));
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
userService.updateUser(id, username, pwd);
request.getRequestDispatcher("/user.jsp").forward(request, response);
response.sendRedirect("user?action=query");
}
}
资源文件列表:
bmms_maven.zip 大约有115个文件