Rails | Java Servlet + JSP | |
---|---|---|
Model | XXX.rb | XXX.java |
View | XXX.erb | XXX.jsp |
Controller | XXXController.rb | XXXServlet.java |
routage | routes.Spécifié par rb | XXXServlet.Spécifié par l'annotation java |
Rails Spécifié dans routes.rb
routes.rb
Rails.application.routes.draw do
get '/sample' => 'Nom du contrôleur#Nom de l'action'
post '/sample' => 'Nom du contrôleur#Nom de l'action'
end
Java Servlet + JSP Spécifié dans l'annotation de XXXServlet.java
XXXSerblet.java
package src;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/sample")
public class XXXServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
RequestDispatcher dispatcher = request.getRequestDispatcher("Fichier de destination de la transition");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
RequestDispatcher dispatcher = request.getRequestDispatcher("Fichier de destination de la transition");
dispatcher.forward(request, response);
}
}
Exemple: fonction de connexion / déconnexion
http: // localhost: 8080 / SampleApp
dans votre navigateur.
-Cliquez sur le lien to INDEX PAGE
sur la ** page INDEX ** pour accéder à la ** page LOGIN **
-Entrez «tout nom» dans le champ de nom de la ** page CONNEXION ** et «1234» dans le champ de mot de passe, et cliquez sur le bouton «soumettre» pour passer à la ** page PRINCIPALE **.Sommaire
Page de connexion
Page d'accueil
article | valeur | Remarques |
---|---|---|
PC | MacBook Air | |
OS | MacOS Catalina | |
Langue | Java | * Utilisez Java EE |
IDE | Eclipse | ※Eclipse 2019_09《Pléiades All in One Eclipse Télécharger》Télécharger à partir de |
Modèle pour conserver les informations utilisateur
User.java
package model;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private String password;
public User() {};
public User(String name,String password){
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
}
Classe qui traite les informations de connexion
LoginLogic.java
package model;
public class LoginLogic {
public boolean execute(User user) {
if(user.getPassword().equals("1234")){
return true;
}
return false;
}
}
Contrôleur pour gérer la connexion
Login.java
package src;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.LoginLogic;
import model.User;
@WebServlet("/Login")
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("name");
String password = request.getParameter("password");
User user = new User(userName,password);
LoginLogic loginLogic = new LoginLogic();
boolean isLogin = loginLogic.execute(user);
if(isLogin==true) {
HttpSession session = request.getSession();
session.setAttribute("loginUser",user);
response.sendRedirect("/SampleApp/Main");
} else {
response.sendRedirect("/SampleApp/Login");
}
}
}
Contrôleur pour gérer la déconnexion
Logout.java
package src;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/Logout")
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("/SampleApp/Login");
}
}
Afficher pour afficher ** Page INDEX **
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>INDEX PAGE | SAMPLE APP</title>
</head>
<body>
<h1>SAMPLE APP</h1>
<h2>INDEX PAGE</h2>
<a href="/SampleApp/Login">to LOGIN PAGE</a>
</body>
</html>
Afficher pour afficher ** Page de CONNEXION **
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>LOGIN FORM | SAMPLE APP</title>
</head>
<body>
<h1>SAMPLE APP</h1>
<h2>LOGIN FORM</h2>
<form action="/SampleApp/Login" method="post">
<p>Name:</p>
<input type="text" name="name">
<p>PassWord:</p>
<input type="password" name="password"><br>
<input type="submit" value="submit"><br>
<br>
</form>
<br>
<a href="/SampleApp/">to INDEX PAGE</a>
</body>
</html>
Afficher pour afficher ** Page PRINCIPALE **
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="model.User" %>
<%
User loginUser = (User)session.getAttribute("loginUser");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><%= loginUser.getName() %> | SAMPLE APP</title>
</head>
<body>
<h1>SAMPLE APP</h1>
<p>Hello,<%= loginUser.getName() %>!</p>
<h2>Articles posted by <%= loginUser.getName() %></h2>
<a href="/SampleApp/Logout">to LOG OUT</a>
</body>
</html>
Recommended Posts