英文:
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":[
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论