1 / 25

Ce este JSP? Un prim exemplu: s.jsp

Creeati fisierul s.jsp care contine: Va saluta <I><%= request.getParameter("nume") %></I> Depuneti-l in $CATALINA_HOME/webapps/ROOT/s.jsp?nume=Boian+Florian Porniti Tomcat si lansati: http://localhost:8080/s.jsp . Efectul:. Ce este JSP? Un prim exemplu: s.jsp.

colman
Download Presentation

Ce este JSP? Un prim exemplu: s.jsp

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. Creeati fisierul s.jsp care contine: Va saluta <I><%= request.getParameter("nume") %></I> Depuneti-l in $CATALINA_HOME/webapps/ROOT/s.jsp?nume=Boian+Florian Porniti Tomcat si lansati: http://localhost:8080/s.jsp . Efectul: Ce este JSP? Un prim exemplu: s.jsp In:$CATALINA_HOME/work/Catalina/localhost/_/org/apache/jsp/s_jsp.java Este textul sursa al unui servlet, care are partea esentiala: out.write("Va saluta <I>"); out.print( request.getParameter("nume") );out.write("</I>"); F. Boian, JSP

  2. Exemplul 2: 5numere.jsp <html><head><title>Cinci numere aleatoare</title></head> <body><h3>Cinci numere aleatoare, mereu altele:</h3> <ol> <% java.util.Random r = new java.util.Random(); for (int i=0; i<5; i++) { %> <li><%=r.nextInt()%> <% } %> </ol> </body></html> F. Boian, JSP

  3. Efect 5numere.jsp F. Boian, JSP

  4. Exemplul 3: alfabetCircular.jsp <html> <head> <title>Alfabetul cu JSP</title> </head> <body> <!-- Fixarea informaţiilor globale din pagina --> <%@ page language = "java" %> <!-- Declara o variabila caracter --> <%! char c = 0; %> <!—Scriptulet cu cod Java --> <% for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { // Expune o litera mare a alfabetului c = (char) (0x41+(26-i+j)%26); %> <!—Scoate c.toString() in pagina HTML --> <%= c %> <% } %> <br> <% } %> </body> </html> F. Boian, JSP

  5. Efect alfabetCircular.jsp F. Boian, JSP

  6. Principalele taguri si obiecte JSP • <%= expression %> • <% excerptCode %> • <%! declaration %> • <%@ page atrib=“val” %> • <%@ include file=“url” %> • <jsp:include page=“...” /> • <jsp:forward page=“...” /> • <jsp:useBean atrib=“val”/> • <jsp:setProperty .../> • <jsp:getProperty .../> • <%@ taglib uri=“tagName” ... %> • <%-- ... --%> • request • response • out • session • application • page • config • pageContext • exception Detalii in JSP20Card.pdf F. Boian, JSP

  7. Expresii Evaluată în faza de execuţie, în momentul cererii paginii. După evaluare rezultatul este convertit la String şi depus în ieşire. Exemple: Timpul curent la server este: <%= new java.util.Date() %> Clientul este: <%= request.getRemoteHost() %> F. Boian, JSP

  8. JSP: <% if (Math.random() < 0.5) { %> Ai dat un zar <b>slab!</b> <% } else { %> Ai dat un zar <b>bun</b>(sau destul de bun). <% } %> Fragmente de cod Cod java in servletul generat: if (Math.random() < 0.5) { out.println(“Ai dat un zar <b>slab!</b>“); } else { out.println(“Ai dat un zar <b>bun</b>(sau destul de bun)”); } F. Boian, JSP

  9. Max.html si max.jsp <html><head><title>Maximul a doua numere</title></head><body> <form method="post" action="max.jsp"> <p>Primul numar:<input type="text" name="unu"> <p>Al doilea:<input type="text" name="doi"> <p><input type=submit value= "Maxim"> </form></body></html> <html><head><title>Maximul a doua numere</title></head><body> <% int n1 = Integer.parseInt(request.getParameter("unu")); int n2 = Integer.parseInt(request.getParameter("doi")); %> <CENTER><H1>Maxim(<%=n1%>,<%=n2%>)= <%=(n1>n2)?n1:n2%></H1></CENTER> </body></html> F. Boian, JSP

  10. <html><head><title>Maximul a doua numere</title></head><body> <form method="post" action="maxim.jsp"> <p>Primul numar:<input type="text" name="unu"> <p>Al doilea:<input type="text" name="doi"> <p><input type=submit value= "Maxim"> </form> <% String s1 = request.getParameter("unu"); String s2 = request.getParameter("doi"); if ((s1!=null)&&(s2!=null)) { int n1 = Integer.parseInt(s1); int n2 = Integer.parseInt(s2); %> <CENTER><H1>Maxim(<%=n1%>,<%=n2%>)= <%=(n1>n2)?n1:n2%></H1></CENTER> <%}%> </body></html> maxim.jsp – dublu rol F. Boian, JSP

  11. Directiva page • import="package.class" sau import="package.class1,...,package.classn“ <%@ page import="java.util.*,java.sql.*" %> • contentType="mimeType" sau contentType="mimeType; charset=characterSet"  <%@ page contentType="text/html; charset=ISO-8859-1" %> • isThreadSafe="true|false" • session="true|false" • buffer="sizekb|none"<%@ page buffer="8kb" %> • autoFlush="true|false" • extends="package.class" • info="mesaj“Se defineşte mesajul ce va fi returnat prin intermediul getServletInfo(). • errorPage="urlFisier" • isErrorPage="true|false" • language="java" F. Boian, JSP

  12. principal.jsp : <html><head><title>Test include</title></head> <body bgcolor="white"><font color="blue"> Data si ora curenta este: <%@ include file="data.jsp" %> </font></body></html> data.jsp : <%@ page import="java.util.*" %> <%= (new Date()).toLocaleString() %> include F. Boian, JSP

  13. principal.jsp : <html><head><title>Test include</title></head> <body bgcolor="white"><font color="blue"> Data si ora curenta este: <jsp:forward page="data.jsp" /> </font></body></html> data.jsp : <%@ page import="java.util.*" %> <%= (new Date()).toLocaleString() %> forward F. Boian, JSP

  14. <jsp:useBean id="numeInstanta" scope="page|request|session|application" class="package1.class1" type="package2.class2" /> Folosire bean-uri <jsp:getProperty name="numeInstantaBean" property="numeProprietate" /> <jsp:setProperty name="numeInstantaBean" { property="numeProprietate" value="string" | property="numeProprietate" value="<%= expresie %>" | property="*" | property="numeProprietate" | property="numeProprietate" param="numeParametru" | } /> F. Boian, JSP

  15. <html><head><title>Test usebean</title></head><body>  <jsp:useBean id="st" class="stud.Student" />  <jsp:setProperty name="st" property=“code" value=“AB5" /> <%st.setStipend(900000); %> Stipend, solution 1: <%=st.getStipend()%>. Stipend, solution 2: <jsp:getProperty name="st" property="stipend" />. Code: <jsp:getProperty name="st" property="code" />. </body></html> Exemplu de bean package stud; public class Student { private String code = ""; private int stipend= 0; public Student() {} public String getCode() {return code;} public int getStipend() {return stipend;} public void setCode(String code) {this.code = code;} public void setStipend(int stipend) { this.stipend = stipend;}} $CATALINA_HOME/$CONTEXT/test.jsp $CATALINA_HOME/$CONTEXT/classes/ stud.Student.class F. Boian, JSP

  16. Acces la baze de date:citirea notelor de catre un student F. Boian, JSP

  17. NoteStudent.jsp (1) <html><head><title>Student</title></head><body> <%! String resMaterii="", resNote="", resStudent="", resParola="", student, parola; %> <jsp:useBean id="adb" class="db.NoteAccessDB" /> <% if (request.getParameter("buton") != null) {// Cererea vine de la acest fisier jsp student = request.getParameter("student"); parola = request.getParameter("parola"); if (!db.AuthFTP.isFTPUser("localhost", student, parola)) { resStudent = "ERR: student,parola"; }// Eroare de autentificare else { // Citire materii-note student = adb.getMaterieNota(request.getParameter("student")); resMaterii = student.substring(0, student.indexOf("\t")); resNote = student.substring(student.indexOf("\t")+1); resStudent = "OK citire"; resParola = ""; }// Terminat citire materii-note }//Terminat cererea din prezentul jsp %> F. Boian, JSP

  18. <form method="post" action="NoteStudent.jsp"> <table border="0"> <tr> <td><center>Materie</center></td> <td><center>Nota</center></td> </tr><tr> <td><textarea name="materii" rows="10" cols="10"><%=resMaterii%></textarea></td> <td><textarea name="note" rows="10" cols="10"><%=resNote%></textarea></td> </tr><tr> <td align="right">Student:</td> <td><input type="text" name="student" size="13" value="<%=resStudent%>"></td> </tr><tr> <td align="right">Parola:</td> <td><input type="password" name="parola" size="13" value="<%=resParola%>"></td> </tr><tr> <td><input type="submit" name="buton" value="Citeste"></td> <td></td> </tr> </table></form></body></html> NoteStudent.jsp (2) F. Boian, JSP

  19. package db; - - - public class NoteAccessDB { - - - public NoteAccessDB() throws - - - { - - - } public boolean isMaterieProfesor(String materie, String profesor) throws - - - { - - - } public String getMaterieNota(String student) throws - - - { - - } public String getStudentNota(String materie) throws - - - { - - - } public void setStudentNota(String studentnota) throws - - - { - - - } } NoteAccessDB.java F. Boian, JSP

  20. JSP: Expression Language Declarare variabile: <%@ variable name-given=“nume” variabile-class=“java.lang.Stri|altaClasa”%> Evaluare: ${nume} Evitarea evaluarii: \${nume} • Variante getProperty: • <jsp:getProperty name=“numeInstanta” property=“numeProprietate” /> • <% - - - numeInstanta.getNumeProprietate() - - -%> • <jsp: - - - numeInstanta.getNumeProprietate() - - • ${numeInstanta.numeProprietate} • Expresii de constante si variabile: • Aritmetice: + - * / div % mod ?: ( ) Exemplu: ${10 % 4} afiseaza 2 • Comparatii < lt > gt <= le >= ge == eq != ne) Exemplu: ${4 le 3} afiseaza false F. Boian, JSP

  21. JSP Expression Language obiecte implicite • pageContext • Obiecte java.util.Map (nume,obiect): • pageScope • requestScope • sessionScope • applicationScope • param – parametrii cererii • Header – antetul cererii • initParam - parametrii de initializare • cookie F. Boian, JSP

  22. <html><head><titleEL</title></head><body> <form action="elObjects.jsp" method="GET"> <input type="text" name="ceva" > <input type="submit"></form><table> <thead><td><b>ELexpression</b></td><td><b>Result</b></td></thead> <tr><td>\${param.ceva}</td><td>${param.ceva}</td></tr> <tr><td>\${param["ceva"]}</td><td>${param["ceva"]}</td></tr> <tr><td>\${header["host"]}</td><td>${header["host"]}</td></tr> <tr><td>\${header["accept"]}</td><td>${header["accept"]}</td></tr> </table> </body> </html> JSP EL exemple F. Boian, JSP

  23. <html> <title> Plugin example </title> <body bgcolor="white"> <h3> Current time is : </h3> <jsp:plugin type="applet" code="Clock2.class" codebase="applet" width="160" height="150" > <jsp:fallback> Plugin tag OBJECT or EMBED not supported by browser. </jsp:fallback> </jsp:plugin> <p><h4> <font color=red> The above applet is loaded using the Java Plugin from a jsp page using the plugin tag. </font></h4> </body> </html> Utilizare plugin pentru applet F. Boian, JSP

  24. Efectul plugin (Internet Explorer) F. Boian, JSP

  25. F. Boian, JSP

More Related