Servlet跳转
2016-02-20 11:12
447 查看
取得初始化信息
在讲解JSP内置对象的时候为读者讲解过config对象,通过此对象可以读取web.xml中配置的初始化参数,此对象实际上是ServletConfig接口的实例,可以通过init()方法找到ServletConfig接口实例
具体操作方法如下:
在servlet中:
publicvoidinit(ServletConfigconfig)throwsServletException{
this.initParam
= config.getInitParameter("ref")
;//接收初始化参数
}
在web.xml中:
<span style="font-size:18px;"> <!--定义servlet --></span>
通过init(ServletConfigconfig)方法可以取得在web.xml文件中配置的初始化参数。
初始化参数要在web.xml中进行配置。
Servlet跳转
从一个JSP或者是一个HTML页面可以通过表单或超链接跳转进Servlet,那么从Servlet也可以跳转到其他的Servlet、JSP或其他页面
³两种跳转形式:
客户端跳转
服务器端跳转
客户端跳转(使用该方法跳转时,若跳转的结果是Servlet,则需要添加App名):
在Servlet中如果要想进行客户端跳转,直接使用HttpServletResponse接口的sendRedirect()方法即可,但是需要注意的是,此跳转只能传递session范围的属性,而无法传递request范围的属性
跳转后接收属性:
//get_info.jsp
<span style="font-size:18px;"><span style="white-space:pre"> </span><%@ pagecontentType="text/html"pageEncoding="GBK"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<% request.setCharacterEncoding("GBK") ;%>
<body>
<h2>session属性:<%=session.getAttribute("name")%></h2>
<h2>request属性:<%=request.getAttribute("info")%></h2>
</body>
</html></span>
服务器端跳转(使用该方法跳转时,若跳转的结果是Servlet,则路径中不需要包含App名):
在Servlet中没有像JSP中的“<jsp:forward>”指令,所以,如果要想执行服务器端跳转的话,就必须靠RequestDispatcher接口完成,此接口中提供了如下的两个方法:
public void forward(ServletRequestrequest,ServletResponse response) throwsServletException,IOException
public void include(ServletRequestrequest,ServletResponse response) throwsServletException,IOException
使用RequestDispatcher接口的forward()方法就可以完成跳转功能的实现,但是如果要想使用此接口还需要使用ServletRequest接口提供的如下方法进行实例化
public RequestDispatchergetRequestDispatcher(String path)
在讲解JSP内置对象的时候为读者讲解过config对象,通过此对象可以读取web.xml中配置的初始化参数,此对象实际上是ServletConfig接口的实例,可以通过init()方法找到ServletConfig接口实例
具体操作方法如下:
在servlet中:
publicvoidinit(ServletConfigconfig)throwsServletException{
this.initParam
= config.getInitParameter("ref")
;//接收初始化参数
}
在web.xml中:
<span style="font-size:18px;"> <!--定义servlet --></span>
<span style="font-size:18px;"><span style="white-space:pre"> </span>《 <servlet-name>initparam</servlet-name><!--与servlet-mapping对应--> <servlet-class> <!--定义包.类名称--> web.InitParamServlet </servlet-class> <init-param> <!--配置参数--> <param-name>ref</param-name> <!--参数名称--> <param-value>the init parameter</param-value><!--参数内容--> </init-param> </servlet> <servlet-mapping> <!--映射路径--> <servlet-name>initparam</servlet-name> <!--与servlet相对应--> <url-pattern>/InitParamServlet</url-pattern><!--页面的映射路径--> </servlet-mapping></span>
通过init(ServletConfigconfig)方法可以取得在web.xml文件中配置的初始化参数。
初始化参数要在web.xml中进行配置。
Servlet跳转
从一个JSP或者是一个HTML页面可以通过表单或超链接跳转进Servlet,那么从Servlet也可以跳转到其他的Servlet、JSP或其他页面
³两种跳转形式:
客户端跳转
服务器端跳转
客户端跳转(使用该方法跳转时,若跳转的结果是Servlet,则需要添加App名):
在Servlet中如果要想进行客户端跳转,直接使用HttpServletResponse接口的sendRedirect()方法即可,但是需要注意的是,此跳转只能传递session范围的属性,而无法传递request范围的属性
publicvoid doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,java.io.IOException { //处理服务 <span style="white-space:pre"> </span>req.getSession().setAttribute("name","David") ; //设置session属性,跳转后传递 <span style="white-space:pre"> </span>req.setAttribute("info","employee") ; //设置request属性,跳转后不会传递 <span style="white-space:pre"> </span>resp.sendRedirect("get_info.jsp") ; // JSP页面跳转 <span style="white-space:pre"> </span>// resp.sendRedirect("/web/des"); //Servlet跳转 }
跳转后接收属性:
//get_info.jsp
<span style="font-size:18px;"><span style="white-space:pre"> </span><%@ pagecontentType="text/html"pageEncoding="GBK"%>
<html>
<head><title>www.mldnjava.cn,MLDN高端Java培训</title></head>
<% request.setCharacterEncoding("GBK") ;%>
<body>
<h2>session属性:<%=session.getAttribute("name")%></h2>
<h2>request属性:<%=request.getAttribute("info")%></h2>
</body>
</html></span>
服务器端跳转(使用该方法跳转时,若跳转的结果是Servlet,则路径中不需要包含App名):
在Servlet中没有像JSP中的“<jsp:forward>”指令,所以,如果要想执行服务器端跳转的话,就必须靠RequestDispatcher接口完成,此接口中提供了如下的两个方法:
public void forward(ServletRequestrequest,ServletResponse response) throwsServletException,IOException
public void include(ServletRequestrequest,ServletResponse response) throwsServletException,IOException
使用RequestDispatcher接口的forward()方法就可以完成跳转功能的实现,但是如果要想使用此接口还需要使用ServletRequest接口提供的如下方法进行实例化
public RequestDispatchergetRequestDispatcher(String path)
<span style="font-size:18px;">publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp) throwsServletException,java.io.IOException { //处理服务 req.getSession().setAttribute("name","David") ; //设置session属性 req.setAttribute("info","employee") ; //设置request属性 //实例化RequestDispatcher对象,同时指定跳转路径 RequestDispatcherrd =req.getRequestDispatcher("get_info.jsp"); rd.forward(req,resp) ; //服务器跳转 }</span>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序