Rails | Java Servlet + JSP | |
---|---|---|
Model | XXX.rb | XXX.java |
View | XXX.erb | XXX.jsp |
Controller | XXXController.rb | XXXServlet.java |
routing | routes.Specified by rb | XXXServlet.Specified by java annotation |
Rails Specified in routes.rb
routes.rb
Rails.application.routes.draw do
get '/sample' => 'Controller name#Action name'
post '/sample' => 'Controller name#Action name'
end
Java Servlet + JSP Specified in XXXServlet.java annotation
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("Transition destination file");
dispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
RequestDispatcher dispatcher = request.getRequestDispatcher("Transition destination file");
dispatcher.forward(request, response);
}
}
Example: Login / log-off function
--You can access the ** INDEX page ** by typing http: // localhost: 8080 / SampleApp
in your browser
-Click the to INDEX PAGE
link on the ** INDEX page ** to jump to the ** LOGIN page **
-Enter any name
in the name field of ** LOGIN page ** and 1234
in the password field, and click the submit
button to jump to the ** MAIN page **.
--Even if you enter characters other than 1234
in the password field and click the submit
button, you will not be taken to the ** MAIN page **.
-Click the to LOG OUT
link on the ** MAIN page ** to log out and return to the ** LOG IN page **
INDEX page
LOGIN page
MAIN page
item | value | Remarks |
---|---|---|
PC | MacBook Air | |
OS | MacOS Catalina | |
language | Java | * Use Java EE |
IDE | Eclipse | ※Eclipse 2019_09《Pleiades All in One Eclipse Download》Download from |
Model to hold user information
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;
}
}
Class that processes login information
LoginLogic.java
package model;
public class LoginLogic {
public boolean execute(User user) {
if(user.getPassword().equals("1234")){
return true;
}
return false;
}
}
Controller to handle login
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");
}
}
}
Controller to handle logout
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");
}
}
View to display ** INDEX page **
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>
View to display ** LOGIN page **
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>
View to display ** MAIN page **
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>
-[Introduction to Servlet & JSP (Refreshing Series)](https://www.amazon.co.jp/%E3%82%B9%E3%83%83%E3%82%AD%E3%83%AA% E3% 82% 8F% E3% 81% 8B% E3% 82% 8B-% E3% 82% B5% E3% 83% BC% E3% 83% 96% E3% 83% AC% E3% 83% 83% E3 % 83% 88-JSP% E5% 85% A5% E9% 96% 80-% E3% 82% B9% E3% 83% 83% E3% 82% AD% E3% 83% AA% E3% 82% B7% E3% 83% AA% E3% 83% BC% E3% 82% BA-% E5% 9B% BD% E6% 9C% AC / dp / 4844335804)]
Recommended Posts