Servlet 仅在默认开关情况下被调用。

huangapple go评论80阅读模式
英文:

Servlet is invoked only for the default switch case

问题

Here's the translated portion:

在这里当URL模式为"/Login/new"我通过打印到控制台验证Servlet未被调用它只对默认情况有效即对"/Login"

@WebServlet("/Login")
public class UserServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    private UserDAO userDAO;

    public void init() {
        userDAO = new UserDAO();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("Servlet invoked !!");
        String action = request.getServletPath();
        System.out.println(action);
        switch (action) {
            case "/new":
                showNewForm(request, response);
                break;
            case "/insert":
                insertUser(request, response);
                break;
            case "/update":
                updateUser(request, response);
                break;
            case "/delete":
                deleteUser(request, response);
                break;
            case "/edit":
                showEditForm(request, response);
                break;
            case "/login":
                RequestDispatcher dispatcher = request.getRequestDispatcher("/login.jsp");
                dispatcher.forward(request, response);

            default:
                // listUser(request,response);
                showLogin(request, response);
                break;
        }
    }
}

Please note that I've removed the HTML-encoded characters (e.g., ") for better readability.

英文:

Here when the url pattern is "/Login/new" the servlet is not invoked which I verified by printing to console.It works just for the default case that is for "/Login"

@WebServlet("/Login")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserDAO userDAO;
public void init()
{
userDAO = new UserDAO();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet invoked !!");
String action = request.getServletPath();
System.out.println(action);
switch(action)
{
case "/new":
showNewForm(request,response);
break;	
case "/insert":
insertUser(request, response);
break;
case "/update":
updateUser(request, response);
break;
case "/delete":
deleteUser(request, response);
break;
case "/edit":
showEditForm(request, response);
break;
case "/login":
RequestDispatcher dispatcher= request.getRequestDispatcher("/login.jsp");
dispatcher.forward(request, response);
default:
//listUser(request,response);
showLogin(request,response);
break;
}

Output for "/Login":[Servlet 仅在默认开关情况下被调用。

Output for "/Login/new":Servlet 仅在默认开关情况下被调用。

The desired output is that the function in "/new" case should be called.What am I missing?

答案1

得分: 1

更新路径到以下内容:

@WebServlet("/登录/*")

并尝试检查是否获取了URL路径的最后部分并将其转换为小写:

String action = request.getRequestURI();
action = action.substring(action.lastIndexOf("/")).toLowerCase();
英文:

Update the path to the following

@WebServlet("/Login/*")

, And try to check that you are retrieving the last part of the path to the URL and lower-case following

String action = request.getRequestURI();
action = action.substring(action.lastIndexOf("/")).toLowerCase();

huangapple
  • 本文由 发表于 2020年7月28日 19:10:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63132764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定