Why servlet is used as controller ?not JSP? i want complete Explation?

In struts servlet control the application and we know that a jsp compiled into a servlet first. The only problem with the servlet is that it needs to write out.println call per HTML line. But as a controller in struts servlets do not force the programmers to write out.println, that is the part of JSP which acts as View.

So if the controller would be JSP the translation (JSP compiled to a servlet)step would be of no use and it only affets the performance of the system.

Questions by khalid_it

Showing Answers 1 - 3 of 3 Answers

Satish Bakde

  • Nov 24th, 2006
 

Most of the applications are created in MVC design pattern to seperate the functions of differnet components and handle the complexity of applications. JSP and Servlets are the server side components which can respond to the client request.

Servlets are the basic components for request handling in Web Server. They contains pure Java code. Everything that needs to be done to generate the proper user response (which is genarally HTML code) needs to be done through coding. As a single servlet cannot respond each and every request by itself as client request varies in type (e.g. enquiry, update request, add etc) the request and response object is passed onto different servlets with little task completed by each servlet. This controlling part is easier to write in java. So Servlet is used as controller.

JSP is again a servlet which has syntax more like to HTML with java support. Whatever user is going to see as result of his request is HTML. Programmer can easily write html tags inside JSP to render html. This is very economical to change also. So all this code is written JSP which is (generally) the final link in Servlet chaining.

In simple words, Controller (servlet) has pure Java code while View (JSP) has HTML code.

Satisfied with the above comment. Why we are using struts, we can use JSP. as we know the advantages of Struts, so we use it. So we use Servlet that is for writing logic, as a controller. JSP is for Presentation.

  Was this answer useful?  Yes

One thing you have to know first of all i.e JSP internally is implementd by servlet only.
First servlets are introduced and they feel that it is always difficult to implement this common interface so the developed JSP's. So when you develop JSP internally JSPC (JSP compiler) is going to convert JSP into Servlet class. So JSP itselt is internally a servlet.
So servlet can be used to customise it and used according to our requirment but it is not possible to customise the JSP's.

  Was this answer useful?  Yes

Hanuman

  • Apr 13th, 2012
 

Excellent question

  Was this answer useful?  Yes

Amandeep Bhatia

  • May 22nd, 2012
 

If JSP is used as controller,it will be violated MVC architecture of web based applications.Since as per MVC pattern JSP is for VIEW and servlet is for controller.

  Was this answer useful?  Yes

anil

  • Nov 19th, 2012
 

Servlets are meant to carry out the dynamic work. If we put Servlet to View rather than as controller, it holds so many server resources, unnecessary creation of Request and Response objects that makes the Server to have unnecessary burden.

  Was this answer useful?  Yes

Ajay

  • Sep 3rd, 2014
 

To maintain the control flow huge amount of code is required.Writing huge code in jsp is against jsp technology.
So Jsp is not recommended to use as a controller

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions