The relationship between Servlet and JSP

Source: Internet
Author: User

A servlet is a component that Java provides to develop a Web server application that runs on the server side and is managed by the servlet container for generating dynamic content. Servlets are platform-independent Java classes that write a servlet that actually writes a Java class according to the servlet specification.

, Java provides a series of interface classes (the so-called interface class is that all methods in a class only provide method declarations, and do not provide any method implementations, which are left to the successors to do.) ): Servlet, ServletConfig, Serializable, and then through multiple inheritance produces one of the most common servlet implementation classes (the Gerneric servlet class in the diagram), and next, through a multiple inheritance and implementation, To generate a new implementation class HttpServlet, the user only needs to inherit this class when developing the servlet program, thus producing a class of its own (the Hello_servlet class in the diagram), and then implementing the relevant methods in the class according to the actual development function and information processing needs. This is what was mentioned earlier by writing a Java class according to the servlet specification, thus writing a servlet.

As for the JSP (Javaservlet Page), it can be seen that it is actually inherited from the servlet. It's just that it adds/modifies some methods in the servlet and makes a new package. Specifically to the Tomcat Web application server, it inherits and implements several methods from Java's Httpjsppage and httpservlet two classes through a multiple inheritance, Then encapsulate a class called httpjspbase to implement a generalized JSP class, users in the development of their own JSP, only need to inherit a class from Httpjspbase (middle hello_jsp Class), and then according to the need to implement the corresponding method.

So this is why the code of the JSP always flashes the servlet code frame shadow, in fact, they are only to achieve the same function and different packages of components only, the blood is left in the same vein.

"Both Born and Sheng?" "Oh, because JSP is really better than the servlet, so-called" 've seen than Blue ", since the sun company to launch the servlet on the basis of JSP technology, it must be because JSP has its more clever place.

Using a servlet to generate a Dynamic Web page requires you to print out a lot of HTML tags in your code, and in the servlet we have to mix the static reality content with the code that dynamically produces the content. Using Servlets to develop dynamic Web pages, programmers and web editors will not be able to work together because web editors do not understand the Java language and cannot modify the servlet code, and Java programmers may not be familiar with the intent of web editors to modify and implement Web page functionality. To address these issues, Sun has introduced JSP technology.

The JSP is the extension of the servlet, and the servlet technology has appeared before the JSP. A servlet is the use of an output stream to dynamically generate HTML pages, including each HTML tag and the content that appears in each HTML page.

JSP uses Java Script control to insert Java code into a standard HTML page, and its static parts do not have to be programmed by Java, only those that need to read from the database and dynamically generate information based on the program.

In fact, JSP is a special form of servlet, each JSP page is a servlet instance--jsp page compiled by the system into Servlet,servlet is responsible for responding to user requests. JSP is also a simplification of the servlet, when using JSP, the servlet is actually used, because each JSP page in the Web application will be generated by the servlet container corresponding servlet. For Tomcat, the servlet generated by the JSP page is placed under the Web application corresponding to the work path.

Taking apache-tomcat-7.0.37\webapps\myapp\index.jsp as an example,

[HTML]View PlainCopy
    1. <html>
    2. <body>
    3. <center>
    4. Now time is: <%=new java.util.Date ()%>
    5. </Center>
    6. </body>
    7. </html>

When Tomcat is started, the apache-tomcat-7.0.37\work\catalina\localhost\myapp\org\apache\ in Tomcat can be The following files are found in the JSP directory: Indexd.java and Index.class. All two files are generated by Tomcat, andTomcat generates Java files and class files corresponding to the servlet based on the JSP page .

Index.java

[HTML]View PlainCopy
  1. JSP page after Tomcat compiles the default package
  2. Package org.apache.jsp;
  3. Import javax.servlet.*;
  4. Import javax.servlet.http.*;
  5. Import javax.servlet.jsp.*;
  6. Inherits the Httpjspbase class, which is actually a subclass of a servlet
  7. Public final class Index_jsp extends Org.apache.jasper.runtime.HttpJspBase
  8. Implements Org.apache.jasper.runtime.JspSourceDependent {
  9. private static final javax.servlet.jsp.JspFactory _jspxfactory =
  10. javax.servlet.jsp.JspFactory.getDefaultFactory ();
  11. private static Java.util.Map<java.lang.string,java.lang.long> _jspx_dependants;
  12. Private Javax.el.ExpressionFactory _el_expressionfactory;
  13. Private Org.apache.tomcat.InstanceManager _jsp_instancemanager;
  14. Public Java.util.Map<java.lang.string,java.lang.long> Getdependants () {
  15. return _jspx_dependants;
  16. }
  17. public void _jspinit () {
  18. _el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). Getservletcontext ()).  Getexpressionfactory ();
  19. _jsp_instancemanager = Org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager (  Getservletconfig ());
  20. }
  21. public void _jspdestroy () {
  22. }
  23. Methods for responding to users
  24. public void _jspservice (final javax.servlet.http.HttpServletRequest request, final Javax.servlet.http.HttpServletResponse response)
  25. Throws Java.io.IOException, Javax.servlet.ServletException {
  26. Final Javax.servlet.jsp.PageContext PageContext;
  27. Javax.servlet.http.HttpSession session = null;
  28. Final Javax.servlet.ServletContext Application;
  29. Final Javax.servlet.ServletConfig config;
  30. Get the page output stream
  31. Javax.servlet.jsp.JspWriter out = null;
  32. Final Java.lang.Object page = this ;
  33. Javax.servlet.jsp.JspWriter _jspx_out = null;
  34. Javax.servlet.jsp.PageContext _jspx_page_context = null;
  35. Start Build response
  36. try {
  37. Set the page format for the output
  38. Response.setcontenttype ("text/html");
  39. PageContext = _jspxfactory.getpagecontext (this, request, response,
  40. NULL, True, 8192, true);
  41. _jspx_page_context = PageContext;
  42. application = pagecontext.getservletcontext ();
  43. config = pagecontext.getservletconfig ();
  44. session = pagecontext.getsession ();
  45. Page output stream
  46. out = pagecontext.getout ();
  47. _jspx_out = out ;
  48. Output stream, starting output page document
  49. Out.write ("<HTML>\r\n");
  50. Out.write ("<body>\r\n");
  51. Out.write ("<center> \ r \ n");
  52. Out.write ("Now Time is:");
  53. Out.print (New Java.util.Date ());
  54. Out.write ("\ r \ n");
  55. Out.write ("</Center>\r\n");
  56. Out.write ("</body>\r\n");
  57. Out.write ("</html>");
  58. } catch (Java.lang.Throwable t) {
  59. if (! ( T instanceof Javax.servlet.jsp.SkipPageException)) {
  60. Out = _jspx_out;
  61. if (out! = null && out.getbuffersize ()! = 0)
  62. try {out.clearbuffer ();} catch (Java.io.IOException e) {}
  63. if (_jspx_page_context! = null) _jspx_page_context.handlepageexception (t);
  64. else throw new servletexception (t);
  65. }
  66. } finally {
  67. _jspxfactory.releasepagecontext (_jspx_page_context);
  68. }
  69. }
  70. }

Several objects are built into the JSP page, such as PageContext, Application, config, page, session, out, and so on _jspservice () methods, which are defined here.

Based on the above JSP page working schematic, you can get the following conclusion: JSP files must be run within the JSP server. The JSP file must generate a servlet to execute. The first visitor for each JSP page is slow because it must wait for the JSP to compile into a servlet. A JSP page visitor does not need to install any client, or even run the Java environment, because the JSP page is delivered to the client's standard HTML page. Each character in the Index.jsp page is generated by the output stream of the Index.java file.
A servlet is a Java program on a Web server that provides services that are passed to you in HTML format. The Servlet API provides a unified programming interface for Servlets
The servlet must be deployed in a servlet container in order to respond to client requests for service. To the external unified interface, called by the container.
JSP focuses on display; servlet focuses on control logic.
MVC pattern: Jsp + Servlet + JavaBean. M-javabean v-jsp C-servlet
Small applications (applets) are HTML-based programs created with Java. The browser temporarily downloads it to the user's hard disk and runs locally when the Web page opens. They can be embedded directly into a Web page or other specific container and can produce special effects.

Full version:

http://blog.csdn.net/kaixinbingju/article/details/9409927

The relationship between Servlet and JSP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.