我是 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.
伺服器:
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>