Как я могу вызвать путь сервлета, используя html href? - PullRequest
0 голосов
/ 13 июня 2019

У меня есть эта программа, где мне нужно перейти на homepage.jsp, чтобы отобразить детали адреса в моей базе данных. Но прежде чем перейти на домашнюю страницу, он должен сначала пройти через сервлет списка адресов. Я включаю Navbar HTML в каждый JSP, который я использую.

Как я могу использовать href в html, чтобы сначала перейти на мой сервлет, прежде чем отобразить его на домашней странице jsp?

navbar в html :

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Insert title here</title>
  <style>
    body {
      margin: 0;
    }

    ul {
      list-style-type: none;
      margin: 0;
      padding: 0;
      width: 15%;
      position: fixed;
      height: 100%;
      overflow: auto;
      background: linear-gradient(to bottom, #33ccff 0%, #ff99cc 100%)
    }

    li a {
      display: block;
      color: #000;
      padding: 8px 16px;
      text-decoration: none;
    }

    li a.active {
      background-color: #4CAF50;
      color: white;
    }

    li a:hover:not(.active) {
      background-color: #555;
      color: white;
    }
    /*/* Style the buttons */

    .btn {
      border: none;
      outline: none;
      padding: 10px 16px;
      background-color: #f1f1f1;
      cursor: pointer;
    }
    /* Style the active class (and buttons on mouse-over) */

    .active,
    .btn:hover {
      background-color: #666;
      color: white;
    }

    */
  </style>
</head>

<body>
  <ul>
    <li><a href="homepage.jsp">ホームページ</a></li>
    <li><a href="showtest.html">一覧表示</a></li>
    <li><a href="add.jsp">新規追加</a></li>
    <li><a href="#import">インポート</a></li>
    <li><a href="export-to-excel">エクスポート</a></li>
    <li><a href="logout.html">ログアウト</a></li>
  </ul>


  </div>
</body>

</html>

домашняя страница jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.List, model.entity.AddressBean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
    name="viewport" content="width=device-width, initial-scale=1">
<title>ホームページ</title>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
</style>
</head>
<body>

    <jsp:include page="menu.html" />
    <div style="margin-left: 25%; padding: 1px 16px; height: 1000px;">
        <div>
            <%
                request.setCharacterEncoding("UTF-8");
                String userID = request.getParameter("userID");
                session.setAttribute("userID", userID);
            %>
            <h6>
                welcome,
                <%=userID%>さん
            </h6>
            <br>
        </div>


        <h2 align="center">ホームページ</h2>

        <table align="center">
            <tr>
                <th>姓</th>
                <th>名</th>
                <th>メールアドレス</th>
            </tr>
            <%
                List<AddressBean> addressList = (List<AddressBean>) request.getAttribute("addressList");
                for (AddressBean ab : addressList) {
            %>
            <tr>
                <td><%=ab.getLastName()%></td>
                <td><%=ab.getFirstName()%></td>
                <td><%=ab.getEmail()%></td>
                <td>
                    <form action="edit-process-servlet" method="post">
                        <input type="hidden" name="code" value="<%=ab.getRecordID()%>">
                        <input type="submit" value="編集">
                    </form>
                </td>
                <td>
                    <form action="delete-process-servlet" method="post">
                        <input type="hidden" name="recordID" value="<%=ab.getRecordID()%>">
                        <input type="submit" value="削除"><br>
                    </form>
                </td>

            </tr>
            <%
                }
            %>


        </table>
    </body>
</html>

сервлет списка адресов

package servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.dao.AddressDAO;
import model.entity.AddressBean;

/**
 * Servlet implementation class AddressList
 */
@WebServlet("/address-list")
public class AddressList extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddressList() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession();
        String login_auth = (String) session.getAttribute("login_auth");

         String path = "login.html";
        if (login_auth != null) {

            path = "homepage.jsp";

            try {
                List<AddressBean> addressList = new ArrayList<AddressBean>();
                AddressDAO dao = new AddressDAO();
                addressList = dao.selectAddress();
                request.setAttribute("addressList", addressList);
            } catch (SQLException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }else {
            path="login-failure.html";
        }
        RequestDispatcher rd = request.getRequestDispatcher(path);
        rd.forward(request, response);
    }

}
...