JSP combine markup (whether HTML or XML) with nuggets of Java code to produce a dynamic web page. Each page is automatically compiled to a servlet by the JSP engine, the first time it is requested, and then executed. JSP provides a variety of ways to talk to Java classes, servlets, applets and the web server. With it, you can split the functionality of your web applications into components with well-defined public interfaces glued together by a simple page. This model allows tasks to be sub-divided ? a developer builds custom components and the page designer assembles the application with a few judicious method calls. In this ?application assembly? model, the business logic is separated from the presentation of data. Finally, it?s nice and simple In this chapter, we will : Discuss the JavaServer Pages (JSP) architecture Look at the elements of a JSP file, and the tags used to represent them Encapsulate logic in a JavaBean component and integrate it with JSP Walk through a detailed example using JSP, showing a typical web application architecture.
A JavaServer Page is a simple text file consisting of HTML or XML content along with JSP elements (a sort of shorthand for Java code). When a client requests a JSP page of the web server and it has not been run before, the page is first passed to a JSP engine which compiles the page to a servlet, runs it and returns the resulting content to the client. Thereafter, the web server?s servlet engine will run the compiled page. It should be no surprise, given the flexibility of the servlet model, that the current reference implementation of the JSP engine is itself a servlet. It is possible to view the finished servlet code that is generated by locating it within the directory structure of the servlet engine. For example, with JRun, you can find the source code for your JSP files (in servlet form) in the jrun/jsm-default/services/jse/servlets/jsp directory. This is very helpful when trying to debug your JSP files. If you take a look in the source files for the javax.servlet.jsp package, you?ll find the following classes : JSPPage HttpJspPage They define the interface for the compiled JSP page ? namely that it must have three methods. Not surprisingly they are : jspInit() jspDestroy() _jspService(HttpServletRequest request, HttpServletResponse response)
Declaratives ? these are for page-wide variable and method declarations. Scriptlets ? the Java code embedded in the page. Expressions ? formats the expression as a string for inclusion in the output of the page.
Servlet container provides many built in functionality to ease the development of the applications. Programmers can use these functions in JSP applications. The JSP Actions tags enables the programmer to use these functions. The JSP Actions are XML tags that can be used in the JSP page.
Here is the list of JSP Actions:
The jsp:include action work as a subroutine, the Java servlet temporarily passes the request and response to the specified JSP/Servlet. Control is then returned back to the current JSP page.
The jsp:param action is used to add the specific parameter to current request. The jsp:param tag can be used inside a jsp:include, jsp:forward or jsp:params block.
The jsp:forward tag is used to hand off the request and response to another JSP or servlet. In this case the request never return to the calling JSP page.
In older versions of Netscape Navigator and Internet Explorer; different tags is used to embed applet. The jsp:plugin tag actually generates the appropriate HTML code the embed the Applets correctly.
The jsp:fallback tag is used to specify the message to be shown on the browser if applets is not supported by browser.
Unable to load applet
Unable to load applet
This JSP Tutorial shows you how to track the session between different JSP pages. In any web application user moves from one page to another and it becomes necessary to track the user data and objects throughout the application. JSP provide an implicit object "session", which can be use to save the data specific the particular to the user.
In JSP cookie are the object of the class javax.servlet.http.Cookie. This class is used to creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookies value can uniquely identify a client, so cookies are commonly used for session management. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. The getCookies() method of the request object returns an array of Cookie objects. Cookies can be constructed using the following code: Cookie(java.lang.String name, java.lang.String value)
When we are creating a Java web application, we are using some Java specific technologies, more specifically servlets and jsp pages. Jsp pages could be seen as normal html pages which however, can interact with Java backend code, as well as contain some Java code within them, which are called scriptlets. Implicit objects are 9 specific objects that exist in every jsp page by default and provide multiple functionality depending on the object. Let?s take a look at what are these objects and what they can be used for: out: It is the implementation of JspWriter class and it is used as a convenient and easy output to the jsp client page. It is more or less standard in its usage, and that?s why it is heavily used in the backend as well (servlet) in order to output information after the execution of some method. page: It is a simple instance of Object, and essentially represents the current jsp page that the client is using. request: It is an instance of HttpServletRequest and represents a request (HTTP), from the client to the server. Each time that a new request is made, a new instance is created representing that exact request. It is used heavily for important information like headers, cookies, etc. response: In addition to request, we are also using the response object, an instance of HttpServletResponse, and represents the response of the server to the client. exception: it is an instance of Exception, the Java class used for throwing exceptions when something goes wrong with the code. It is rarely used, although in some cases jsp an use it for error pages. config: It represents the ServletConfig class and it is used to access information about the servlet and the initialization of the jsp page. pageContext: As an instance of the PageContext class, it is mainly used to access information about the page, as well as to work with attributes of different scopes and pages. session: Implementation of the HttpSession class, and it represents the current session of using the jsp page. It represents the scope of this session, and it is useful in order to keep attributes and values and providing them in different jsp pages of the same application. application: Instance of ServletContext, and mainly used to keep objects and values in order to access them in different jsp pages.
We can configure error pages in jsp by using the following two approaches. 1) Declarative approach 2) Programmatic approach
In servlets we have the following 3 scopes for storing information in the form of attributes. request scope session scope application scope In addition to these 3 scopes in the jsp?s we have page scope also. Request scope In servlet we can maintain this scope by using ServletRequest object,but in the jsp?s we can maintain it by using request implicit object. The information stored in request scope is by default available for all the components which are processing that request. Request scope will start at the time of request object creation (i.e just before starting service ()) and ends at the time of request object destruction(i.e just after completing service()). We can perform attribute management in request scope by using the following methods of servlet request interface. public void setAttribute (string name, object value) public object getAttribute(string name) public void removeAttribute(string name) public enumeration getAttributenames() session scope In servlet we can maintain session scope by using HttpSession object but in jsp?s we can maintain by using session implicit object. The information stored in session scope is by default available for all the components which are participating in that session. Session scope will be started at the time of session obj is creation and ends once session expires. By using the following methods of Http session interface we can perform attribute management in session scope. setAttribute(string name, object value) getAttribute(string name) removeAttribute(string name) getAttributeNames() Application scope We can maintain application scope in servlet by using servlet context object but in jsp?s we can maintain by using application implicit object. The information stored in the application scope is by default available for entire web application. Application scope will start at the time of creation (i.e at the time application deployment and ends at the time of context object destruction(i.e at the time of application undeployment) Servlet context interface defines the following methods to perform attribute management in application scope setAttribute(string name, object value) getAttribut(string name) removeAttribute(string name) getAttribute names() Page scope This scope is application only for jsp?s but not for servlets. We can maintain this scope by using pagecontext implicit object. The information stored in page scope is by default available within the ?translation unit? page scope is most commonly used scope in customer tags to share into between tag handle classes. pagecontext class defines the following methods to perform attribute management in page scope. public void setAttribute (string name,object value). public Object getAttribute(string name) public void removeAttribute(string name).
we can implement this method to define initialization activities. jspInit() method will be executed only once. Web Container always calls init(servletConfig) method present in HttpJspBase class,Which intern calls jspInit() method. If we have any initialization activities then we can override jspInit() method in the jsp.
This method will be executed only once to perform clean-up activities just before taking jsp from out of service. Web Container always calls destroy() method available in HttpJspBase class which intern calls jspDestory() method. We can override jspDestroy() method but we can?t override destroy() method in jsp ,because it is declared as the final in HttpJspBase class
Web container always generates this method at the time of translation phase and we are not responsible to write this method explicitly. If we are trying to write this method explicitly in the jsp the generated servlet class will contain 2 _jspService() methods which causes a compile time error.(A class cann not have 2 methods with the same signature otherwise compiletime error.)Web container always calls service() method of HttpJspBase which intern calls _jspService() method.In the jsp we can?t write service() method directly,because it is declared as final in base class.
Simple and dept learning technology is very important part in software development. Same story has been implemented at this site, so learn and implement the inovative things, Thanks
Ravi Kumar Verma