从单个servlet调用多个链接

时间:2019-02-28 15:46:15

标签: java jsp servlets

我正在开发和图书馆管理系统,并为具有添加书,查看用户列表和已发行书的特权的管理员人员编写servlet,所有这些选项都在标题中可用。我对servlet的映射感到困惑,我有一个称为admin_dashboard的主jsp,它将调用AdminActionServlet,我包含了包含header.jsp的{​​{1}},我想调用url模式就像下面的代码片段一样。

admin_dashboard

href

header.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Admin Dashboard</title>
<%@include file="../common/include.jsp"%>

</head>
<body>
    <form action="AdminActionServlet">
        <%@include file="../common/header.jsp"%>


    </form>

    <%@include file="../common/footer.jsp"%>
</body>

</html>

AdminActionServlet

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Book Sheleve</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse"
        data-target="#navbarText" aria-controls="navbarText"
        aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarText">
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active"><a class="nav-link"
                href="./add">Add Book<span
                    class="sr-only">(current)</span></a></li>
            <li class="nav-item"><a class="nav-link" href="./userList">Registered
                    User</a></li>
            <li class="nav-item"><a class="nav-link" href="./issuedBookList">Books
                    Issued</a></li>
        </ul>
        <!--     <span class="navbar-text"> -->
        <!--       Navbar text with an inline element -->
        <!--     </span> -->
    </div>
</nav>

WEB.XML

package com.javarnd.lbms.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

    import com.javarnd.lbms.enums.ResourceEnum;

    public class AdminActionServlet extends HttpServlet {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String requestUrl = request.getServletPath();
            switch (requestUrl) {
            case "/add":
                addBooks(request, response);
                break;
    //      case "/userList":
    //          showUserList(request, response);
    //          break;
    //          case "/issuedBookList":
    //          showIssuedBookList(request, response);
    //          break;
            }
        }

        private void addBooks(HttpServletRequest request, HttpServletResponse response) {
            try {
                request.getRequestDispatcher(ResourceEnum.RESOURCE_PATH.getName() + "admin/add_books.jsp").forward(request,
                        response);
            } catch (Exception e) {
                e.printStackTrace();

            }
        }

    }

当前我的网址就像

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">


    <!-- <servlet> -->
    <!-- <servlet-name>ContactBookServlet</servlet-name> -->
    <!-- <servlet-class>com.javarnd.lbms.servlet.ContactBookServlet</servlet-class> -->
    <!-- <load-on-startup>1</load-on-startup> -->
    <!-- </servlet> -->
    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <!-- <display-name>LoginServlet</display-name> -->
        <servlet-class>com.javarnd.lbms.servlet.LoginServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- <servlet-mapping> -->
    <!-- <servlet-name>ContactBookServlet</servlet-name> -->
    <!-- <url-pattern>/</url-pattern> -->
    <!-- </servlet-mapping> -->
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <!-- <url-pattern>/LoginServlet</url-pattern> -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>RegistrationServlet</servlet-name>
        <!-- <display-name>LoginServlet</display-name> -->
        <servlet-class>com.javarnd.lbms.servlet.RegistrationServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegistrationServlet</servlet-name>
        <url-pattern>/RegistrationServlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>AdminActionServlet</servlet-name>
        <!-- <display-name>LoginServlet</display-name> -->
        <servlet-class>com.javarnd.lbms.servlet.AdminActionServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AdminActionServlet</servlet-name>
        <url-pattern>/AdminActionServlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/WEB-INF/view/login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

为空白。 我想从单个servlet管理所有网址,请帮助我

0 个答案:

没有答案