The include directive does not support the writing of the servlet path, but we have a workaround!

The include instruction does not support the writing method of the servlet path, and the writing method of the servlet path will report an error, but we have a workaround!

( If the servlet path cannot be used, write the jsp path, and then forward it to the servlet in jsp, which is equivalent to using jsp to make a transition . as a workaround )

look down

home2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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">
<title>Test whether the include directive can include Servlet</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/body.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/mark.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/console.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/console.js"></script>
</head>
<body>
<h2>
Test whether the include instruction can include Servlet (if the Servlet path cannot be used, write the jsp path, and then turn it in the jsp
Sending it to Servlet is equivalent to using jsp to make a transition. In other words, it is equivalent to jsp being a transit station, and jsp plays the role of transit, which is also a workaround)
</h2>
<h2>The include directive does not support the writing method of the servlet path, and the writing method of the servlet path will report an error</h2>
<%@ include file="/index.jsp"%>

</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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">
<title>Temporary transition page, similar to the role of a transfer station</title>
</head>
<body>
<h2>Temporary transition page, similar to the role of a transfer station</h2>
<%
request.getRequestDispatcher("/SaveData2").forward(request, response);
%>
</body>
</html>

servlet named SaveData2

package com.jiongmeng.servlet;

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

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 com.jiongmeng.entity.HyperLink;

/**
 * Save the data, then forward to the jsp page
 */
@WebServlet("/SaveData2")
public class SaveData2 extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setAttribute("fromPath", "SaveData2");
		System.out.println("--------------SaveData2--------------");
		System.out.println("SaveData2 / city=" + request.getParameter("city"));
		System.out.println("SaveData2 / moeny=" + request.getParameter("moeny"));
		System.out.println("SaveData2 / workYear=" + request.getParameter("workYear"));
		// hyperlink entity, collection
		List<HyperLink> hyperLinkList = new ArrayList<HyperLink>();
		HyperLink hyperLink1 = new HyperLink("626", "Zhaopin Recruitment", "http://www.zhaopin.com/", "Looking for a job on Zhaopin Recruitment");
		HyperLink hyperLink2 = new HyperLink("627", "Worry-free career", "http://www.51job.com/", "Looking for a job and worry-free career");
		HyperLink hyperLink3 = new HyperLink("628", "Lagou", "https://www.lagou.com/", "IT Internet Vertical Recruitment");
		HyperLink hyperLink4 = new HyperLink("629", "Kugou Music", "http://www.kugou.com/", "Just Geduo");
		HyperLink hyperLink5 = new HyperLink("630", "Dianping", "http://www.dianping.com/", "The best in reviews");
		HyperLink hyperLink6 = new HyperLink("891", "12306", "http://www.12306.cn/mormhweb/", "Official train ticket purchase");
		hyperLinkList.add(hyperLink1);
		hyperLinkList.add(hyperLink2);
		hyperLinkList.add(hyperLink3);
		hyperLinkList.add(hyperLink4);
		hyperLinkList.add(hyperLink5);
		hyperLinkList.add(hyperLink6);
		request.setAttribute("hyperLinkList", hyperLinkList);
		//forward to jsp page
//		request.getRequestDispatcher("/hyperLinkList.jsp").forward(request, response);
		request.getRequestDispatcher("/hyperLinkList.jsp").include(request, response);
		
	}

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

}

hyperLinkList.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!--
<title>I am the included page hyperLinkList.jsp</title>
 -->
	<center>
	<hr>
	<h1>Demo jsp:include action and include directive</h1>${param.onLineName}<br><br>
		<h2>I am displaying the hyperlink collection page (I am hyperLinkList.jsp)</h2>
		<c:if test="${'SaveData2' == requestScope.fromPath }">
		<c:forEach var="hyperLinkItem" items="${requestScope.hyperLinkList}">
		<a style="background-color: red;" href="${hyperLinkItem.url}" target="_blank">${hyperLinkItem.name}</a>
		    
		</c:forEach>
		</c:if>
		<c:if test="${'SaveData' eq requestScope.fromPath }">
		<c:forEach var="hyperLinkItem" items="${requestScope.hyperLinkList}">
		<a href="${hyperLinkItem.url}" target="_blank">${hyperLinkItem.name}</a>
		    
		</c:forEach>
		</c:if>
	</center>
	<jsp:include page="/toDesktop.jsp?userName=jack&age=6">
	<jsp:param name="hobby" value="football"/>
	</jsp:include>
<hr>
toDesktop.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!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">
<title>Put it on the desktop, send a shortcut to the desktop</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/body.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/mark.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/console.css">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/console.js"></script>
</head>
<body>
	<center>
	<h1>
	I am toDesktop.jsp page<br><br>
	${param.userName}<br><br>
	${param.age}<br><br>
	${param.hobby}<br><br>
	</h1>
		<h2>Put it on the desktop, send a shortcut to the desktop (IE browser will display the button to the desktop, other browsers will not display the button to the desktop)</h2>
		<input name="myToDesktopButton" style="display: block;" id="toDesktopButton" type="button" value="put on desktop" onclick="toDesktop(location.href, 'Snow Leopard Software Studio')">
		<!--
		block This element will appear as a block-level element with line breaks before and after this element.
		inline default. This element is displayed as an inline element, without line breaks before and after the element.
		If the style display of the button button above is set to inline, then the button button will be displayed on the same line as the button button below.
		If the style display of the button above is set to block, the button will not be displayed on the same line as the button below.
		 -->
		<input type="button" value="I am a button, my button has no meaning">
	</center>
</body>
<script type="text/javascript">

	//Send the shortcut to the desktop
	function toDesktop (sUrl, sName) {
		try {
			var WshShell = new ActiveXObject("WScript.Shell");
			var oUrlLink = WshShell.CreateShortcut(WshShell
					.SpecialFolders("Desktop")
					+ "\\" + sName + ".url");
			oUrlLink.TargetPath = sUrl;
			oUrlLink.Save();
			alert("Successfully created desktop shortcut!");
		} catch (e) {
			alert("The current IE security level does not allow operation or your browser does not support this function!");
		}
	}
	
	/ / Determine whether it is an IE browser
	function isIE () {
		if (!!window.ActiveXObject || "ActiveXObject" in window) {
// alert("It's IE browser");
			return true;
		} else {
// alert("Not IE browser");
			return false;
		}
	}
	
	//If the page include (included) is included multiple times, the onload event will not be executed until the entire large page is loaded, and the onload event will only be executed once
	/* For example, the a.jsp page is included in the home.jsp page multiple times, then the onload event will be executed after the entire home.jsp page is loaded, and the onload event will only be executed once*/
	window.onload = function() {
		alert("url=" + window.location.href);
		var result = isIE ();
		if (!result) {
			//not IE browser
			
			/*
			If the page include (included) is included only once, use the following document.getElementById("element id") method to get the element, only
			get an element
			*/
			//get button element
			var toDesktopButtonNode = document.getElementById("toDesktopButton");
			// hide button
			toDesktopButtonNode.style.display = "none";
			
			/*
			If the page includes (included) is included multiple times, use the following method of document.getElementsByName("element name")
			Get elements in the way, you can get multiple elements, and what you get is an array
			*/
			var toDesktopButtonNodes = document.getElementsByName("myToDesktopButton");
			for (var index = 0; index < toDesktopButtonNodes.length; index++) {
				var toDesktopButtonElement = toDesktopButtonNodes [index];
				toDesktopButtonElement.style.display = "none";
			}
		}
	}
</script>
</html>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693520&siteId=291194637