I would like to write an article for those who are writing applications for the first time using Java. We would appreciate it if you could use it as a reference for creating training assignments for your portfolio and company. This time, we will create a task manager. By applying this, you can also use it to create Twitter clones.
I will post articles for each function of the application.
eclipse4.16.0 Tomcat9 Java11
login.jsp
<body>
<!--If there is an input error, this page will be reopened so we will let you know why the error occurred. Receives the value of request scope data name error-->
<%
List<String> error = (List<String>)request.getAttribute("error");
%>
<div class="container mt-5">
<div class="row justify-content-center">
<h3>Login screen</h3>
</div>
<!--Display error in red.-->
<%for(String er : error){ %>
<div class="row justify-content-center">
<p style="color:red"><%=er %></p>
</div>
<%} %>
<div class="row justify-content-center">
<!-- login-Input data (user) by specifying servlet_id,password)-->
<form action="login-servlet" method="post">
<div class="form-group">
<label>User ID</label>
<input type="text" name="user_id" class="form-control" placeholder="User-ID">
</div>
<div class="form-group">
<label>password</label>
<input type="password" name="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
<small class="form-text text-muted">We'll never share your Password with anyone else.</small>
</div>
<button type="submit" class="btn btn-outline-info">Login</button>
</form>
</div>
</div>
We will define methods in DAO to make the Servlet cleaner and easier to change later.
model.dao.UserDAO.java
/**
*This method is used to get all the user information from the entered user ID.
*Method to get user information with user ID as an argument
* @param user_id
* @return User information
* @throws SQLException
* @throws ClassNotFoundException
*/
public UserBean getUser(String user_id) throws SQLException, ClassNotFoundException {
UserBean user = new UserBean();
//SQL statement user_Get record by specifying id
String sql = "select * from m_user where user_id = ?";
//Connect to database
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
//Received user_Assign id to SQL statement
pstmt.setString(1, user_id);
//Execute SQL statement and get execution result
ResultSet res = pstmt.executeQuery();
while(res.next()) {
//Get the value of each column from the execution result
String password = res.getString("password");
String user_name = res.getString("user_name");
//Set to user object
user.setPassword(password);
user.setUser_id(user_id);
user.setUser_name(user_name);
}
return user;
}
}
/**
*Method to get user name with user ID as argument
* @param user_id
* @return user name
* @throws SQLException
* @throws ClassNotFoundException
*/
public String getUserName(String user_id) throws SQLException, ClassNotFoundException {
//SQL statement User_user from id_Get name
String sql = "select user_name from m_user where user_id = ?";
//usre_Set the initial value of name
String user_name ="unknown";
//Connect to database
try(Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
//Received user_Assign id to SQL statement
pstmt.setString(1, user_id);
//Execute SQL statement and get execution result
ResultSet res = pstmt.executeQuery();
while(res.next()) {
//Get the value of each column from the execution result
user_name = res.getNString("user_name");
}
}
return user_name;
}
/**
*Login authentication method
* @param user_id
* @param password
* @If it is true with return boolean type, you can log in. If false, you cannot log in.
* @throws SQLException
* @throws ClassNotFoundException
*/
public boolean logincheck(String user_id, String password) throws SQLException, ClassNotFoundException {
//Exclude the case where the input field is blank in the first place
if (user_id == null || user_id.length() == 0 || password == null || password.length() == 0){
return false;
}
//SQL statement entered user_id,Get record by specifying password
String sql = "select * from m_user where user_id = ? && password = ?";
//Connect to database
try (Connection con = ConnectionManager.getConnection();
PreparedStatement pstmt = con.prepareStatement(sql)){
pstmt.setString(1, user_id);
pstmt.setString(2, password);
ResultSet res = pstmt.executeQuery();
//If there is an execution result of the SQL statement, it is true because the user exists. No result means false because the user is not registered
if(res.next()) {
return true;
} else {
return false;
}
}
}
We will create a Servlet using the method created by DAO.
Servlet.Login.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//Receiving request parameters
request.setCharacterEncoding("UTF-8");
String user_id = request.getParameter("user_id");
String password = request.getParameter("password");
//DAO object generation
UserDAO userdao = new UserDAO();
//session scope generation
HttpSession session = request.getSession();
try {
boolean logincheck = userdao.logincheck(user_id, password);
if(logincheck) {
//-----------Processing when the input is correct---------------------------------------------------
//Set login status to session scope
session.setAttribute("login", true);
//Get login user name and information and set to session scope
String current_user_name = userdao.getUserName(user_id);
UserBean current_user = userdao.getUser(user_id);
session.setAttribute("current_user_name", current_user_name);
session.setAttribute("current_user", current_user);
session.setAttribute("current_user_id", user_id);
//Transfer to menu session
RequestDispatcher rd = request.getRequestDispatcher("menu-servlet");
rd.forward(request, response);
} else {
//--------Processing when the input is different-----------------------------------------------------------
//Check blank
List<String> error = new ArrayList<String>();
if(password.equals("")) {
error.add("Password is blank");
}
if(user_id.equals("")) {
error.add("User ID is blank");
}
error.add("There is an error in the user ID or password");
request.setAttribute("error", error);
//Set login status to session scope (authentication not possible)
session.setAttribute("login", false);
//Transfer to login screen
RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
//---------Other errors such as SQL--------------------------------------------------------------
}catch(SQLException | ClassNotFoundException e) {
e.printStackTrace();
RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
}
}
This time, I implemented the login function. Next time, we will implement the task registration function.
Recommended Posts