Thursday 8 March 2012

What is jsp


JavaServer Pages (JSP) is a server-side technology that allows developers to create Web-based applications that use Java components.
JavaServer Pages (JSP) technology is technology for delivering dynamic content to web clients in a secure and well-defined way.
Servlets are powerful and sometimes they are a bit cumbersome when it comes to generating complex HTML. Most servlets contain a little code that handles application logic and a lot more code that handles output formatting. . For these reasons, web application developers turn towards JSP as their preferred servlet environment
The JavaServer Pages specification extends the Java Servlet API to provide web application developers with a robust framework for creating dynamic web content on the server using HTML, and XML templates, and Java code A JSP page is a text based document containing static HTML and dynamic actions which describe how to process a response to the client in a more powerful and flexible manner. Most of a JSP file is plain HTML but it also has, interspersed with it, special JSP tags.

Documents the JSP file, but is not included in the response                                
<%-- comment --%>
No equivalent.
Declares variables or methods valid in the page’s scripting language.
<%! declaration; [ declaration; ]+ ... %>               
<jsp:declaration>
declaration [ declaration; ]+ </jsp:declaration
Contains an expression valid in the page’s scripting language.
<%= expression %> 
<jsp:expression>
expression
</jsp:expression>
Contains a code fragment valid in
the page’s scripting language.. 
<% code fragment %>             
<jsp:expression>
code fragment
</jsp:expression>
To process a JSP file, we need a JSP engine that can be connected with a web server Firstly when a web browser seeks a JSP file through an URL from the web server, the web server recognizes the .jsp file extension in the URL requested by the browser and understands that the requested resource is a JavaServer Page. Then the web server passes the request to the JSP engine. The JSP page is then translated into a Java class, which is then compiled into a servlet.
This translation and compilation phase occurs only when the JSP file is requested for the first time, or if it undergoes any changes to the extent of getting retranslated and recompiled. For each additional request of the JSP page thereafter, the request directly goes to the servlet byte code, which is already in memory. Thus when a request comes for a servlet, an init() method is called when the Servlet is first loaded into the virtual machine, to perform any global initialization that every request of the servlet will need. Then the individual requests are sent to a service() method, where the response is put together. The servlet creates a new thread to run service() method for each request. The request from the browser is converted into a Java object of type HttpServletRequest, which is passed to the Servlet along with an HttpServletResponse object that is used to send the response back to the browser. The servlet code performs the operations specified by the JSP elements in the .jsp file.
Example of a jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
      <form action="abc">
<%    int a=2;
      int b=3;
            if(a<3){
            out.print(a);
            }
            else{
                  out.print(b);
            }
%>
      </form>
</body>
</html>

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...