0

我是 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>
4

1 回答 1

0

发生此错误是因为您使用 href 到页面 /Navigation/user/delete 并且此页面不存在。您必须创建此页面才能消除错误,但这并不能解决您的问题。

您在执行 req.getPathInfo() 时分析 url。据我了解,您有包含用户列表和编辑的页面,但没有单独的页面来删除它们。删除用户的单独逻辑。

String action = req.getPathInfo();//here you analyze url
String actionProperty = req.getParameter("action");//here you analyze property
if(actionProperty.equalsIgnoreCase("delete")){
    //you code for delete action
}

并替换您的链接

<a href="${pageContext.request.contextPath}/user/delete" class="btn btn-success">Delete</a>

有了这个

<a href="${pageContext.request.contextPath}/user?action=delete" class="btn btn-success">Delete</a>
于 2017-05-31T22:58:09.207 回答