Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

100.6. JSP

100.6.1. 注释

JSP页面中的注释有以下几种样式的注释方法:

		
JSP页面中的HTML注释 
     <!-- 注释内容 -->  

JSP页面中的普通注释,不会再浏览器中输出
     
    <% // 注释内容 %>    
    <% /* 注释内容 */ %> 

JSP页面中的隐藏注释,不会再浏览器中输出 
<%-- 注释内容 --%>
		
		

100.6.2. pageContext

100.6.2.1. queryString

输出?问号后面的变量

			
${pageContext.request.queryString}
<c:out value = "${pageContext.request.queryString}" />		
			
			

100.6.3. request

new URL(
	request.getScheme(), 
    request.getServerName(), 
    request.getServerPort(), 
    request.getContextPath()
    );		
		

获取主机名

		
<%=request.getServerName() %>
		
		

100.6.3.1. Form

			
<%
   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}"/>
			
			

100.6.4. 

100.6.4.1. sendRedirect

页面跳转

response.sendRedirect("/content/test.jsp");			
			

100.6.5. cookie

获取 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>
 		
		

100.6.6. session

获取 Session 值

		
<% out.print(session.getAttribute("random")); %>
 		
		

100.6.7. page

		
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>		
		
		

100.6.7.1. Session

禁用Session

			
<% @page session="false" %>
			
			

启用Session

			
<%@ page session="true" %>
			
			

100.6.8. trimDirectiveWhitespaces

删除JSP编译产生的空行

		
<%@ page trimDirectiveWhitespaces="true" %> 
				
		

100.6.9. include

		
<%@ 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>
		
		

注意上面包含结果相同,使用上略有差异。

100.6.10. jsp

100.6.10.1. jsp:forward

jsp:forward 只能跳转到存在的物理页面

			
<jsp:forward page="/test.jsp" />
			
			

100.6.11. error-page

在 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>
		
		

100.6.12. JSP 编程

100.6.12.1. 随机数

			
<%= (int) (Math.random() * 1000) %>			
			
			

100.6.13. FAQ

100.6.13.1. http://www.netkiller.cn/test.html;jsessionid=7D25CE666FF437F2094AA945E97CEB37

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;