Comparison of WEB application development with Rails and Java Servlet + JSP

Comparison of WEB application development with Rails and Java Servlet + JSP

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

How to specify the routing

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);

	}

}

WEB application development with Java Servlet + JSP

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 スクリーンショット 2019-11-17 13.41.34.png

LOGIN page スクリーンショット 2019-11-17 13.42.12.png

MAIN page スクリーンショット 2019-11-17 13.42.30.png

Development environment

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

Code example

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>

Reference material

-[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

Comparison of WEB application development with Rails and Java Servlet + JSP
Roughly the flow of web application development with Rails.
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
Role of JSP in Web application [Java]
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
Hello World with Java Servlet and JSP (Easy web server startup with Maven + Jetty)
Rails web server and application server
Java web application development environment construction with VS Code (struts2)
Until you create a Web application with Servlet / JSP (Part 1)
Reasons to use Servlet and JSP separately in Java development
Start web application development with Spring Boot
[Java / Swift] Comparison of Java Interface and Swift Protocol
Creating a java web application development environment with docker for mac part1
Create a java web application development environment with docker for mac part2
[Java / Eclipse / Servlet / JSP / PostgreSQL] A WEB application framework with data posting / saving / editing / updating / deleting functions
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
Basic Web application creation Servlet / JSP (login function)
Basic Web application creation Servlet / JSP (posting screen)
This and that of conditional branching of rails development
Rails development environment created with VSCode and devcontainer
Basic Web application creation Servlet / JSP (logout function)
[Rails] Implementation of drag and drop function (with effect)
Summary of ToString behavior with Java and Groovy annotations
The comparison of enums is ==, and equals is good [Java]
Experienced Java users get started with Android application development
[Java] String comparison and && and ||
[Tutorial] Download Eclipse → Run Web application with Java (Pleiades)
[Rails] Development with MySQL
[Rough explanation] How to separate the operation of the production environment and the development environment with Rails
[Development] Java framework comparison
Create a JAVA WEB application and try OMC APM
Equivalence comparison of Java wrapper classes and primitive types
[Java] Development with multiple files using package and import
Story of making a task management application with swing, java
Web application development environment construction in Java (for inexperienced people)
Window aggregation of sensor data with Apache Flink and Java 8
[Java] Comparison method of character strings and comparison method using regular expressions
Kotlin post- and pre-increment and operator overload (comparison with C, Java, C ++)
Comparison of Web App for Containers and Azure Container Instances
Rails application development environment construction with Docker [Docker, Rails, Puma, Nginx, MySQL]
Try developing a containerized Java web application with Eclipse + Codewind
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Java: Start WAS with Docker and deploy your own application
Domain Driven Development with Java and Spring Boot ~ Layers and Modules ~
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
[Java] Beginner's understanding of Servlet-②
[Java] Beginner's understanding of Servlet-①
Web application development article summary
Java Servlet / JSP View drawing
Getting Started with JSP & Servlet
Web application built with docker (1)
Advantages and disadvantages of Java
Of the three Java embedded web servers, Tomcat, Jetty, and Undertow, which one worked with GraalVM?
Spring5 MVC Web application development with Visual Studio Code Environment construction (Installation of JDK11, Maven, Tomcat, Visual Studio Code)
Graph the sensor information of Raspberry Pi in Java and check it with a web browser
Procedure for building a Rails application development environment with Docker [Rails, MySQL, Docker]
Basics of Java development ~ How to write programs (variables and types) ~
Exciting personal development with Rails Part 1: Try designing specifications and DB
Install Rails in the development environment and create a new application
Comparison of processing times based on awk, shell command, and Java