! " # $ %& ' () ! "# $ "% ) ******************************************************************************************************************************** + $&'($ ,% -(,. -. . $ (/ 0-&-0 $&1 '$% - $ * ******************************************************************************************************************* + $&'($ ,0- . $ (/ * ******************************************************************************************************* + $&'($ "% . -2$% -($ . $ (/ &"0 "&- . $ "% $&'($ ************************************************************************************** 3 ********************************************************************************************************************** 4 $&'($ 2# /. / . $( 0,0(/ . $ ',. - ******************************************************************************************************************** 5 $ 6 78************************************************************************************************************************************** 5 $ 6 ********************************************************************************************************************************* 9 $ 6 ******************************************************************************************************************************** 9 -! "$ $ -'-: * $&'($ *; < -'-: * $&'($ *= *; ********************************************************************** > ( , ? ***************************************************************************************************************************** > ( 0 = @ ************************************************************************************************************************** ( 0 & AB ( 0 & @ ( 0 0 *********************************************************************************************************************** D ( 0 0 E ******************************************************************************************************************** D $F @ ( 0 B ? ******************************************************************************************************************** C ***************************************************************************************************************** C G 0 H $ ,0,I % &$ "$ 0 E ********************************************************** 3 ******************************************************************************************************************** 3 7&$! "$ - 7&$ 8 -( $&'($ = /% $8 . $( $&'($ ************************************************************************************************ 4 = $ $2 (/ . $ 2/. $(/ $ ,0,I % J&$ "$ ***************************************************************************************** 5 - ****************************************************************************************** 9 0&$-0,I % ****************************************************************************************************************************************************CK = $ ,/% ********************************************************************************************************************************************CK H G G ********************************************************************************************************CK H G ******************************************************************************************************************************C3 2 F G ***********************************************************************************************************C3 , = @ ( 2 = @ - = @ $ F ( **************************************************************************************************************C4 B L ( *********************************************************************************C4 G *********************************************************************************C4 ************************************************************************************************************C4 = @ - ( B = @ ( *******************************************************************************************************C4 L $ ************************************************************************************************C5 ! ,% $& -0$ $&'($ 0/% $: *****************************************************************************************************************C9 2-% $ / . $ $'$% / ********************************************************************************************************************************D > , 0 E ( , 0 E - " # ***********************************************************************************************D > B ( ****************************************************************************D > $ " - , ,( &/ *************************************************************************************************************************************************D D F E* * F E* * 0 ****************************************************************************************************************D D F E* * 0M ********************************************************************************************************************D + % *****************************************************************************************************************************D D & -% / -0,/% $ ********************************************************************************************************************************************D 3 &/0$ -2,$% / - 1% 0&/% / ******************************************************************************************************************D 5 &-H 2$% / $L **************************************************************************************************************************************+> / &/ **********************************************************************************************************************************************************+> $ $2 (/ ****************************************************************************************************************************************************+> $ $2 (/ C****************************************************************************************************************************************************+ Contenedor / Servidor " F B G @ B AB @ @ @ @ B @ @ *$ " @ B ? G @ ? B * F B G @ B @ B 6 @ @ @ @ F B G @ " $$ @ B AB @ $ L B $$ O @ ( 7 F F B G P @ AB F B $ $L @ 0 F B @ B B @ FB 8 * B 2 * . AB * B @ @ @ @ B ( @ B AB AB N AB * G $$ * @ @ @ B = ** * 0 B -@ G @ '2 @ G $$ * B @ @ @ *$ B B @ @ B -: Q . L 0Q 2 -@@ @ ? -@@ AB @ -- * 0 @ -@@ AB 0 @ $ L @ 7$ L 8 @ @ F B @ @ B @ B * @ @ B G B Servlet: Introducción a los Servlets ¿Qué es un Servlet? ' R O" B B B F S @ G R AB AB B T B @ B @ AB G *( @ AB U @ AB B S B @ @B * @@ @@ @ F B AB *% AB M AB *( @ F B R F B * F B M @ *P B @ @ @ AB S R B @ @ M @* B B @ @ ? ( S ? G 8 AB M @B AB @ B @B B B F @ @ @ @B 7 @ M S @ @* 7 @ 8 B F @ AB * B F B F @B * Finalidad de los Servlets. -B AB @ @B @ F B AB * B @ @ M G B R @ = @* ( @ AB * 0B B = @ F @ E* F E* B @ *M 6 H @ @ 7 B - , B H 6 @ M F @8 @B * ( = * Características de los servlets. ( B F B G B @ ? B @ E AB S @ @ B AB G J 7 '28 F B @B B * @ * B * @ B AB B @ B AB ( @ * @ @ @ G G * . @@ AB , * " @ 7 ? S @ B AB B AB B AB F B * ( @B 8 F B @ F * 0 &"% @M L $- - B M ( M - , @ 7@ AB 7 o = @ & AB = @ & M @ * E* *M 8 & AB & E = @ @B @8 H 7M @ $ @ AB @ 7M @ @ @B H F AB M o " 6 B 8 @ 8 *$ 6 @ G = @ 7 A M M G @ 6 @ S M = @ F N @ @ o 7 6 6 = * B 8V H T H 78 78 H$ / G 6 @ M @* AB H$ "&( @ @ AB * @ 7! B H$ @ @ 6 "&( 8 / @ B @ AB 6 B AB @ B @ / B B ? $E @ @ G / M @* @ B *$ 6 H$ * @ AB @B M M " B B B 7@ M B @ @ AB @8 S B . $($ $ 7@ 8* Ventajas fundamentales de los Servlets '()(*+)(, o , @ @ G o $F B G o 2 o ( B o o o @ F B B M B @ @ G B B @ @ M B @ B B @ AB G F @ E 0H , * AB B 0 @ ? M ' B G @ @ B @ * @ G 6 @ @ * M + M M * 0 = B @ @ 6 * 0 M @ 0 @ @ B B AB * 0 * ? M6 E B AB B @ *( 0H , @ @ * *-./(0 ,0 AB 7 8 B E - , B @ M AB @ G - , @B $$ AB * B F * +1*-/,)(2+ 3 41*+)(, 0* 5, 65,1,'4/7 ,8 , G B * ( 0 @ * M 4/1,9 5* ( B B - , ? * :1*+;(9(5(0,0 < '5*: (9(5(0,0 ( - , o E 7 G ** o 8* ( , o * @ W* Estructura de un servlet $ G B @ @ F * *;Q @ F E* *;Q @ F E* *M @B @*;Q B E = @ V @B @ M H 7 $E @ = @ & AB AB = @ & 8 S ,/$E @ V*** X X @ * C* E D* 6 H 7 AB @ = @ AB 8* H * $% @ Ciclo de Vida de los Servlets " B ? B S B @B B @ * $ @ M B 7 0B B F B G B B B @ J @ G G 8 @ * B ? @ E o 0 o 0 o , 0 7 M * 8 B 7 B ? , 8 6 6 @ B F * G J& @B * ? 6 * $ ( 6 6 @B * @ F AB @ O @ B M F * P AB Métodos del ciclo de Vida $ 6 M $ . @B M @ @ . @ B 7S *E 8 @B @ B @B $ 78 * AB 6 B B @ B AB @ G * @B6 AB B B M G @ 6 * 78 7 , 0 7 7 NX 78 @ 8* $ 8 AB 0 F @ 0 6 B 8V G * 78V NX " @ @ ? AB S S ? @ ? ? B ? B AB ? ? @B G * G @ 6 ? AB B G G @ B * $ 6 B B * $ AB @ @ $ 6 = @ B & B & AB G * = @ @ @ * @ 7= @ F @ & AB = @ 6 AB = @ & & AB H @ @ 8V **X $ @ * @ B ?* AB @B API de los Servlets Paquetes javax.servlets.* y javax.servlet.http.* $ - ,@ @ AB E* *; E* *M @ @ @*; @ @ M @7 F G B 8 ! "# @ ? AB 6 * $ @ AB B F B @ ? * = = 7 F @B F E* F E* G B 8 *H @ *M @*= @ @ @ * @ - @ ? @ 26 ? ? @ B B @ @ 6 * ? 7 0 @ 0 8 ? AB S */=5*1 4+'(- AB S E 7 $ & AB 6 26 A F *>.*;1 *;64+;* & F B B G * 8 B & AB @ B @ B G AB @ B 7 B & @ B AB & @ 6 7 @B * @ B @ G 8 B F 8* G @B @ 78 0 ? AB @ @ 7 B S @ ? E $ @B B @ M F B G * B B @ G 8*$ B B ? BF 6 G E @ *** @ M @ @ G ** % &' 0 ( 6 AB M = @ H H AB @ " B ? @ 6 ? @ * @ resp) . 26 *;.7 *+ protected void 04 *5*1*(= @ , @ protected = @ 7 void 04@ *1(= @ & AB & 8@ & M @ = @ req, @ 7 F B & @ resp) 8@ F B H$ void 04$ *,0(= @ void req, . $($ $ @ protected & AB @ , protected ?1404; B & AB @ G = service 04 61(4+;(= @ = @ req, & @ =$-. M & AB resp) AB M req, M = @ AB * & @ resp) , B protected @ @ / B & AB protected & AB @ resp) 8@ @ = @ req, @ F & 7 @ resp) 8@ F " void 04 /,)*(= @ , @ @ &-0$ & AB = @ req, 7 B & AB @ = @ & F B ? AB * = @ req, @ resp) B B & AB B @ req) B & AB void ;*/=()*(= @ & 8 @ long -*1 ,;1 40('(*0(= @ . protected F / , protected & 7 void 04 .1(= @ B = @ req, @ @ 8@ ,/% void 04 4;1(= @ , protected 7 & @ resp) = @B G ::: @ * void ;*/=()*( & $ $ 0 ' 0 0 ( )* ' ( & & req, @ AB AB AB & AB @ B 6 @ B @ 6 @B G @R @ @ B @ res) * $ ' , $ @ @ 26 " G @ *;.7 *+ 0* 54; 7 ?1404; java.lang.String -*1 +(1 ,/,7 *1*/(java.lang.String name) . B B AB @ ? B @ ? Y G T@ ZY@ YJ B T ZYJ@ B $ B T@ Z ? F E . B @ B B * 0 F B java.lang.String E * B * + ? AB @ B G E F B 26 *1A40 java.lang.Object B -*1 */=5*1 ,7 *() . , ZYJ -*1 */=5*1 4+1*:1() E $ T @ $ B ' AB Z B $ * ( -*1 +(1 ,/,7 *1*/ ,7 *;() . 0 @ Z Y java.util.Enumeration E .7 7 ,/< -*1 11/(9.1*(java.lang.String name) . B B B B * java.util.Enumeration -*1 11/(9.1* ,7 *;() . B B B @ $ B * AB * E 0 R B B $ B * -*1 4+1*:1(java.lang.String uripath) E . B B F 0 E B "&, B AB * java.lang.String -*1 +(1 ,/,7 *1*/(java.lang.String name) . B B @ AB @ S @ *E Y E T@ * B ZY@ T @ Z AB E B * java.util.Enumeration -*1 +(1 ,/,7 *1*/ ,7 *;() . B @ E 7S *E @ int 8 B $ B * $ B -*1 ,B4/ */;(4+() . B @ @ @ java.lang.String - , AB *$ B B @ 2,2$ M @ @ 2,2$ int * B * -*1 (+4/ */;(4+() . - ,& . @ M B B - ,* F & AB @ . @ @ M AB R * -*1 *,5 ,1A(java.lang.String path) B @ M M B @@ . . @ M -*1 ,7 *0 (;6,1)A*/(java.lang.String name) B S java.lang.String B B . & AB C*D -*1 (7 * <6*(java.lang.String file) . & AB E * B OP * B ? * -*1 *>.*;1 (;6,1)A*/(java.lang.String path) . B * S B @@ B @ F & AB B . @ B M AB R @ MJ B java.net.URL -*1 *;4./)*(java.lang.String path) . B java.io.InputStream B "&( AB B @ B M B B @ S B B B * -*1 */=*/ +'4() . B G AB * -*1 */=5*1 4+1*:1 ,7 *() . B @ F @ @ @ @ @ B G 0 E B S AB @ G S * F * 54-(java.lang.String message, java.lang.Throwable throwable) $ $E B F @ E@ * /*7 4=* 11/(9.1*(java.lang.String name) $ 0 void T @ 54-(java.lang.String msg) $ void AB * @ @ void , @B B B void B -*1 *;4./)* ,1A;(java.lang.String path) . java.lang.String @ * B @ java.lang.String F -*1 *;4./)* ; 1/*,7 (java.lang.String path) . java.util.Set B @ M B E * ;*1 11/(9.1*(java.lang.String name, java.lang.Object object) $ 0 B E * F B @ , & * 6.95() =4(0 # - ' 7 $ " * B *@ 7[ * B *@ 7[% + * B *@ @ [\ ] 7[' B] $E V 78[8Q 0 $ B $ 8 1A/4C ; 0 @ ' * , % 7[ @ @ * * 0 , [8Q [\@ 0 % 788Q 8Q 78Q ][[Q C A(5* 7 B*M 2 $ 788V ]7 [\ 8 B* E $ * B *@ 7[% * B *@ * , 7[' 78Q @ 0 @ [\ 8Q 0 7 88Q X 0 E 0 E] @ 0 E ]PPQ @ 0 E ] * B *@ 7[' XJJ $ 0 @ * 0 0 E* , 0 E [\@ E 78Q 7[0B 0 E 0 E 8Q 78 . ' AB 6 @ 0 @ @ * F [8Q Manejo de peticiones y respuestas Petición (request) al Servlet HTTP 0B ? = @ @ @ G $ M ? , M & AB @ G G B @ 26 B & 26 @ G 7 G @ 6 @ , @B AB B @ = @ ?F @ B @ B 7 G & AB * E* 7 G @ *= H $ 8* & AB G @ G B , ? G $ B @ @ G $ @ @ G $ $ & G @ G 7. @ @ @ @ @ = @ & AB / F - M M 8 AB B 6 B 7 @ 8 B B B B R M AB M B * F *B *$ B B 7 AB @ , @B @ 6 $ @ B 8* ? G @ 8* !B 26 AB & @ B 6 / ? B LB AB / B $ $ & AB * ' B * % * ? B @ 0 B @ @ B B % 78 @ B @ 8 B B B B G AB G * @ M @ E * F @ *B *2 @ G * 2 @78 ' B B @ 0 ^ M @ 0 M78 ^ = F *B *B B 8 @ , @ Y B Z Y = @ Z % @ = 7 B B 8 B G B @* B B * 2 H$ 8 78 G * 7 M = B G * * * B M E ^ @ *$ B @ "&( AB M F B B * 7 *$ B @ B B 78 @ @ 8 E E B 7 M @ 0 G * B M 78 " * / = @ @ G 6 B 78 G @ 7 @ = @ G M @ B @ @ G Q @ G * B F @ 8 6 @ G * @ G B * Respuesta (response) del Servlet HTTP ? $ 116 */=5*1 *;64+;* @ ( 0 @ 26 M U @ @ @ / = 2( @ ? @ = 2(* @ S F @ 6 AB B @B F F & @ @ ? @ , @ 2 B * 0 $E = 2( @ @ 8 AB 8* B G @ /B @B 6 6 @ 6 @ 2,2$ 72B @B @ @ 7 E B @ * B AB " AB @ B B @ 6 G @ 0 @ E AB * /B @B B @ * 0 AB /B @B S @ AB @B @B6 M @B @ * @ ( 6 R F @ * * 78 = @ & S * *,/$E M F AB @B @ S B AB @ @ 7 * @ B [ E J@ @ 0 ^ 70 ^ = 7 E @ F @ [ E JM S @ ]" T5[ @ AB ^ 8 = F @B F Q M B B B @ U B 8 ]" T5[ @ T5 @ AB * " AB @B U @B @ 7 Q M ^ B 8 @ B *= * @ 8 AB AB @ B B @ 0 @ B * B * @ B @B B * & 6 = F *B *B B *0 8 Y Z @B @ *0 Y $ @ B 8 @ 7 M F "&(7 *2 & B "&( @ @ B B B = M % 78 @ @ 6 * @ * B * Z 7 @ = M 7 8 B @* 8 B @B G * & 7 /B @B /B @B 8 B /B @B Q *+ 78 F @ ! ,* B "&(* B F @ @ AB B - * &+ .( / 0 1 Ejemplo de modelo petición/respuesta @ E G / H$ 6 @ = * H$ o $ "&( @ @ AB o 6 7! B @ 8 @ "&(* / o $ @ B @ AB @ @ M @* 6 o B B / B ? @ AB 6 @ - B G G B *-(;1/4 A17 5* B B F B @ B G @ / * $ H$ * B @ B @B @ M AB *( @ *-(;1/4 A17 5* YM Z YM ZY Y Z Y Z& ,. 2$ =/. ][ / JM Z [ -0 ,/% ][J J& [Z YM Z YMCZ Y Z& % Y,% " % -2$][% 0 Y Z [ '-("$][[ZY Y,% " % -2$][0 Y,% " < $] B YJ [ '-("$][[ ZY ' B ] [$ Z Z [Z Z YJMCZ Y Z YJ Z YJM Z $ AB B @ @ *-(;1/4 B,=, @ G @ F * *;Q @ F E* *;Q @ F E* *M @B G & @*;Q E = @ V @B M 7= @ S $E @ & AB ,/$E A = @ & @ V * 0 @ 7[ B ] JJ( E JM [8Q * 78Q @ @ ]7 8 A* @ G 7O% P8Q @ 8 ]7 JJ2 X @ 7[YM Z[8Q B *@ 7[YM Z[8Q B *@ 7[Y Z& B *@ 7[YJM B *@ 7[Y B *@ 7[. B *@ 7[YMCZ% B *@ 7[YMCZ0 B *@ 7[YJ Z[8Q B *@ 7[YJM Z[8Q 78Q JJ A* @ 7O0 P8Q @B B *@ B * X 8 YJ Z[8Q Z[8Q Z[8Q [ 8Q [\ [\ \[YJMCZ[8Q \[YJMCZ[8Q AB M G Escribiendo un Servlet sencillo @ B B G ? B @ B B B B F @ ? AB AB B @ @ AB G * @ B G * <html> <head> <title>Ejemplo de servlet</title> </head> <body> <h1>Introduzca su nombre y pulse el boton de enviar</h1> <form action="miaplicacion/HolaServlet" method="post"> Nombre:<input type="text" name="nombre" size="30"> <input type="submit" name="enviar" value="Enviar"> </form> </body> </html> - B G B G import java.io.*; import javax.servlets.*; import javax.servlets.http.*; public class HolaServlet extends HttpServlet { /** Redefinición del parámetros de inicio*/ metodo init y configuración de los public void init(ServletConfig config) throws ServletException { /** Llamada la metodo init() de la superclase.Esto es imprescindible para la correcta inicialización del servlet y debe realizarse antes que cualquier otra acción*/ super.init(config); System.out.println("HolaServlet arrancado a las " + new Date()); } /** Redefinición del metodo destroy sin tareas a realizar en este caso*/ public void destroy() { System.out.println("HolaServlet detenido a las " + new Date()); } /** En este caso se ha optado por redefinir el metodo doPost(),pudiéndose igualmente haberse optado por redefinir service().Lo que sería incorrecto es redefinir doGet() ya que la peticion se realizará por el método post*/ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException { /** Se obtiene el valor del parametro enviado*/ String name = request.getParameter("nombre"); /** Se establece el contenido MIME de la respuesta*/ response.setContentType("text/html"); /** Se obtiene un flujo de salida para la respuesta*/ PrintWriter out; try { out = response.getWriter(); }catch (IOException e){ System.out.println("Error en el canal de "+e.toString()); } /**Se escribe la respuesta en HTML estándar*/ out.println("<html>"); salida: out.println("<head>"); out.println("<title> Respuesta de HolaServlet </title>"); out.println("<head>"); out.println("<body>"); out.println("<h1>¡Funcionó!:El pagina</h1>"); servlet ha generado la out.println("<br>"); out.println("<font color="red">"); out.println("<h2>Hola " + name + "</h2>"); out.println("</font>"); out.println("</body>"); out.println("</html>"); /** Se fuerza del volcado del buffer de la salida y se cierra el canal*/ out.flush(); out.close(); } //fin doPost() }//fin clase Sesión y estado @ 6 • G @ ? B • * $ @ ? B AB EG B * M • B 0 @ * G - , G B ? B ^ B ^ $F @ AB AB @ AB G * ^ OF P@ @ !B * @ !B 0 8 @ @ B M @ "&(* ^ 6 0 @B6 @ B 7= B @ JJS S S *F B * Y @B ) @ ]P= P ]D D D D D D ]PB D P B ]P PZ ^ 0 ^ 0 ( ^ ]+*C 0 0 ^ * 2 E- 0 ^ * . @* 0 ^ 7 E* *M $ - , 7[*B D * 0 @*0 @ = @ ^ 7[ 73> 8Q JJ B F G B @ E AB G B @ - B *= J 6 • ( B 6 !B • B [ [F B [8Q @ 3> B [8Q ^ 8Q ^ B ^ 0 @ * ^ B ] AB @ * 0 ^ 78Q AB B @ B S Creación ( G B G B AB @ ? @ F = @ * ( B 6 = @& AB 26 . 78 7 B F E B G B B * B B 8 G B = @ E B @ G B ] A* 78Q HttpSession $ @ B . - G G * - . AB G EG R *0 M @B G B B B @ B B G B @ ? @ B @ * 26 0 ( - B . B , 78 2 E, , 7 . AB AB R M @ B @ @ B @ G AB G B Y B T B Z B E B G * , AB B $ . B B ? B ' 78 B M G@ 2 E, , 78 M G 78 8 , . 78 G S *E G $ ? ? J B . . ? B @ G M S @ *E B @ G Y T Z* G AB = @ B@ 6 AB @ B AB E@ G * 26 - B 7 - B 7 B B B E / F % - M - B B B B F * $ @ P* , B ? @ AB 6 B F 6 , B * - @ @B * @ B G * B AB @ G G AB B @ 7 B B @ ? B . 8 B @ B B 7 B O . 78 = / B 8 - @ . 8 * B ? @B G 8* ? , - B $E @ * - % B B @B = @ ( 7= @ $ B G * 26 0 8 . 7= @ $ $ 6 B G $ 8 0B AB @ @B @ @ @ B 7S *E 8* F @ B B 6 G 6 E 6 @ 6 G B B E@ B Y Z Y T YJ ! $ " Z2 $ B M YJ T Z Z % &' B @B 0 ? @ G = @ - @ G G * ( 26 . - 7= @ $ $ @ 8 B 7= @ $ 6 @ B S G @B G $ 8 ( @B6 ? @ G * *% B *E % &' $ G B @ @B 6 = @ 78 @ B G * / , % &' ( 1* - 2 $ 116 *;;(4+ (+0(+- (;1*+*/ @ M 6 B ? 8 B - @B B U B = @ @ L B F 7 AB G 7 8 ( E F *B *$ ( 26 B L B $ 78 6 AB B G $ B " L B 78 F 6 AB F B G E@ % &' ( 0 G * 1* $ 116 *;;(4+ 11/(9.1* (;1*+*/D F B G * @ @B = @ - 7= @ L B ( E F *B *$ ( 26 B - $ $ B 8 B B B U B G & 7= @ L $ $ 6 B 8 B 6 B B G * & @ 7= @ L $ 0B B B 8 % &' 2 & @ B @B = @ G L B $ G E G * = @ $ 26 % . 78 B B B B B . ' B 78 B B B 78 . B B B @ ? B B / F = @ AB B @ G B @ ? * Contexto de Servlet = B - , O0 B @ E P G B @ G B B 7M B B G G AB B AB B @ B G ? @ AB E @ E AB E * G B B *E JZ @ M @ T @@Z 8 @ R T @@Z Y B G E @ Y @ @ G G * Y ( B B R B @ @ @ B * 0B @ * - @ @B ? * 7$ B M B @ G @ '28* Interface ServletContext $ @ B " F E B G * @ @B 0 . B @ E G ? 0 ] 0 E@B @ E 78Q @ E B 6 @ * $ G @ @ @B 26 @B / F @B $ B @B @B F G * 0 E - B - - B - 7 8 B 7 B % 78 / F 7 B 8 8 Manejo de Eventos ! " " ' $ + F G *. AB B G E B AB @ @B B @ E @ G * 0 E ( E F *B * ( 26 E , ? 7 0 E $ B 8 0 E $ B 78 E AB @ " B * S *E Z ' % B E M Y ! B E E . ( B $ B +0 E 1* B @B B 0 E - B ( E E * F *B * ( 26 $ B - 7 0 E - B $ 6 8 B M B B E B & @ 7 0 E - B $ * , B , B 8 B 8 & 7 0 E - B $ B U La interface RequestDispatcher $ @ B B E * @ B ? @ B @ B $ J @B * 6 S B B AB J B AB @ B 6 B , B = 2( B @B * B G ? * B $ 6 @ B @ @B *$ B @ B @B @B $F @ @B * $ @ R @ B B @ @ * @ & AB A. * & AB A. * . @ M S 7 . @ B A. A M 7 A* & AB . @ M 7[ A* & AB . @ M 7[J [8Q @8Q A. A ] @8 ] @ J F *F @[8Q G Filtros " B F @ B @ @B AB @ @ B @ @ $ L **8* @ G B AB @ @ ? = B B @ * % G ?G G , @B * B , R B T AB @ G G @B6 B @ * B B 6 @ AB B 6 AB @ AB ? 78 G * B * . B G 6 78 * " B AB @ @ F B @ * $ B *" @ . Y @ @ B 6 78 AB E Y @ T Y Y 0 Z T B B B YJ 0 ZFM * T@ YJ YJ Z T YJ 0 B B @ Z T YJ Z T Z Z Y@ T Y@ T T@ Z@ YJ@ T B Z YJ@ T B Z 0 YJ T Z Z Z T @@ Y Z T Z @ 78 Z T AB @ *E Y Y AB * @ 6 G S AB E* Z @ 78 @ * YB T@ YJ T Z;*M @@ YJB T@ Z @ AB AB . @ @ *E 8 @ B B AB 7S Z B AB @ G * @ @ @ B * API Filtro 0 , B +3 34 26 $ , 7 0 6 8 *$ B 7 & AB & . , $ A @ 0M G F B *2 B 8 * @ @ B 0M @ AB @ * ( 78 +3 G 6 @ M @ B * 34 $ " 0 @ @ @ * 26 % 78 , 7 , % 8 78 . B S *E . B . B @ @ B @ 0 E 78 . B E $ B * ? B %B @ G * B E @ G * , +3 34 $5 @ 7 6 0B B 8 78* B 6 B B B R E B * 6 @B 7 & AB A & @ @ M 0M ,/$E @ F @ ^ @ B AB B SQ (7 64/1 F * *,/$E (7 64/1 F E* * Q (7 64/1 F E* * 0M (7 64/1 F E* * 0 (7 64/1 F E* * 0 (7 64/1 F E* * $E (7 64/1 F E* * & AB (7 64/1 F E* * & 6.95() )5,;; @ 1A(;* Q Q E Q @ @ Q Q Q (7 65*7 *+1; B 0 6.95() =4(0 Q 0 JJ X B E Q 7 0 ] V 8V Q @ M S $E $F 6 G * @ @ 8 6.95() =4(0 7 & AB & @ 0M JJ 0 0 E AB @ 8 1A /4C ; ,/$E M E 0 JJ ' $E @ V G E ] * B 0 E 78Q E E JJ $ ? , 0 ('7 @ ] 7, 8 0 E * - B 7[ [8Q ]] +.558 V 0 ] +*C , 0 7> 8Q X ] +*C , 0 0 E JJ , M * * - X B 0 * 7[ [ B 78 VX AB @ 8Q ' B 78 \ 8Q 0 @ 7 X 6.95() =4(0 7 8Q AB AB B JSR 315: Servlet 3.0 @ G &D K D *> 2 0 @ @ G C*K D *> Anotaciones 2B @ M @ S B *E *( B @ G 7 B AB B B &D K @B @ "&( M D *> 2 B ? @ 8 @ @ ? G E * ( B _ `. B _ * 7 @B E ]P2 P B @ " @B = @ H ]V[J - 7= @ B [ [J E & AB [X8 = @ A= @ V & @ 8V X ( AB AB6 @ AB @ B G B _ 7 @ ][2 @ @ ; "&( 7 $$ 3 7AB B ][ 2 @ @ _ _ [X8 @ [ B G G D *> E B AB ]V[J2 @ B _ = [ B @ G / /7 / T. B 7OJ / F *. P8 @ = @ @ 6 8 @ @ ]V[J 2 J;[X8 G * $ 8 $$ 3 @ B * javax.servlet.Filter. B @B 2 @ @B V 7= @ & AB A = @ & @ 8V *** X X _ ( `. B ( * @ o F E* * 0 E ( o F E* * 0 E - o F E* * & AB ( o F E* * & AB - o F E* *M @*= @ ( o F E* *M @*= @ - _ B ( B ( B ( ( @B 2 ( @ @B 0 E , 0 ? E * 7 0 ] 0 [ [ [ 2 @@ V E $ * 7[ * E ( * 8V E 78Q @ [ *2 7[ [ [ B S T 8Q V [JB @ J;[ X8Q X X _ , ` . @ _ 7 ]V_ B ][' 2 B B ][0 AB ? [ 7 , ][@ 7 S G * B ? @ @ * B , [8 _ @ B B ]V[J0 B B ][' C[8X8 [ ][@ C[ *E <servlet> <servlet-name>ConfigurableServlet</servlet-name> [X <servlet-class>tidw.ConfigurableServlet</servlet-class> <init-param> <param-name>parametro1</param-name> <param-value>Valor1</param-value> </init-param> <init-param> <param-name>parametro2</param-name> <param-value>Valor2</param-value> </init-param> </servlet> $ AB @ B * ( @ S @ E *E G B @ @ F @ ? ? @ S @ * Procesamiento asíncrono " @ @ D *> *= AB AB AB @ M *- B AB B R B M AB AB @ @ @B G 6 @ ? @ AB F B @ AB B AB B *$ AB @ M B AB * ( B G @ @ AB @ M @ $ @ $ D *> B B @ @ AB @ M @ T U AB B B B @ 6 @ B @ F @ * G AB F AB T B@@ Z B YJ T B@@ B@@ ] B 8 B@@ 7 8 F * B $ 2 - - @ AB S G@ *K @ M @ import import import import import import import import import *E _ @ @ - S G B B @ Y 7 * - B MB - , G java.io.IOException; javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; tidw.asincservlet.MiClaseAsincrona; java.util.concurrent.ScheduledThreadPoolExecutor; javax.servlet.AsyncContext; Z * @WebServlet(name = "/MiPrimerServletAsincrono", urlPatterns = { "/asincrono" }, asyncSupported = true) public class MiPrimerServletAsincrono extends HttpServlet { private static final long serialVersionUID = 1L; public MiPrimerServletAsincrono() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub procesandoElRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub procesandoElRequest(request, response); } protected void procesandoElRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AsyncContext aCtx = request.startAsync(request, response); ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10); executor.execute(new MiClaseAsincrona(aCtx)); } } package tidw.asincservlet; import javax.servlet.AsyncContext; public class MiClaseAsincrona implements Runnable { AsyncContext ctx; @Override public void run() { try { // Ejecutaremos este hilo cada 6 segundos Thread.sleep(6000); System.out.println("Se lanza cada 6 segundos"); } catch (Exception ex) { ex.printStackTrace(); } ctx.complete(); } public MiClaseAsincrona(AsyncContext ctx) { this.ctx = ctx; } } Fragmentos web / B = G @ M S @ G -M B $ B B S @ -& AB M @ J @ M S S S P B B AB T @ *E B G S * 2$ -T,% T G B B B @ @ S S B *E *E *- Z Y Z Y Y T T YJ Y Z2 Z S *2 YJ T Z YJ T Z Z Z Y T YJ YJS T $ *E * U YS *E B @ G F @ S G * M AB @ AB -& $ O @ S B AB * Z2 ( YJ T Z Z Z F B B @ @ S F B@ * Otros. $ U 0 E @ AB ? G Q @ B U @ U F @B ^ 6 6 ? @ = @/ AB AB @ E@B 0 * Ejemplo 1 @WebServlet(name="CabecerasServlet", urlPatterns={"/cabeceras"}) public class CabecerasServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try{ out.println("<html>"); @ E out.println("<head>"); out.println("<title>Cabeceras Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1> Cabeceras: </h1>"); out.println("<ul>"); Enumeration<String> nombresDeCabeceras = request.getHeaderNames(); while (nombresDeCabeceras.hasMoreElements()) { String cabecera = nombresDeCabeceras.nextElement(); out.println("<li><b>" + cabecera + ": </b>" + request.getHeader(cabecera) + "</li>"); } out.println("</ul>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } } Ejemplo 2 B@ B @ = 2( B <form method="get" action="/solicitud" name="datos"> Nombre: <input name="nombre"><br> Apellidos: <input name="apellidos"><br> Edad: <select name="edad"> <option>Menor de 18</option> <option>De 18 a 30</option> <option>De 30 a 55</option> <option>Mayor de 55</option> </select> <br> Hobbies:<br> <input name="hobbies" value="lectura" type="checkbox">lectura<br> <input name="hobbies" value="futbol" type="checkbox">verfutbol<br> <input name="hobbies" value="deporte" type="checkbox">jugar tenis<br> <br> <button>Enviar</button></form> @ @ B @WebServlet(name=" FormularioServlet ", urlPatterns={"/solicitud"}) public class FormularioServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String nombre = request.getParameter("nombre"); String apellidos = request.getParameter("apellidos"); String edad = request.getParameter("edad"); String[] hobbies = request.getParameterValues("hobbies"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet que procesa una solicitud</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>" + "Hola " + nombre + " " + apellidos+ "</h1>"); out.println("Tu Franja de edad es " + edad + " y tus hobbies son:"); } out.println("<ul>"); for (String hobby : hobbies) { out.println("<li>" + hobby + "</li>"); } out.println("</ul>"); out.println("Esta solicitud ha sido invocado con Los siguientes parametros:<br/>"); out.println(request.getQueryString()); out.println("</body>"); out.println("</html>"); } finally { out.close(); }