简单的Servlet页面导航,用于多个操作无效

时间:2012-07-28 19:35:03

标签: jsp java-ee servlets

我是Java EE的新手。使用演示应用程序。现在必须使用JSP&使用JDBC进行学习的Servlet。

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>

1 个答案:

答案 0 :(得分:0)

发生此错误是因为您使用href进行页面/导航/用户/删除并且此页面不存在。您必须创建此页面才能删除错误,但无法解决您的问题。

执行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>