About the separation of servlet and service, I was confused when writing small project exercises.

in order to decouple, I learned that servlet is required to be separated from Service (jsp/servlet presentation layer; service business logic layer; Dao persistence layer), but I feel very uncomfortable when writing practical exercises for small projects, such as:
users should inject successful login msg, into the request domain after they have successfully logged in.

request.setAttribute("msg","");

because there will be

in jsp
${requestScope.msg}

but shouldn"t this be part of business logic? And the Service layer should not get request this thing, can only be given to servlet to complete it?
if this ultimately affects the jsp layer, that is, the presentation layer, so you can leave it to servlet to handle, okay. Well, now I am in a situation where members of the website have different permissions. After logging in successfully, I want to inject the user"s permission information into session

request.getSession.setAttribute("authority","admin");

this should be a matter for the business logic layer, isn"t it? But theoretically, if the Service layer cannot accept request objects, it should not be able to accomplish this task.
and if you leave it to Servlet, the login method returns a User object, and then gets the User object in Servlet, gets its authority attribute value, and injects it into session. No matter how I think about it, I think it"s business logic! How can you let Servlet handle it!
has a big head. I hope there are seniors who can answer it. Thank you!


the purpose of code layering is to make your logic clearer, not forced fragmentation, which is a way of code organization.

in practice, the most complex business to be handled by the service layer is to deal with input and output, which must involve obtaining and outputting data through request,response.

if you look more independent in the service layer, you can encapsulate the capabilities of request and response as input and output interfaces for the current context, instead of directly manipulating them

.

servlet is a native means provided by a programming language, not a code organization. It is inappropriate for you to compare code layering with servlet. Servlet provides a very convenient package for java's web development (otherwise you will have to implement the http protocol yourself). If you read and understand the mainstream framework, spring mvc and so on, you are not breaking away from servlet, but encapsulating the capabilities of request and response and of course some CI things.


has been watching you emphasize business logic. How convenient you are and how to do it is the most important. There is no need to strictly abide by those specifications.

Menu