1
2
XML tags: JSP action tags and custom tags are written in XML tags. JSP action tags are predefined tags which can be used in place of java code for some specific actions like creating an object of Java Bean, etc. Custom tags are use user defined tags used to reduce Java code in JSP page.
3
4
JSP is really nothing more than a regular HTML page with special tags that allow you to embed server-side function calls and raw Java logic. Jsp page will have an extension of “.jsp�. Jsp pages are preferred if you have both static and dynamic content. If it is only pure dynamic content always prefer Servlet over JSP page. In the above JSP page when client makes a request to the page only the Date will change, remaining every content is same for every user.
5
A JavaServer Pages compiler, or JSP compiler, is a program that parses JavaServer Pages (JSPs), and transforms them into executable Java Servlets. A program of this type is usually embedded into an application server and run automatically the first time a JSP is accessed, but pages may also be precompiled for better performance, or compiled as a part of the build process to test for errors. Most JSP containers support configuring how often the container checks JSP file timestamps to see if the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development, and a longer interval (perhaps minutes, or even never) for a deployed Web application. The Web Container receives the request for the JSP page and first checks to see if the JSP has been referenced before. If not, it needs to pass the JSP through a translation phase. This is the phase in which the text of your JSP page is parsed, and a servlet is generated on behalf of the JSP. Most containers will actually generate a “.java� file and place it in the file system of the server. This java file is compiled and the resulting servlet class, is deployed. In the Request Processing phase (execution phase), the servlet is executed by calling its service method which results in executing the code that was generated on behalf of your JSP. This results in "writing" an HTML document which is then sent to the browser. 6
Life Cycle of JSP is same as that of Servlet, internally JSP is converted to Servlet and deployed. The Life Cycle methods: jspInit() called once when Servlet generated from JSP page is deployed. _jspService() called for every client request. Unlike Servlet in JSP we do not have different methods to handle GET, POST, PUT, etc. We can Override jspInit() and jspDestroy() in JSP page. The method _jspService() is generated, we cannot override this method.
7
JSP Scripting elements allow you to write java code in JSP page.
8
Refer: http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro7.html You can access these implicit objects anywhere in JSP page except inside declarations (discussed later).
9
•Used to define variables or methods that get inserted into the main body of servlet class outside of _jspSevice() method. •Implicit objects are not accessible to declarations. •Use declarations to override jspInit() and jspDestroy().
10
Scriptlets can access all the implicit objects of JSP page. Every Scriptlet written in JSP page gets placed inside _jspService(HttpServletRequest request, HttpServletResponse response) method which is generated by JSP Compiler. Variables declared inside Scriptlets will be local to _jspService().
11
Semi-colons are not allowed for expressions. Expressions will also be placed within generated _jspService() method.
12
13
14
What Is JSP Really? JSP is really just a development methodology. There's nothing you can do with it that you can't do with a Servlet. The main difference is that JSP can help you separate presentation logic from business logic so that, instead of generating HTML from a Servlet, you can keep it in a JSP file.
This can make development much more efficient, especially once you embrace the integration of JSP, HTML, and Java classes.
15
16
1) Client types the URL into the address bar of browser 2) The request goes to the container, and container invokes first resource (servlet/jsp) 3) The Servlet/Jsp decides that the request should go to a completely different URL.The servlet code calls sendRedirect(url) on the response object. 4) The HTTP response sends a URL and status Code 301/303 in the header. 5) The Web Browser (client) sees the status code 300 series and looks for a URL in the header. The Web Browser makes a new request using the URL, (User can notice the change of URL in address bar). 6) The Second resource is invoked 7) Response is generated by the second resource.
17
Example on sendRedirect() refer: http://www.java2s.com/Tutorial/Java/0400__Servlet/ServletResponseSendRedirect.ht m Note: All URLs sent to the HttpServletResponse.sendRedirect method should be run through encodeRedirectURL(String url) , Otherwise, URL rewriting cannot be used with browsers which do not support cookies Example: response.sendRedirect(response.encodeRedirectURL(destination)); Refer JEE Documentation for difference between encodeURL() and encodeRedirectURL(): http://download.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.ht ml.
18
•The forward method should be used to give another resource responsibility for replying to the user. If you have already accessed a ServletOutputStream or PrintWriter object within the servlet, you cannot use this method; it throws an IllegalStateException.
19
•RequestDispatcher’s forward method is used for Server Side Redirection. •Since the redirection happens with the Web Container, the client( Web Browser) will not be aware from where the response is generated (URL in address bar does not change).
• Since the forward method passes both request and response objects to the next resource in the chain, programmer can carry additional attributes in the request object. •The method of REQUEST does not change. i.e., from doGet() if forward is done it invokes doGet() of second servlet, similarly from doPost() if forward is done it invokes doPost() of Second Servlet. •Only the Last resource in the chain can send the response to the client
20
Server-Side redirection from JSP page can be done using JSP action tag as shown below.
<jsp:forward page=“url of Second Resource” /> Note: you can also use RequestDispatcher in jsp scriptlets (avoid, minimize usage of Java in JSP ) Client-Side redirection code is same as that used in servlet. For JSP include and forward tag examples refer: 1) http://www.exforsys.com/tutorials/jsp/jsp-action-tags.html 2) http://www.easywayserver.com/jsp/JSP-action-tag.htm
21
If any response is sent after calling forward() method of RequestDispatcher, it throws IllegalStateException. In JSP page Server – Side redirection is done using jsp action tags: <jsp:forward page=“home.jsp” />
22
Understand the package structure created for OnlineShopping application. Notice that the database drivers are placed in WEB-INF/lib folder. Any third party libraries should be place here. Understand the concept of funneling of exceptions
23
24
Answer: 1) a
25
Answer: 2) C ( Implicit objects of JSP are not available in JSP Declerations)
26
Answer: 3) b and d.
27
Answer: 4) a) Client-side redirection ( Should not carry any information in request) b) Client-side redirection ( RequestDispather cannot be used to redirect outside of Servlet engine ( Web container)) c) Server-side redirection(store data into request attribute and use the attribute value in JSP page)
28
Note: Can use this as hands on assignment in class room.
29
30
31
32
33
34
35