1 / 29

Using JSP Directive

Using JSP Directive. Syntax. Syntax <%@ include file=“name of file” %> Ex: <%@ include file=“a.html” %> The file a.html will be displayed inside jsp file. Example. Create a jsp file and save it as crazy.jsp <html> <body> <%@ include file=“header.html" %> <hr>

eddy
Télécharger la présentation

Using JSP Directive

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Using JSP Directive

  2. Syntax Syntax <%@ include file=“name of file” %> Ex: <%@ include file=“a.html” %> The file a.html will be displayed inside jsp file.

  3. Example Create a jsp file and save it as crazy.jsp <html> <body> <%@ include file=“header.html" %> <hr> <p> Welcome to <i>It's a Crazy World</i> <p> </body> </html> ----------------------------------------------------------------//header.html This is header.html

  4. Example Create a jsp file and save it as crazy.jsp <html> <body> <%@ include file=“header.html" %> <hr> <p> Welcome to <i>It's a Crazy World</i> <p> </body> </html> ----------------------------------------------------------------//header.html This is header.html

  5. More about include directive <%@ include file=“header.html" %> The file “header.html” was included at compiled time. Can we include an jsp file using include directive? No Why? Bcos, to view jsp file, it needs to be compiled. So, how can we include jsp file?

  6. Use jsp:include directive To include jsp files, use <jsp:include page=“filename.jsp” /> The file filename.jsp will be included at run time. Step1: compile filename.jsp Step2. include filename.jsp

  7. a.jsp <% int i = 10; %> The value of i is <%=i%> test.jsp <jsp:include page = "a.jsp" /> This file includes jsp file a.jsp Example

  8. Using jsp:param jsp:param is used with jsp:include param means parameter. jsp:param is used to pass parameters For ex: <jsp:include page = “sum.jsp" /> I want to pass 2 parameters to sum.jsp i=10 j=20 sum.jsp will calculate sum of i and j.

  9. sum.jsp <% int i = request.getParameter(i); int j = request.getParameter(j); int sum = i + j out.println(sum); %> test.jsp <jsp:include page = “sum.jsp" > <jsp:param name="i" value="10" /> <jsp:param name="j" value="20" /> </jsp:include> This file includes jsp file sum.jsp Example

  10. Using jsp:forward Jsp:forward is used to load another page. Or For ex: User requested a.jsp but request is transferred to b.jsp Syntax <jsp:forward page=“name of jsp file”>

  11. a.jsp <% int i = 10; if (i==20) { %> <jsp:forward page="b.jsp"/> <% } else out.println("hello"); %> Output: “hello” Example

  12. a.jsp <% int i = 10; if (i==10) { %> <jsp:forward page="b.jsp"/> <% } else out.println("hello"); %> b.jsp <% out.println(“I am a forwarded page”); %> Output: “I am a forwarded page” Example

  13. More about jsp:forward If you need, you can pass parameter to forwarded page i.e. b.jsp Ex: <jsp:forward page=“b.jsp”> <jsp:param name=“i” value=“10” /> </jsp:forward>

  14. Using an applet in JSP To include an applet in JSP, we use <jsp:plugin> To include a flash movie in JSP, we use <jsp:plugin>

  15. Syntax Syntax: <jsp:plugin type=“applet” code=“name of applet” codebase=“.” width=“” value=“”> <jsp:fallback> unable to find java applet </jsp:fallback> </jsp:plugin> <jsp:fallback> is executed if applet is not found.

  16. How to pass parameters Syntax: <jsp:plugin type=“applet” code=“name of applet” codebase=“.” width=“” value=“”> <jsp:fallback> unable to find java applet </jsp:fallback> <jsp:params> <jsp:param name=“i” value=“10”> <jsp:param name=“j” value=“50”> </jsp:params> </jsp:plugin>

  17. Scope of a variable a.jsp <% int i = 10; // variable i has a page scope. // it is only valid inside file a.jsp out.println(i); %> b.jsp <% out.println(i); //this will give an error, %>

  18. Scope of a variable If I want variable ‘i’ accessible outside a.jsp, there are 3 solutions • Use hidden variable. • Set the variable ‘i’ in session scope. • Set the variable ‘i’ in application scope.

  19. How to pass data from a.jsp to b.jsp We can pass data from a.jsp to b.jsp using form parameters. We can use variable ‘i’ as form parameter and pass it from a.html to b.html To understand this, we will take an example

  20. LoginForm.html <body> <form method="post" action="validate.jsp"> <table border=0> <tr> <td>UserName</td> <td><input type=text name=uname></td> </tr> <tr> <td>PassWord</td> <td><input type=password name=pwd></td> </tr> </table> </form> </body>

  21. validate.jsp <% String u = request.getParameter("uname"); String p = request.getParameter("passwd"); if (u.equals("abc")) { %> welcome <%=u%> <br> <a href='c.jsp?uname=<%=u%>' > Go to c.jsp</a> <% } else out.println("you entered wrong user name"); %>

  22. c.jsp <% out.println("this is c.jsp"); String uname = request.getParameter("uname"); %> Welcome <%=uname%>

  23. Second Solution is Session We can pass variable uname from login.html to validate.jsp using request variable. But to pass variable from validate.jsp to c.jsp, we have another better solution  Use Session

  24. How to use session session is an object in JSP It provides 3 functions - void setAttribute(String name, Object value) -Object getAttribute(String name) -void removeAttribute(String name)

  25. session.setAttribute setAttribute() is used to set the value of attribute Syntax: session.setAttribute(name of variable,value) session.setAttribute(“uname”,u) Please note that data type of 2nd attribute is Object. Object is a super class of all classes. A reference variable of type Object can store any type of object.

  26. session.getAttribute() getAttribute() returns the value of attribute. Syntax: Object getAttribute (name of variable) String uname = (String) session.getAttribute(“uname”); Because the return type is Object, we need to typecast to String type.

  27. session.removeAttribute() Session.removeAttribute() removes the attribute Example: Session.removeAttribute(name of attribute); Session.removeAttribute(“uname”);

  28. Validate.jsp <% String u = request.getParameter("uname"); String p = request.getParameter("passwd"); if (u.equals("abc")) { session.setAttribute("uname",u); %> welcome <%=u%> <br> <a href='c.jsp' > Go to c.jsp</a> <% } else out.println("you entered wrong user name"); %>

  29. c.jsp <% out.println("this is c.jsp"); String uname = (String)session.getAttribute("uname"); %> Welcome <%=uname%>

More Related