jsp basics

Download jsp Basics

Post on 27-Oct-2014

120 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

JSP Jsp is a web technology function which is used to develop server side web components. Jsp will be used in presentation layer. Complete jsp life cycle will be taken care by web container. Jsp page is combination of HTML elements and JSP elements.

Following is the list of JSP elements: ---------1. Scripting elements Scriplets Expressions Declarations 2. Directives Page directives Include directives taglib directives 3. Standard Action 4. Custom elements(Tags)

Information About JSP by JLC www.jlcindia.commrajmit81@gmail.com

Scripting ElementsSyntax: --------- Scriplets is used to write any valid java statement. All the elements inside the Scriplets must be terminated by semicolon. Example: ------- All the statements inside the Scriplets will be placed inside the service() method of translated servlet.

Expression

Syntax: ----------------- Exp:---- X(invalid) Expression is a short-cut form of out.println. All the expression inside the JSPs will be placed inside the service() method of translated

servlet .

DeclarationSyntax:---------- It is used to write variable declaration and method definition. Java statement are not allowed inside the declaration all the declaration inside the JSP will be placed directly inside the translated servlet class and outside the service() method. Exp: --------- Hello.jsp Hello-jsp.java Package org.apache.jsp; Import javax.servlet.*; Import javax.servlet.http.*; Import javax.servlet.jsp.*; Hello.jsp Public final class Hello-Jsp extends I am the Scriplets HttpJspBase I am the expression I am the declaration

JSP By Mukesh

Void m1() { System.out.println(x); System.out.println(m1); } Public void -JSPService(HttpServletRequest req,HttpServletResponse res)throws java.io.IO Exception ,ServletException{ 1) Page context page context=null; 2) HttpSession session=null; 3) Servlet context application= null; 4) Servlet config config=null; 5) JspWriter out=null; 6) Object page=this; Response.setContentType(text/html); PageContext= ---------Application=pageContext.getSession(); Out=pageContext.getOut(); out.write(\r\n \r\n I am the Scriplets\r\n); Int a=10; out.println(Hello-----------------!); out.println(a); out.println(x); m1(); out.writer(\r\n I am expression\r\n); out.print(hai----------); out.write(\r\n I am the declaration \r\n); out.write(\n); } }

DirectivesPage directive

Language

This attribute is used to specify the language whose statements are allow inside the

Scriplets. Currently only valid value for this attribute is java . if you give other value for this you can get the error called invalid language attribute.

Extends When you developing servlets we are taking HttpServlet as super class as

Class HelloServlet extend HttpServlet { --------------------- ---------} When container is generating the servlet container take HttpJSPBase as supper class Class test-Jsp extends HttpJSPBase { -------- -----------} If you want to change the supper class what container is taking by default when it is generating the servlet you can use extends attributes when you are changing the supper class from HttpJSPBase to HttpServlet then you need to override any one of the methods available in HttpServlet.If I want to implement something inside the init and destroy method of the servlet. what I have to do? In Jsp you have to implement JspInit() and JspDestroy() method in the declaration section Example :--------- Having two methods inside the class with the same name and same method signature is invalid. What is the Jsp life cycle method? JspInit() _JspService() JspDestroy() Why there is _ for the JspService method? It show that override method is not possible .

JSPInit() & JSPDestroy()

Import

Import attribute is used to specify the package which you want to import for the translated servlet class. you can specify the multiple package with the comma ,separation.

Session attributePossible values for this attributes are true/false default value is true. If you specify the session value as false then session implicit object will be disabled. Example: ------ Because of the above statement you are unable to use session implicit object inside the JSP.

IsThreadSafeContainer follows two models for the servlet: ---------- Multi Thread model Single Thread Model In multi Thread Model only one instance will be created for the servlet and multiple

thread will be created to serve multiple request. In single thread model multiple instances will be created for the one servlet to provide the service to multiple request multi thread model is better than single thread model. The default model what container is following is multithread model. When you writing the servlet by extending HttpServlet as supper class to follow the single thread model to servlet class must implement single thread model marker interface which is available in javax.servlet package. Class HelloServlet extends HttpServlet implements single Thread Model { -------------- ----------- -------------} In the case of Jsp . if you want to follow single thread model you have to specify the page directive as follows:--

errorPage

or

IsErrorPage

When you are writing the java code inside the Jsps. you may get exception inside that to handle the exception you need to write try, catch blocks many times in all the Jsps .to avoid the try ,catch block and to centralize the exception handling use errorPage or IsErrorPage attribute inside the JSPs where you want to handle all the exceptions. Enable the exception implicit objects by writing the following: ----------- Test.jsp hello guys errors.jsp I am handling exceptions

Include directive It is used to include one Jsp or Html to another JSP.

Syntax: -----

Taglib directiveit is used to use the custom tags developed by you in JSP & also used to use JSTL tags inside the JSP. JSP standard Actions Include tag is used to include one jsp or html inside other jsp. Syntax:----

Example Include tag functionality is similar to include() method of request Dispatcher. Forward tags used to forward the request from one jsp to another jsp or html. In this same request, response from one JSP is given as request, response to other JSP. Forward tag functionality is similar to forward() method of requestDispatcher. Param tag Param tag is used to pass the parameters when you are forwarding the request from one jsp

to another JSP or when you including one JSP to another JSP. Param tag must be used always along with forward and include. Exp:------------- For include x.jsp Header.jsp

For forward x.Jsp

y.jsp

header.Jsp,

Student.java package com.jlc; public class Student { private String sid; private String sname; private String email;

x.jspStudent.java

private String phone; public void setSid(String sid) { this.sid=sid; } public void setSname(String sname) { this.sname=sname; } public void setEmail(String email) { this.email=email; } public void setPhone(String phone) { this.phone=phone; } public String getSid() This is 1st { JSP return sid; program } public String getSname() JLC { return sname; RAMANSHYAM } public String getEmail() 01-jun-2008 { return email; } public String getPhone() { return phone; } } X1.Jsp this jsp is adding Student object to session
open y1.jsp and check the data..

this jsp is adding Student object to session
open y.jsp and check the data.....

y.jsp Sid:
Sname
Email:
Phone:

y1.Jsp Sid:
Sname:
Email:
Phone:

Steps to develop custom tags:--------- Write one TLD file (with extension *.tld)(tag library descriptor) contains description about your tag Write one tag handler java class Write the tag inside the web.xml. After developing the tag use that tag in any jsp by writing one taglib directive inside the Jsp. x.Jsp // wrong according to our requirement Here sd Hello Name Sri o/p of x.jsp hello,sri welcome to JLC your email is sri@sd.com hello,vas welcome to jlc your email is not specified web.xml WEB-INF/jlc.tld WEB-INF/jlc.tld Jlc.tld 1.0 1.1 /WEB-INF/jlc.tld hello com.jlc.HelloTagHandler empty name //prefix // tag name // attribute name // attribute value

Custom tag development

true true email false HelloTagHandler.java package com.jlc; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class HelloTagHandler extends TagSupport { HelloTagHandler.java String name; String email; public void setName(String name) { this.name=name; System.out.println(name); } public void setEmail(String email) { this.email=email; System.out.println(email); } Tracing First it will take the prefix(minatory ) and compare this prefix with prefix written in

public int doEndTag() throws JspException { try { String msg="Hello"+" "+name+" "+"welcome to jlc"; if(email!=null||email.length()!=0) { msg=msg+" "+"email is "+email; } else { msg=msg+" "+"email is not specified"; } JspWriter out=pageContext.getOut(); out.write(""+msg+""); }catch(Exception e) { System.out.println(e); } return EVAL_PAGE; } }

If it ok then through this prefix it takes the uri from Then it compare this uri and uri given in web.xml If they are same then it takes the name of the tld file (location) and checks whether this type of file is present or not. If yes then it load the tld file and create instances of this class. name should be same as x.jsp file if we wouldnt (as in x.jsp) that attribute name=name is mandatory in our custom tag then in jlc.tld write like name

Then it checks the tag name which should be same as x.jsp file and also attribute

true If any attribute name is not

Recommended

View more >