Java Web项目的控制器层应该写什么代码?
大家讲道理
大家讲道理 2017-04-18 10:49:19
0
4
487

我们现在在做毕业项目,要求不用框架手写一个系统。那么servlet是不是就相当于控制器?

比如说我写一个用户登录系统,这个控制器里面应该写什么内容呢?
我是不是在servlet中接收到post数据,然后传递到service层中,然后service层再根据数据new出实体,并且对实体操作,然后将这个操作通过dao应用到数据库中?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
刘奇

Your idea is the layered idea in the framework, and there is nothing wrong with doing so.

巴扎黑

If you don’t use a framework to write a system by hand, you don’t have to use the MVC structure at all. Why do you have to stick to what a controller is? Remember that all servers only do the following work: “Receive some data, do calculations, and return some data. ”, if it can meet this requirement, your system will be formed. There is no need to stick to any structure

伊谢尔伦

Servlet has only two functions, receiving requests and forwarding. Receive form data, encapsulate it into corresponding objects, and then pass it to service

刘奇

First of all, you need to understand a few points. The framework is also encapsulated on the basis of Servlet and becomes easier to use.
Your idea is right, but I suggest you take a look at the Servlet API and you will understand it immediately.
Let me give you some ideas. After the Servlet is started, tomcat is responsible for monitoring http requests and forwarding them to your application.
Every time a new request comes, the service method in the Servlet you wrote will be called, and then the doGet, doPost and other methods are called according to the content of the request, so you only need to inherit the Servlet and override these methods. accomplish. As for the layering underneath. It has nothing to do with Servlet. You have to understand that the purpose of your layering is to reduce coupling.
If you need help, please send me a private message. Recently, I am imitating Tomcat to build a web server, so I know a lot about this area.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!