[JAVA] Basic Web application creation Servlet / JSP (login function)

Introduction

Create a WEB application that allows you to post like a bulletin board with Servlet and JSP. First, create JDBC, DB, and Mysql without using them.

Execution environment

About function

Let's think about the function to be implemented this time. Since we will create an application like a bulletin board, we will create the following functions.

―― 1. Login function ―― 2. Logout function ―― 3. Posting function ―― 4. Post viewing function

This time, I will make it a simple configuration with the goal of getting used to creating web applications using Sevlet and JSP.

Login function creation

In this article, we will create a login function.

1. Create user model

Since the login function requires a user, create a user model. It is assumed that the user model to be created has two pieces of information, a name and a password.

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(){     // 1.Get user name
		return this.name;
	}

	public String getPassword(){   // 2.Get password
		return this.password;
	}
}

Set the access modifier to private so that the member variables name (user name) and password (password) cannot be referenced by other classes. It also creates methods 1 and 2 to get the value set in the constructor.

2. Create View of top page

Now that we have created the model, we will create a view in JSP for the user to log in. First, create a simple view.

top-page.jsp


<form action="./login" method="post">
  <div>
    <label>username</label>
    <input type="text" name="userName">
  </div>
  <div>
    <label>password</label>
    <input type="text" name="userPassword">
  </div>
  <div>
    <input type="submit" value="Registration">
  </div>
</form>

3. Create a Model to perform login processing

LoginProcess.java


package model;

public class LoginProcess {
	public boolean execute(User user) {
		if (user.getPassword().equals("hoge")) {
			return true;
		}
		return false;
	}
}

Login process that returns true if the registered password is hoge.

4. Create a controller to handle login requests

login.java


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");  // 1
		String name = request.getParameter("userName");
		String password = request.getParameter("userPassword");
		User user = new User(name,password);  // 2

		LoginProcess loginProcess = new LoginProcess();
		boolean isLogin = loginProcess.isLogin(user);  // 3

		if(isLogin) {
			request.getSession().setAttribute("loginUser",user);  // 4
		}

		request.getRequestDispatcher("/WEB-INF/view/login-result.jsp").forward(request, response);  // 5
	}

  1. Get request parameters
  2. Set the name and password of the User class for the acquired parameters.
  3. Process whether the password is correct and initialize the boolean value
  4. Set User class information in session scope
  5. Forward to the view login-result.jsp showing the login result

5. Create a view that represents the login result

Create a jsp based on the loginUser specified in the session scope.

login-result.jsp


<c:choose>
  <c:when test="${loginUser != null}">  // 1
    <p>Login is complete.</p>
    <p>Hello!<c:out value="${loginUser.name}"/>Mr.</p> //2
  </c:when>
  <c:otherwise>  // 3
    <p>I failed to login.</p>
    <p>Please log in again.</p>
    <a href="./login">User login</a>  //4
  </c:otherwise>
</c:choose>
  1. If loginUser is not null (if the password input is" hoge ")
  2. Output the name of the User class with loginUser.name For conditions other than 3.1
  3. Link on the user registration screen

6. Run

On the user login screen, enter hoge or something else as the password and the login result will be displayed firmly and it is successful.

Finally

Next time, we will check whether you are logged in or not on the posting screen and what to do if you are not.

Recommended Posts

Basic Web application creation Servlet / JSP (login function)
Basic Web application creation Servlet / JSP (logout function)
Basic Web application creation Servlet / JSP (posting screen)
[Spring Boot] Web application creation
Web application development memo with MVN, Tomcat, JSP / Servlet with VScode
Until you create a Web application with Servlet / JSP (Part 1)
Web application creation with Nodejs with Docker
Comparison of WEB application development with Rails and Java Servlet + JSP
Login function
Role of JSP in Web application [Java]
A certain programming classroom Ruby basic application function
Personal application creation # 2
Personal application creation # 3
Personal application creation # 1
[Servlet] Basic method
Rails application guest login function implemented (devise not used)