Spring Leaning: Servlet and Controller
Controller is essentially the same as a Servlet in Java Web development but under MVC framework. What a controller/servlet does is
- Receive user Requests
- Control service dispatch
- Send the request and code to Service layer.
High-Level flow of Servlet
Create a Servlet
1. create a Servlet
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter();
writer.print("Hello,Serlvet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Step 2: Register Servlet mapping
< !--register Servlet-->
< servlet>
< servlet-name>myServlet
< servlet-class>com.siyu.sevlet.MyServlet
< /servlet>
< !--servlet request path-->
< servlet-mapping>
< servlet-name>myServlet
< url-pattern>/siyu
< /servlet-mapping>