Home>

If i try to call ExecuteLogin from the following jsp, it will be 404.
How can I improve it?
]
Login.jsp code

<% @ page language = "java" contentType = "text/html;charset = UTF-8"
    pageEncoding = "UTF-8"%>

<! DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title>Login screen</title>
</head>
<body>
 Login screen 
<form action = "<% = request.getContextPath ()%>/ExecuteLogin" method = "post">
<p>User ID:
    <input type = "text" name = "USER_ID" maxlength = "20">
</p>
<p>Password:
    <input type = "text" name = "PASSWORD" maxlength = "20">
</p>
<input type = "submit" value = "login">
</form>
</body>
</html>


ExecuteLogin code

package controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.BusinessLogic;
import model.UserInfoDto;
public class ExecuteLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ExecuteLogin () {
        super ();
    }
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost (request, response);
    }

    protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession ();
        UserInfoDto dto = (UserInfoDto) session.getAttribute ("LOGIN_INFO");
        if (dto! = null) {
            response.sendRedirect ("/ WebContent/WEB-INF/view/Input.jsp");
        } else {
        String userId = request.getParameter ("USER_ID");
        String passWord = request.getParameter ("PASSWORD");
        // BusinessLogic call (information verification)
        BusinessLogic logic = new BusinessLogic ();
        UserInfoDto userInfoDto = logic.executeLoginCheck (userId, passWord);
        // sort by result
        if (userInfoDto.getUserId ()! = null) {
            session.setAttribute ("LOGIN_INFO", userInfoDto);
            request.setAttribute ("LOGIN_INFO", userInfoDto);
            RequestDispatcher dispatch = request.getRequestDispatcher ("/ WebContent/view/Input.jsp");
            dispatch.forward (request, response);
            response.sendRedirect ("/ WebContent/view/Input.jsp");
        } else {
            response.sendRedirect ("/ WebContent/view/Login.jsp");
                }
            }
        }
    }
  • Answer # 1

    First, there is no need to describe/WebContent.
    This is because the JavaWeb application of Eclipse is the directory where/WebContent places files for Web publishing.


    Even if/WebContent is deleted, an error appears,

    if (dto! = null) {
       response.sendRedirect ("/ WebContent/WEB-INF/view/Input.jsp");
    } else {
       ....
    }
    In

    , dto is not null, so it redirects, but resources in WEB-INF cannot be referenced if they are not servlet resources registered in web.xml etc.

    The reason for this is that the redirect returns a response to the browser once, the command "Request immediately to the specified URL" is returned, and the browser then directs /WEB-INF/view/Input.jsp directly to the browser. Will be in the same state as entered in the address bar.

    There are two ways to solve this:

    Register Input.jsp in web.xml

    Forward to servlet to display Input.jsp

    To forward, get RequestDispatcher and use forward.

  • Answer # 2

    M.ts10806's question is an answer or a hint, but it may be difficult to start studying.
    Because you have just started studying, it is better to proceed while understanding the meaning of the code.

    Learn the difference between redirect and forward

      

    Can I access the browser address directly?

      

    Is it impossible with this writing?

    You cannot redirect to a location that cannot be accessed with a browser.