Home>

Nice to meet you.
I am currently learning Java.

I am currently trying to implement a login function.

However, I don't quite understand the description of the servlet for passing the user ID and password information received from JSP to DAO.

          // Servlet initialization parameter information
            String userid = request.getParameter("loginid");
            String password = request.getParameter("userpassword");

What kind of description should I make in order to pass the information received in to DAO?

If i know anything about how to describe it, I would appreciate it if you could teach me.
I am still learning, and honestly I don't know much. Therefore, corrections to the questions are also welcome.
Thank you very much.

<%@ page language="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8"%><%@ page import="common.LoggerTester" %><% request.setCharacterEncoding("UTF-8");%><% String loginErrorMessage = (String)request.getAttribute("loginErrorMessage");%><html><head>    <meta charset="UTF-8">        <title>Book management system-<Login></title>        <link rel="stylesheet" href="header.css"></head><header>​<p>Login to book management system</p>​<hr></header><body>                                    <table border="0">                                   Used to create a blank. It does not appear on the screen.                                                        Used to create a blank. It does not appear on the screen.                                 <%
                LoggerTester lt = new LoggerTester();
                lt.outActionLog("login.jsp", "start processing");
                %>            <% if(loginErrorMessage != null){ %>            <center><p><%= loginErrorMessage %></p></center>            <%} %>                                   Used to create a blank. It does not appear on the screen.                 <form method="POST" action="Login">                 <p>User ID<br>                 <input type="text" name="loginid" value="" size="24">                 <p>Password<br>                <input type="password" name="userpassword" value="" size="24">                <br>                 <input type="submit" value="login">              </form>          </table>        </body></html>
package exam;
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;
import dao.UserDAO;
@WebServlet("/Login")
public class LoginServ extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //Start session by getSession method
        HttpSession session = request.getSession();

        //Specify the character string encoding method received in the request
        request.setCharacterEncoding("UTF-8");
        LoggerTester lt = new LoggerTester();
        lt.outActionLog("Login", "Start processing");
        try {
            // Servlet initialization parameter information
            String userid = request.getParameter("loginid");
            String password = request.getParameter("userpassword");
            //Get login data from UserDAO
            UserDAO dao = new UserDAO();
            User user = dao.selectUser(loginid, userpassword);
            //if (request.getParameter("username") == un){
            if (user != null) {
            session.setAtribute("user", user);
            getServletContext().getRequestDispatcher("/BookManagementListDisp.java").forward(request, response);
            }else {
            request.setAttribute("loginErrorMessage","The input is incorrect.");
            getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);

            }
// In case of DB error, register error message in request and forward to login screen
            }catch(Exception e){
                e.printStackTrace();
                request.setAttribute("dbConnectionError", "There was an error with the database connection.");getServletContext().getRequestDispatcher("/errorMessage.jsp").forward(request, response);
            }
    }
}
package dao;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
public class UserDAO implements Serializable {
    // accessor method
    private String _loginid;
    private String _userpassword;
    private int _admin;
    private String _name;
    public void setloginid(String loginid) {
        this._loginid = loginid;
    }
    public String getloginid() {
        return this._loginid;
    }
    public void setuserpassword(String userpassword) {
        this._userpassword = userpassword;
    }
    public String getuserpassword() {
        return this._userpassword;
    }
    public void setadmin(int admin) {
        this._admin = admin;
    }
    public int getadmin() {
        return this._admin;
    }
    public void setname(String name) {
        this._userpassword = name;
    }
    public String getname() {
        return this._name;
    }

    // Get all the data registered in the table
    // Get the set information
    public UserDAO selectUser(String loginid, String userpassword)
Throws ClassNotFoundException, SQLException{
        UserDAO user = new UserDAO();
        // 1 user information
        UserDAO info = new UserDAO();
        info.setloginid("000014");
        info.setuserpassword("aiueo");
        info.setadmin(0);
        info.setname("aiueo");
        // set return value
        return UserList;
    }
}
  • Answer # 1

    Ahh. I finally understood.

    What I want to do is get the user ID ("000014" this time) and password ("aiueo" this time) sent by JSP POST and receive it by the servlet.

    I want to match it with the information that DAO has, and if the user ID and password match, I want to create session information on the servlet side.

    Please match the information on the DAO side (stub) instead of getting the information from DAO.

    public UserDAO selectUser(String loginid, String userpassword)
    Throws ClassNotFoundException, SQLException{
            // returns null if id or pass are different
            if(!"000014".equals(loginid) || !"aiueo".equals(userpassword))
                return null;
            UserDAO user = new UserDAO();
            // 1 user information
            UserDAO info = new UserDAO();
            info.setloginid("000014");
            info.setuserpassword("aiueo");
            info.setadmin(0);
            info.setname("aiueo");
            // set return value
            return info;

Related articles