com.maike.controller
AddTradeServlet
package com.maike.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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.maike.model.Trade;
import com.maike.model.TradeDto;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* 增添
*/
@WebServlet("/AddTradeServlet")
public class AddTradeServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddTradeServlet() {
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
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String tradeName = request.getParameter("tradeName");
String tradePrice = request.getParameter("tradePrice");
TradeDto tradeDto = new TradeDto();
tradeDto.setTradeName(tradeName);
tradeDto.setTradePrice(tradePrice);
int k = userService.addTrade(tradeDto);
if(k > 0) {
List<Trade> list = userService.seleTradeAll();
request.setAttribute("tradeList", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
}else {
response.getWriter().append("添加失败").append(request.getContextPath());
request.getRequestDispatcher("add.jsp").forward(request, response);
}
}
}
DeleteStuServlet
package com.maike.controller;
import java.io.IOException;
import java.util.List;
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.maike.model.Trade;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* Servlet implementation class DeleteStuServlet删除
*/
@WebServlet("/DeleteStuServlet")
public class DeleteTradeServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteTradeServlet() {
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
Integer pid = Integer.parseInt(request.getParameter("pid"));
System.out.println(pid);
String tradeName = request.getParameter("tradeName");
int k = userService.deleteTrade(pid);
List<Trade> list = userService.seleTradeAll();
request.setAttribute("tradeList", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
/**
* @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);
}
}
LoginServlet
package com.maike.controller;
import java.io.IOException;
import java.util.List;
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.maike.model.Trade;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* Servlet implementation class LoginServlet登陆页面
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
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 userName = request.getParameter("userName");
String uesrPwd = request.getParameter("userPwd");
int k = userService.judgeLogin(userName, uesrPwd);
if (k > 0) {
List<Trade> list = userService.seleTradeAll();
request.setAttribute("tradeList", list);
request.getRequestDispatcher("index.jsp").forward(request, response);
} else {
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
}
RegisterServlet
package com.maike.controller;
import java.io.IOException;
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.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* Servlet implementation class RegisterServlet注册页面
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
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
request.setCharacterEncoding("utf-8");
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userRePwd = request.getParameter("userRePwd");
int k = userService.insertUser(userName,userPwd,userRePwd);
if(k > 0) {
System.out.println("成功");
request.getRequestDispatcher("login.jsp").forward(request, response);
}else {
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
}
ToAddTradeServlet
package com.maike.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ToAddTradeServlet
*/
@WebServlet("/ToAddTradeServlet")
public class ToAddTradeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ToAddTradeServlet() {
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
request.getRequestDispatcher("add.jsp").forward(request, response);
}
/**
* @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);
}
}
ToUpdateTradeServlet
package com.maike.controller;
import java.io.IOException;
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.maike.model.Trade;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* Servlet implementation class ToUpdateTradeServlet
*/
@WebServlet("/ToUpdateTradeServlet")
public class ToUpdateTradeServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ToUpdateTradeServlet() {
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
Integer tradeId = Integer.parseInt(request.getParameter("tradeId"));
System.out.println(tradeId+"");
Trade trade = userService.getTradeByName(tradeId);
request.setAttribute("trade", trade);
request.getRequestDispatcher("update.jsp").forward(request, response);
}
/**
* @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);
}
}
UpdateTradeServlet
package com.maike.controller;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
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.maike.model.Trade;
import com.maike.service.UserService;
import com.maike.service.UserServiceImpe;
/**
* Servlet implementation class UpdateTradeServlet修改页面
*/
@WebServlet("/UpdateTradeServlet")
public class UpdateTradeServlet extends HttpServlet {
UserService userService = new UserServiceImpe();
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public UpdateTradeServlet() {
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
request.setCharacterEncoding("utf-8");
try {
Integer tradeid = Integer.parseInt(request.getParameter("tradeid"));
String tradeName = request.getParameter("tradeName");
String tradePrice = request.getParameter("tradePrice");
System.out.println(tradeName);
System.out.println(tradePrice);
Trade trade= new Trade();
trade.setTradeId(tradeid);
trade.setTradeName(tradeName);
trade.setTradePrice(tradePrice);
int k = userService.updateTrade(trade);
List<Trade> list = userService.seleTradeAll();
request.setAttribute("tradeList", list);
}catch(Exception e) {
}
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}