JSP 相关题目 (网络整理)
1、JSP静态文件的引入
JSP中的两种包含页面的方法
第一种:include指令:当JSP转换成Servlet时引入指定文件 <%@ page contentType="text/html; charset=GB2312" language="java" errorPage=""%>
<%@ include file="head.jsp"%>
<%@ include file="body.jsp"%>
<%@ include file="tail.jsp"%>
第二种:<jsp:include>动作元素:当JSP页面被请求时引入指定文件
<%@ page contentType="text/html; charset=GB2312" language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:include page="body.jsp"/>
<jsp:include page="tail.jsp"/>
第二种方法可以很方便的用<jsp:param>来向所包含页传递参数,方法如下:
<%@ page contentType="text/html; charset=GB2312" language="java" errorPage=""%>
<jsp:include page="head.jsp"/>
<jsp:include page="body.jsp">
<jsp:param name="uid" value="username"/>
<jsp:param name="pwd" value="password"/>
</jsp:include>
<jsp:include page="tail.jsp"/>
2、JSP页面的重定向
{
response.setContentType("text/html; charset=UTF-8");
ServletContext sc = getServletContext();
RequestDispatcher rd = null;
rd = sc.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
这种方式是在客户端作的重定向处理。该方法通过修改HTTP协议的HEADER部分,对浏览器下达重定向指令的,让浏览器对在location中指定的URL提出请求,使浏览器显示重定向网页的内容。该方法可以接受绝对的或相对的URLs。如果传递到该方法的参数是一个相对的URL,那么Web container在将它发送到客户端前会把它转换成一个绝对的URL。
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
response.setContentType("text/html; charset=UTF-8");
response.sendRedirect("/index.jsp");
}

浙公网安备 33010602011771号