W3schools - JSP_Intro
찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요
JSP
JSP(Java Server Pages) is a server-side programming technology that enables platform-independent method for building Web-based app
This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <%
and end with %>
Have access to the entire family of Java APIs, including the JDBC API to access enterprise DB
Is a type of Java servlet that is designed to fulfill the role of a UI for a Java web app
Using JSP, can collect input from users through Webpage forms, present records from a DB or another source
Can be used for a variety of purpose, sush as information from a DB or registering user preferences, accessing JavaBeans components, passing control between pages, and sharing information between requests, pages
Why to learn JSP?
JSP often serve the same purpose as programs implemented using the CGI(Common Gateway Interface)
But JSP offers several advantages in comparison with the CGI
-
Performance is significantly better than JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files
-
JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested
-
JavaServer Pages are built on top of the Java Servlets API, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP
-
JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines
Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications
Environment Setup
Setting up List
- JDK, Web server(Tomcat), CLASS PATH(must identify the servlet classes to the compiler)
Architecture
The web server needs a JSP engine(i.e, a container to process JSP pages)
The JSP container is responsible for intercepting requests for JSP pages
JSP container works with the Web server to provide the runtime environment and other services a JSP needs
JSP processing
-
As with normal page, browser sends and HTTP request to the web server
-
The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine
- This is done by using the URL or JSP page which ends with .jsp instead of .html
- The JSP engine loads the JSP page from disk and converts it into a servlet content
-
The conversion is simple in which all template text is converted to
println()
and all JSP elements are converted to Java code -
This code implements the corresponding dynamic behavior of the page
-
The JSP engine compiles the servlet into a executable class and forwards the original request to a servlet engine
-
A part of the web server called the servlet engine loads the Servlet class and executes it
-
During execution, the servlet produces an output in HTML format
-
The output is further passed on to the web server by the servlet engine inside an HTTP response
-
The web server forwards the HTTP response to your browser in terms of static HTML content
-
Finally, the web browser handles the dynamically-generated HTML page inside the HTTP response exactly as if it were a static page
Lifecycle
Compilation -> Initialization -> Execution -> Cleanup
Compilation
When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page
- If the page has never been compiled, of if the JSP has been modified since it was last compiled, the JSP engine compiles the page
Process
- Parsing the JSP -> Turning the JSP into a servlet -> Compiling the servlet
Initialization
When a container loads a JSP it invokes the jspInit() method before servicing any requests
- If you need to perform JSP-specific initialization, override the jspInit()
Typically, it is performed only once and as with the servlet init method
- You generally initialize DB connections, open files, and create lookup tables in the jspInit method
Execution
Represents all interactions with requests until the JSP is destroyed
Whenever a browser requests JSP and the page has been loaded and initialized
- JSP engine invokes the _jspService() method in the JSP
void _jspService(HttpServletRequest request, HttpServletResponse response){
// Service handling code..
}
_jspService() of a JSP is invoked on request basis
This is responsible for generating the response for that request and this method is also responsible for generating responses to all seven of the HTTP methods, i.e, GET,POST,DELETE
Cleanup
Destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container
JspDestroy() method is the JSP equivalent of the destroy method for servlet
-
Override jspDestroy when you need to perform any cleanup
-
Such as releasing DB connections or closing open files
public void jspDestroy() {
// Your cleanup code goes here
}