【发布时间】:2012-07-28 19:35:02
【问题描述】:
我是 Java EE 的新手。开发演示应用程序。现在必须使用 JSP & Servlet 和 JDBC 来学习。
UserServlet.java 管理所有用户相关列表的导航、编辑和删除操作。所有 3 个 JSP 页面都有链接来导航它。 JSP 页面位于 /WEB-INF/admin/。请参阅调度方法。
有人请告诉我出了什么问题以及如何使用导航。修复它。
所有链接的错误:
type Status report
message /Navigation/user/delete
description The requested resource (/Navigation/user/delete) is not available.
SERVLET:
package com.myapp.web;
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;
@WebServlet("/user/")
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserServlet() {
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
// String action = req.getServletPath();
String action = req.getPathInfo();
System.out.println("doGet action = " + action);
if ((action == null) || (action.equals("/list"))) {
System.out.println("/UserList.jsp");
dispatch(req, res, "/UserList.jsp");
} else if (action.equals("/edit")) {
System.out.println("/UserEdit.jsp");
dispatch(req, res, "/UserEdit.jsp");
} else if (action.equals("/delete")) {
System.out.println("/UserDelete.jsp");
dispatch(req, res, "/UserDelete.jsp");
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,
IOException {
String action = req.getPathInfo();
System.out.println("doPost action = " + action);
if (action.equals("/edit")) {
res.sendRedirect(req.getContextPath() + "/user/list");
} else if (action.equals("/delete")) {
res.sendRedirect(req.getContextPath() + "/user/list");
}
}
protected void dispatch(HttpServletRequest req, HttpServletResponse res, String page)
throws ServletException, IOException {
String path = "/WEB-INF/admin" + page;
System.out.println(path);
getServletContext().getRequestDispatcher(path).forward(req, res);
}
}
JSP 页面链接。都一样
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<tags:layout path="${pageContext.request.contextPath}" title="list of Users">
<jsp:attribute name="search">
<tags:search />
</jsp:attribute>
<jsp:body>
<span class="right" style="margin-bottom: 3px;"><a href="${pageContext.request.contextPath}/user/list" class="btn btn-success">List</a></span>
<br />
<span class="right" style="margin-bottom: 3px;"><a href="${pageContext.request.contextPath}/user/edit" class="btn btn-success">Edit</a></span>
<br />
<span class="right" style="margin-bottom: 3px;"><a href="${pageContext.request.contextPath}/user/delete" class="btn btn-success">Delete</a></span>
</jsp:body>
</tags:layout>
【问题讨论】:
-
@WebServlet("/user/*")还不够吗?顺便说一句,你考虑过jax-rs吗? -
@tomasz-nurkiewicz,当我使用 /* 时,我得到的 URL 类似于 localhost:8080/Navigation/user* 。你想让我添加这样的额外条件吗 ((action == null) || (action == "*") || (action.equals("/list"))) ?
-
我不太明白。通过说
"/user/*",您指示容器处理所有以/user/开头的传入URL,例如/user/delete,/user/foo... -
如果我像 @WebServlet("/user/*") 一样添加,那么当加载 servlet 并且我看到像“localhost:8080/Navigation/”这样的 url user/*" 它应该在加载 servlet 时加载 localhost:8080/Navigation/user/list。希望你明白我的意思?
-
@tomasz-nurkiewicz,请对我的上述评论有任何帮助。
标签: jsp jakarta-ee servlets