mirror of
https://github.com/kmitresse/Cards-Rush.git
synced 2026-05-16 09:05:28 +00:00
draft(DevWeb): Rooting by servlet
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* LoginServlet.java, 20/03/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.servlet;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "indexServlet", value = "/")
|
||||
public class IndexServlet extends HttpServlet {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
if (request.getSession().getAttribute("user") != null) {
|
||||
response.sendRedirect(request.getContextPath() + "/dashboard");
|
||||
return;
|
||||
}
|
||||
|
||||
response.sendRedirect(request.getContextPath() + "/login");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
/*
|
||||
* LoginServlet.java, 20/03/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.servlet;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
@@ -15,9 +22,9 @@ import uppa.project.dao.DAOException;
|
||||
import uppa.project.dao.jpa.Game_JPA_DAO_Factory;
|
||||
import uppa.project.servlet.json.ErrorApi;
|
||||
import uppa.project.pojo.User;
|
||||
import uppa.project.servlet.utils.RequestUtils;
|
||||
import uppa.project.servlet.utils.HttpRequestUtils;
|
||||
|
||||
@WebServlet(name = "loginApiServlet", value = "/api/login")
|
||||
@WebServlet(name = "loginServlet", value = "/login")
|
||||
public class LoginServlet extends HttpServlet {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
@@ -25,6 +32,10 @@ public class LoginServlet extends HttpServlet {
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
|
||||
}
|
||||
|
||||
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
@@ -33,7 +44,7 @@ public class LoginServlet extends HttpServlet {
|
||||
// Convert the string to a JSON object
|
||||
JsonObject jsonBody;
|
||||
try {
|
||||
String requestBody = RequestUtils.getRequestBody(request);
|
||||
String requestBody = HttpRequestUtils.getRequestBody(request);
|
||||
jsonBody = JsonParser.parseString(requestBody).getAsJsonObject();
|
||||
} catch (Exception e) {
|
||||
int STATUS = 400;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* LoginServlet.java, 20/03/2024
|
||||
* UPPA M1 TI 2023-2024
|
||||
* Pas de copyright, aucun droits
|
||||
*/
|
||||
|
||||
package uppa.project.servlet;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.annotation.WebServlet;
|
||||
import jakarta.servlet.http.HttpServlet;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
@WebServlet(name = "indexServlet", value = "/")
|
||||
public class MainMenuServlet extends HttpServlet {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
public void init() {
|
||||
}
|
||||
|
||||
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
||||
|
||||
if (request.getSession().getAttribute("user") != null) {
|
||||
request.getRequestDispatcher("/WEB-INF/mainMenu.jsp").forward(request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
response.sendRedirect(request.getContextPath() + "/login");
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: lucas
|
||||
Date: 20/03/2024
|
||||
Time: 16:47
|
||||
To change this template use File | Settings | File Templates.
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
+5
-1
@@ -15,7 +15,10 @@
|
||||
<div class="flex-column login-gap">
|
||||
<div>
|
||||
<h1>Login</h1>
|
||||
<form id="login-form" data-login-endpoint="api/login" action="dashboard.jsp" method="POST">
|
||||
<form id="login-form"
|
||||
data-login-endpoint="${pageContext.request.contextPath}/login"
|
||||
action="${pageContext.request.contextPath}/login"
|
||||
method="POST">
|
||||
|
||||
<label id="username-label" for="username">Username:</label>
|
||||
<input type="text" id="username" name="username" required>
|
||||
@@ -28,6 +31,7 @@
|
||||
</form>
|
||||
</div>
|
||||
<div>
|
||||
<p><a href="forgottenPassword.jsp">Forgotten password?</a></p>
|
||||
<hr>
|
||||
<p>Don't have an account? <a href="register.jsp">Register</a></p>
|
||||
</div>
|
||||
+1
-1
@@ -34,7 +34,7 @@
|
||||
<th>Game won</th>
|
||||
<th>Invite</th>
|
||||
</tr>
|
||||
<%-- Récuperer les joueurs connecter et les lister sous forme de tableau--%>
|
||||
<%-- Récuperer les joueurs connecter et les lister sous forme de tableau--%>
|
||||
</table>
|
||||
</div>
|
||||
</section>
|
||||
@@ -1,25 +0,0 @@
|
||||
<%@ page import="static jdk.internal.net.http.common.Utils.dump" %>
|
||||
<%@ page import="uppa.project.pojo.User" %>
|
||||
<%@ page import="uppa.project.dao.jpa.Game_JPA_DAO_Factory" %>
|
||||
<%@ page import="uppa.project.dao.DAO" %>
|
||||
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
|
||||
|
||||
<%
|
||||
User user = (User) (request.getAttribute("session"));
|
||||
%>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>JSP - Hello World</title>
|
||||
</head>
|
||||
<body>
|
||||
<% if (user == null) { %>
|
||||
<%-- Pas de user connecté -> On envoi sur la page de connection --%>
|
||||
<jsp:include page="login.jsp"></jsp:include>
|
||||
<% } else {
|
||||
session.setAttribute("sessionUser", session);
|
||||
%>
|
||||
<jsp:include page="dashboard.jsp"></jsp:include>
|
||||
<% } %>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user