-
-
Save wagnerluis1982/ef23ee1f99f3e967fb52f2c5a5c879e4 to your computer and use it in GitHub Desktop.
Solução para a 1ª Avaliação (Questão 3): Estratégia 1
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; | |
@WebServlet(name = "AV1 Q3 E1", urlPatterns = {"/av131"}) | |
public class Av131Servlet extends HttpServlet { | |
/** | |
* Constante para auxiliar no processamento | |
*/ | |
private static final String[] LINGUAGENS = new String[] {"java", "python", "ruby", "php", "c", "haskell", "julia"}; | |
@Override | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
RequestDispatcher rd = request.getRequestDispatcher("/linguagens-form1.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 cada linguagem e define os marcados | |
if (escolhidas != null) { | |
for (String linguagem : LINGUAGENS) { | |
Cookie ck = new Cookie(linguagem, null); | |
// Marca o cookie se a linguagem foi escolhida | |
if (isEscolhida(escolhidas, linguagem)) { | |
ck.setValue("marcado"); | |
} | |
// Linguagem não foi escolhida: remove o cookie! | |
else { | |
ck.setMaxAge(0); | |
} | |
response.addCookie(ck); | |
} | |
// Mensagem de sucesso :-) | |
response.getWriter().println("Sucesso"); | |
} | |
// Nenhuma opção escolhida, remove todos os cookies de linguagem | |
else { | |
for (String linguagem : LINGUAGENS) { | |
Cookie ck = new Cookie(linguagem, null); | |
ck.setMaxAge(0); | |
response.addCookie(ck); | |
} | |
// Mensagem de aviso | |
response.getWriter().println("Todos os cookies removidos"); | |
} | |
} | |
/** | |
* Método auxiliar para identificar se uma opção de linguagem está presente na lista de escolhidas | |
*/ | |
private static boolean isEscolhida(String[] escolhidas, String opcao) { | |
for (String name : escolhidas) { | |
if (name.equals(opcao)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} |
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
<%@ page contentType="text/html;charset=UTF-8" language="java" %> | |
<html> | |
<head> | |
<title>Linguagens Preferidas</title> | |
</head> | |
<body> | |
<h1>Estratégia 1</h1> | |
<p>Utilizando um cookie para cada item da lista.</p> | |
<form method="post"> | |
Linguagens preferidas: <br> | |
<input type="checkbox" name="linguagem" value="java" | |
${cookie['java'].value == 'marcado' ? 'checked' : ''}> Java <br> | |
<input type="checkbox" name="linguagem" value="python" | |
${cookie['python'].value == 'marcado' ? 'checked' : ''}> Python <br> | |
<input type="checkbox" name="linguagem" value="ruby" | |
${cookie['ruby'].value == 'marcado' ? 'checked' : ''}> Ruby <br> | |
<input type="checkbox" name="linguagem" value="php" | |
${cookie['php'].value == 'marcado' ? 'checked' : ''}> PHP <br> | |
<input type="checkbox" name="linguagem" value="c" | |
${cookie['c'].value == 'marcado' ? 'checked' : ''}> C <br> | |
<input type="checkbox" name="linguagem" value="haskell" | |
${cookie['haskell'].value == 'marcado' ? 'checked' : ''}> Haskell <br> | |
<input type="checkbox" name="linguagem" value="julia" | |
${cookie['julia'].value == 'marcado' ? '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