-
-
Save wagnerluis1982/965c1ade88279eb7bb13f3670bfbd831 to your computer and use it in GitHub Desktop.
Solução para a 1ª Avaliação (Questão 3): Estratégia 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fanese.web; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.Cookie; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.Arrays; | |
import java.util.List; | |
@WebServlet(name = "AV1 Q3 E2", urlPatterns = {"/av132"}) | |
public class Av132Servlet extends HttpServlet { | |
@Override | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// Obtém o cookie de linguagens | |
Cookie ckLinguagens = getCookie(request, "linguagens"); | |
// Passa a lista de linguagens presente no cookie para o JSP | |
String escolhidas = ckLinguagens.getValue(); | |
if (escolhidas != null) { | |
// Cria um array com a lista de escolhidas | |
String[] arrayEscolhidas = escolhidas.split(":"); | |
// Passando o atributo "escolhidas" no escopo request | |
List<String> listaEscolhidas = Arrays.asList(arrayEscolhidas); | |
request.setAttribute("escolhidas", listaEscolhidas); | |
} | |
RequestDispatcher rd = request.getRequestDispatcher("/linguagens-form2.jsp"); | |
rd.forward(request, response); | |
} | |
@Override | |
protected void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws IOException { | |
// Obtém as linguagens escolhidas | |
String[] escolhidas = request.getParameterValues("linguagem"); | |
// Cria um cookie para as linguagens escolhidas | |
if (escolhidas != null) { | |
// Cria string única para identificar as linguagens escolhidas | |
// ex: "java:python:ruby" | |
String chaveEscolhidas = String.join(":", escolhidas); | |
// Escreve a string única no cookie | |
Cookie ck = new Cookie("linguagens", chaveEscolhidas); | |
response.addCookie(ck); | |
// Mensagem de sucesso :-) | |
response.getWriter().println("Sucesso"); | |
} | |
// Nenhuma opção escolhida, remove o cookie de linguagens | |
else { | |
Cookie ck = new Cookie("linguagens", null); | |
ck.setMaxAge(0); | |
response.addCookie(ck); | |
// Mensagem de aviso | |
response.getWriter().println("Cookie 'linguagens' removido"); | |
} | |
} | |
/** | |
* Obtém um cookie da requisição ou cria um novo | |
*/ | |
private static Cookie getCookie(HttpServletRequest request, String name) { | |
Cookie[] cookies = request.getCookies(); | |
if (cookies != null) { | |
for (Cookie ck : cookies) { | |
if (name.equals(ck.getName())) { | |
return ck; | |
} | |
} | |
} | |
return new Cookie(name, null); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | |
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>Linguagens Preferidas</title> | |
</head> | |
<body> | |
<h1>Estratégia 2</h1> | |
<p>Utilizando um único cookie para identificar os itens marcados.</p> | |
<c:set var="escolhidas" value="${requestScope.escolhidas}"/> | |
<form method="post"> | |
Linguagens preferidas: <br> | |
<input type="checkbox" name="linguagem" value="java" | |
${escolhidas.contains('java') ? 'checked' : ''}> Java <br> | |
<input type="checkbox" name="linguagem" value="python" | |
${escolhidas.contains('python') ? 'checked' : ''}> Python <br> | |
<input type="checkbox" name="linguagem" value="ruby" | |
${escolhidas.contains('ruby') ? 'checked' : ''}> Ruby <br> | |
<input type="checkbox" name="linguagem" value="php" | |
${escolhidas.contains('php') ? 'checked' : ''}> PHP <br> | |
<input type="checkbox" name="linguagem" value="c" | |
${escolhidas.contains('c') ? 'checked' : ''}> C <br> | |
<input type="checkbox" name="linguagem" value="haskell" | |
${escolhidas.contains('haskell') ? 'checked' : ''}> Haskell <br> | |
<input type="checkbox" name="linguagem" value="julia" | |
${escolhidas.contains('julia') ? 'checked' : ''}> Julia <br> | |
<br> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment