Имеется файл cabinetList.jsp, из него нужно взять параметр idCabinet./n и перенаправить в другой documentList.jsp чтобы выводились документы которые находятся в определенном Cabinet с со своим idCabinet после нажатия кнопки Access.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="lifecycle/js/cabinetJS.js"></script>
<title>Cabinets</title>
</head>
<body>
<p>You are logged in as <%=session.getAttribute("name")%></p>
<a href="DocumentList">Document List</a>
<form action="${pageContext.request.contextPath}/logout" method="post">
<input type="submit" value="Logout" />
</form>
<c:if test="${(not empty name) && (name1=='name1')}">
<form method="POST" action="" onsubmit="return setCabinet(this);">
<p>${answer}</p>
<c:remove var="answer" scope="session" />
<table border="1">
<tr align="center" bgcolor="#E6E6E6">
<th>Cabinet ID</th>
<th>Cabinet Name</th>
<th>Owner Name</th>
<th>Creation Date</th>
</tr>
<c:forEach items="${cabList}" var="cab">
<tr>
<td><label><input type="radio" name="cabinet" onclick="setDocumentButton(this);" value="${cab.idCabinet}" > ${cab.idCabinet}</label></td>
<td>${cab.nameCabinet}</td>
<td>${cab.nameOwner}</td>
<td>${cab.dateStorage}</td>
</tr>
</c:forEach>
<tr>
<td colspan="7">
<table class="buttons"
style="width: 100%; text-align: center; border: none !important;">
<tr>
<td><input type="submit" value="Edit" name="edit" disabled onclick="page='CabinetList'" /></td>
<td><input type="submit" value="Delete" name="delete" disabled onclick="page='CabinetDelete'" /></td>
<td><input type="submit" value="Add" name="add" onclick="page='CabinetList'" /></td>
<td><input type="submit" value="Access" name="access" disabled onclick="page='DocumentList'" /></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</c:if>
<c:if test="${(empty name) || (name1!='name1')}">
<%
response.sendRedirect("UserLogin");
%>
</c:if>
Вот Сервлет CabinetList
package com.jmd.cabinet.web;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jmd.cabinet.data.Cabinet;
import com.jmd.cabinet.services.CabinetService;
import com.jmd.util.ServiceException;
import com.jmd.util.web.GenericServlet;
@WebServlet("/CabinetList")
public class CabinetList extends GenericServlet {
private static final long serialVersionUID = 1L;
private CabinetService cabservices = null;
public void init() {
super.init();
if (connector != null) {
cabservices = new CabinetService(connector);
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String view;
try {
view = populateView(request);
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
} catch (ServiceException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("edit")!=null){
List<Cabinet> cabUpdateList;
cabUpdateList = cabservices.getCabinetsById(Integer.parseInt(request.getParameter("cabinet")));
request.setAttribute("cabUpdateList", cabUpdateList);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/cabinet/createCabinet.jsp");
dispatcher.forward(request, response);
}else if(request.getParameter("add")!=null){
String view;
try {
view = populateCreateJsp(request);
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
} catch (ServiceException e) {
e.printStackTrace();
}
}else if(request.getParameter("cancel")!=null){
doGet(request,response);
}else if(request.getParameter("access")!=null){
HttpSession session = request.getSession();
session.setAttribute("idCabinet", "idCabinet");
response.sendRedirect("DocumentList");
}
}
private String populateView(HttpServletRequest request)
throws ServiceException {
if (cabservices == null) {
request.setAttribute("errorMessage", "Service not initialised");
return "/WEB-INF/jsp/500.jsp";
}
List<Cabinet> cabList;
cabList = cabservices.getCabinets();
request.setAttribute("cabList", cabList);
return "/WEB-INF/jsp/cabinet/cabinetList.jsp";
}
private String populateCreateJsp(HttpServletRequest request) throws ServiceException {
if (cabservices == null) {
request.setAttribute("errorMessage", "Service not initialised");
return "/WEB-INF/jsp/500.jsp";
}
return "/WEB-INF/jsp/cabinet/createCabinet.jsp";
}
}
Вот Второй сервлет от DocumentList
package com.jmd.document.web;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import com.jmd.util.ServiceException;
import com.jmd.document.services.DocumentService;
import com.jmd.util.web.GenericServlet;
import com.jmd.document.data.Document;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@WebServlet("/DocumentList")
public class DocumentList extends GenericServlet{
private static final long serialVersionUID = 1L;
private DocumentService documentService = null;
public void init(){
super.init();
if(connector != null){
documentService = new DocumentService(connector);
}
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
String view;
try{
view = populateView(request);
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
} catch (ServiceException e){
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
if (request.getParameter("edit")!=null){
List<Document> docUpdateList;
docUpdateList = documentService.getDocumentsById(Integer.parseInt(request.getParameter("document")));
request.setAttribute("docUpdateList", docUpdateList);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/document/createDocument.jsp");
dispatcher.forward(request, response);
}else if(request.getParameter("add")!=null){
String view;
try {
view = populateCreateJsp(request);
RequestDispatcher dispatcher = request.getRequestDispatcher(view);
dispatcher.forward(request, response);
} catch (ServiceException e) {
e.printStackTrace();
}
}else if(request.getParameter("create")!=null){
createDocument(request, response);
}
else if(request.getParameter("delete")!=null){
deleteDocument(request, response);
}else if(request.getParameter("extend")!=null){
List<Document> docUpdateList;
docUpdateList = documentService.getDocumentsById(Integer.parseInt(request.getParameter("Document")));
request.setAttribute("docUpdateList", docUpdateList);
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/centraldepot/supliedocument.jsp");
dispatcher.forward(request, response);
}else if(request.getParameter("cancel")!=null){
doGet(request,response);
}
}
private void createDocument(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Document document = new Document();
Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
document.setNameDocument(request.getParameter("nameDocument"));
document.setTipDocument(request.getParameter("tipDocument"));
InputStream inputStream = null;
Part filePart = request.getPart("file");
if (filePart != null){
document.setTipContent(filePart.getContentType());
document.setSizeContent((int) filePart.getSize());
inputStream = filePart.getInputStream();
}
document.setAuthor(request.getParameter("author"));
document.setKeyword(request.getParameter("keyword")) ;
documentService.insertDocuments(document);
response.sendRedirect("DocumentList");
}
private void deleteDocument(HttpServletRequest request, HttpServletResponse response) throws IOException {
documentService.removeDocuments(Integer.parseInt(request
.getParameter("document")));
response.sendRedirect("DocumentList");
}
private String populateView(HttpServletRequest request) throws ServiceException {
if (documentService == null) {
request.setAttribute("errorMessage", "Service not initialised");
return "/WEB-INF/jsp/500.jsp";
}
List<Document> docList;
HttpSession session = request.getSession();
session.getAttribute("idCabinet");
docList = documentService.getDocuments();
request.setAttribute("docList", docList);
return "/WEB-INF/jsp/document/documentList.jsp";
}
private String populateCreateJsp(HttpServletRequest request) throws ServiceException {
if (documentService == null) {
request.setAttribute("errorMessage", "Service not initialised");
return "/WEB-INF/jsp/500.jsp";
}
return "/WEB-INF/jsp/document/createDocument.jsp";
}
}
documentList.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="lifecycle/js/documentJS.js"></script>
<title>Document</title>
</head>
<body>
<p>You chose <%=session.getAttribute("idCabinet")%></p>
<form action="${pageContext.request.contextPath}/logout" method="post">
<input type="submit" value="Logout" />
</form>
<c:if test="${(not empty name) && (name1=='name1')}">
<form method="POST" action="" onsubmit="return setDocument(this);">
<input type="hidden" name="idCabinet" value="" />
<p>${answer}</p>
<c:remove var="answer" scope="session" />
<table border="1">
<tr align="center" bgcolor="#E6E6E6">
<th>Document ID</th>
<th>Document Name</th>
<th>Tip Document</th>
<th>Tip Continut</th>
<th>Dimensiune Continut</th>
</tr>
<c:forEach items="${docList}" var="doc">
<tr>
<td><label><input type="radio" name="document" onclick="setDocumentButton(this);" value="${doc.idDocument}" > ${doc.idDocument}</label></td>
<td>${doc.nameDocument}</td>
<td>${doc.tipDocument}</td>
<td>${doc.tipContent}</td>
<td>${doc.sizeContent}</td>
</tr>
</c:forEach>
<%response.sendRedirect("UserLogin");%>
setDocumentButton()
,setCabinet()
, переменнаяpage
?session.setAttribute("idCabinet", "idCabinet");
?