JSP (JAVA Server Pages):
JSP technology is used to create web application. It focuses more on presentation logic of the web application.JSP pages are easier to maintain then a Servlet. JSP pages are opposite of Servlets. Servlet adds HTML code inside Java code while JSP adds Java code inside HTML. Everything a Servlet can do, a JSP page can also do it.
JSP enables us to write HTML pages containing tags that run powerful Java programs. JSP separates presentation and business logic as Web designer can design and update JSP pages without learning the Java language and Java Developer can also write code without concerning the web design.
How JSP becomes servlet :
JSP pages are converted into Servlet by the Web Container. The Container translates a JSP page into servletclass source(.java) file and then compiles into a Java Servlet class.
Benefits of JSP:
· Easy to maintain
· High Performance and Scalability.
· JSP is built on Java technology, so it is platform independent.
Lifecycle of JSP:
A JSP page is converted into Servlet in order to service requests. The translation of a JSP page to a Servlet is called Lifecycle of JSP. The methods used in lifecycle of JSP are same as that of servlet.
The only difference is that the jsp methods are prefixed with the keyword jsp.JSP Lifecycle consists of following steps.
The only difference is that the jsp methods are prefixed with the keyword jsp.JSP Lifecycle consists of following steps.
· Translation of JSP to Servlet code.
· Compilation of Servlet to bytecode.
· Loading Servlet class.
· Creating servlet instance.
· Initialization by calling jspInit() method
· Request Processing by calling jspService() method
· Destroying by calling jspDestroy() method
Web Container translates JSP code into a servlet class source(.java) file, then compiles that into a java servlet class. In the third step, the servlet class bytecode is loaded using classloader. The Container then creates an instance of that servlet class.
The initialized servlet can now service request. For each request the Web Container call the jspService()method. When the Container removes the servlet instance from service, it calls the jspDestroy() method to perform any required clean up.
Translating JSP into Servlet:
consider the following Jsp Program:
The above jsp program gets converted into servlet as given below:
public class hello_jsp extends HttpServlet
{
public void jspService(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
{
PrintWriter out=res.getWriter();
response.setContentType(“text/html”);
out.write(“<html><body>”);
String str=”welcome”;
out.write(str);
out.write(“</body></html>”);
}
}
|
No comments:
Post a Comment