0% found this document useful (0 votes)
120 views5 pages

Guía de Uso de Cookies JSP

The document describes how to use cookies in a Java servlet. It includes an HTML form to input a product code and name. The servlet gets these values from the request, creates a cookie, and adds it to the response. It then gets any existing cookies from the request and outputs the codes and names in a table.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
120 views5 pages

Guía de Uso de Cookies JSP

The document describes how to use cookies in a Java servlet. It includes an HTML form to input a product code and name. The servlet gets these values from the request, creates a cookie, and adds it to the response. It then gets any existing cookies from the request and outputs the codes and names in a table.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Cookies.

html <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Uso de Cookies</h1> <hr color="plate" > <form name="form1" action="miCookies" method="post"> <table border="1" cellspacing="0" cellpadding="0" align="center"> <tr><td>C&oacute;digo</td><td><input type="text" name="txtCodigo" value="102435c"></td></tr> <tr><td>Nombre</td><td><input type="text" name="txtNombre" value="azucar"></td></tr> <tr><td colspan="2" align="center"><input type="submit" name="enviar" value="Procesar"></td></tr> </table> </form> </body> </html>

Cdigo servlets miCookies /** * * @author Administrador */ public class miCookies extends HttpServlet {

/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { [Link]("text/html;charset=UTF-8"); PrintWriter out = [Link](); try {

String codigo= [Link]("txtCodigo"); String nombre= [Link]("txtNombre");

[Link]("<html>"); [Link]("<head>"); [Link]("<title>Servlet miCookies</title>");

[Link]("</head>"); [Link]("<body>"); [Link]("<h1>Resultado miCookies at " + [Link] () + "</h1>"); [Link]("<hr color=plate>");

Cookie productos =new Cookie(codigo,nombre); [Link](3600); [Link](productos); Cookie contenedorCookies[]= [Link]();

[Link]("<table align=center border=1 bordercolor=plate><tr><td>Codigo</td><td>Nombre</td></tr>"); for(int i=([Link]-2);i>=1;i--) {

[Link]("<tr><td>"+contenedorCookies[i].getName()+"</td><td>"+contenedorCookies[i].getVa lue()+"</td></tr>"); } [Link]("<tr><td colspan=2><form name=form1 methos=post, action=[Link]><input type=submit name=enviar value= Agregar_producto></form></td></tr>"); [Link]("</table>"); [Link]("</body>"); [Link]("</html>");

} finally { [Link]();

} }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }

/** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }

/** * Returns a short description of the servlet. * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold>

You might also like