developer
developer

Reputation: 9478

Can jsp_service() method be overridden?

I know that the jsp_service() method cannot be overridden, but one of my friends said that we could use scriptlets in JSP to override the method and could do whatever we need in that method.

Can anyone explain it's true?

Upvotes: 4

Views: 10541

Answers (2)

Hardik Mishra
Hardik Mishra

Reputation: 14887

Not Its not true.

Take a sample JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

If you check generated Servlet for this JSP. You will find _jspService method containing HTML code as out.write

Why ?

Since what ever we wrote code in the JSP will be placed in _jspService() of generated servlet class(from JSP) .means _jspService() is already imlimented by us.So if we attempted to override _jspService() it will give a compilation error regarding the method _jspService() is already defined.

Read More why jspService() cannot be overridden?

Upvotes: 9

Sumit Desai
Sumit Desai

Reputation: 1770

Whatever we write inside the scriplet gets converted into jsp_service() method into the generated Servlet. So, to override this method, you can simply write the code in scriplet. But, you can not manually override this method. It will result in compilation failure.

Upvotes: 1

Related Questions