servlet的一些应用
首先在DAO里接口和实现
void addUser(User user);
@Override
public void addUser(User user) {
String sql=“insert into xs values(null,?,?,?,?)”;
jt.update(sql,user.getUsername(),user.getSex(),user.getAge(),user.getAdress());
}
然后是Service里面的接口和实现
void adduser(User user);
public void adduser(User user) {
dao.addUser(user);
}
最后是servle
package Servlet;
import Entity.User;
import Service.IPersonService;
import Service.impl.IPersonserviceimpl;
import Entity.User;
import Service.impl.IPersonserviceimpl;
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("/addUserServlet2")
public class addUserServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
String sex =request.getParameter("sex");
String age =request.getParameter("age");
String adress =request.getParameter("adress");
IPersonService is=new IPersonserviceimpl();
User user=new User(username,sex,age,adress);
is.adduser(user);
response.sendRedirect("findUserByPageServlet");
}
}