| 知乎专栏 |
JSP页面中的注释有以下几种样式的注释方法:
JSP页面中的HTML注释
<!-- 注释内容 -->
JSP页面中的普通注释,不会再浏览器中输出
<% // 注释内容 %>
<% /* 注释内容 */ %>
JSP页面中的隐藏注释,不会再浏览器中输出
<%-- 注释内容 --%>
new URL(
request.getScheme(),
request.getServerName(),
request.getServerPort(),
request.getContextPath()
);
获取主机名
<%=request.getServerName() %>
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String paramValue = request.getParameter(paramName);
out.println(paramName + ": " + paramValue + "<br/>\n");
}
%>
<%= request.getParameter("first_name")%>
<c:set var="UA" value="${param.first_name}" />
<c:out value="${param.first_name}"/>
获取 cookie 值
<html>
<head>
<title>Reading Cookies</title>
</head>
<body>
<center>
<h1>Reading Cookies</h1>
</center>
<%
Cookie cookie = null;
Cookie[] cookies = null;
// Get an array of Cookies associated with this domain
cookies = request.getCookies();
if( cookies != null ){
out.println("<h2> Found Cookies Name and Value</h2>");
for (int i = 0; i < cookies.length; i++){
cookie = cookies[i];
out.print("Name : " + cookie.getName( ) + ", ");
out.print("Value: " + cookie.getValue( )+" <br/>");
}
}else{
out.println("<h2>No cookies founds</h2>");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="analytics.jsp" %>
<jsp:include page="telephone.jsp" />
<jsp:include page="address.jsp" flush="true"/>
<jsp:include page="${request.getContentType()}/module/html.body.begin.html" flush="true" />
file 与 page 的区别是,file 将文件包含进当前文件,然后变成成servlet。 page 是运行的时候才会包含文件输出结果包含进当前文件。
<pre>
<%@include file="inc.html" %>
=============
<%@include file="/inc.html" %>
=============
<jsp:include page="inc.html" />
=============
<jsp:include page="inc.html" flush="true" />
==============
<jsp:include page="/inc.html" flush="true" />
==============
<jsp:include page="${request.getContentType()}/inc.html" flush="true" />
</pre>
注意上面包含结果相同,使用上略有差异。
在 web.xml 中配置 error-page
<error-page> <error-code>400</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/error.jsp</location> </error-page> <!-- java.lang.Exception异常错误,依据这个标记可定义多个类似错误提示 --> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/error.jsp</location> </error-page> <!-- java.lang.NullPointerException异常错误,依据这个标记可定义多个类似错误提示 --> <error-page> <exception-type>java.lang.NullPointerException </exception-type> <location>/error.jsp</location> </error-page> <error-page> <exception-type>javax.servlet.ServletException</exception-type> <location>/error.jsp</location> </error-page>
URL经常会出现 jsessionid,这是因为当你使用c:url的时候,它会判断当前域下是否已经创建jsessionid的cookie变量,如果没有并且你链接的是静态页面,那么c:url将传递jsessionid变量,迫使Tomcat为当前域创建jsessionid cookie。
当你使用 Apache Nginx 作为Tomcat前端是,由于Apache Nginx不识别;jsessionid=7D25CE666FF437F2094AA945E97CEB37这种URL,会跳出404页面。
解决方案一,放弃使用c:url,如果你需要保持context 的 path 设置,可以使用下面方法
<a href="${pageContext.request.contextPath}/test.html">Netkiller Test</a>
方案二,使用rewrite截取jsessionid做忽略处理
Apache:
ReWriteRule ^/(\w+);jsessionid=\w+$ /$1 [L,R=301] ReWriteRule ^/(\w+\.do);jsessionid=\w+$ /$1 [L,R=301]
Nginx:
rewrite ^(.*)\;jsessionid=(.*)$ $1 break;