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.
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.
In this article, we will create a login function.
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.
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>
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
.
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
}
login-result.jsp
showing the login resultCreate 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>
loginUser
is not null (if the password input is" hoge ")loginUser.name
For conditions other than 3.1On the user login screen, enter hoge
or something else as the password and the login result will be displayed firmly and it is successful.
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