java web服务器跳转

本文介绍了在Java Web开发中,如何实现客户端和服务端的页面跳转。客户端跳转通过发送重定向请求实现,而服务器端跳转则利用RequestDispatcher接口的forward方法。示例代码展示了如何设置并传递session及request属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端跳转:response.sendRedirect("index.jsp");
example :
servlet.java
 
 
package org.lxh.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ClientRedirectDemo extends HttpServlet { // 继承HttpServlet
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException { // 处理服务
  req.getSession().setAttribute("name", "李兴华") ; // 设置session属性
  req.setAttribute("info", "MJAVA") ; // 设置request属性
  resp.sendRedirect("index.jsp") ; // 页面跳转
 }
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException { // 处理服务
  this.doGet(req, resp);    // 调用doGet()
 }
}

 

index.jsp:

 

 

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.java.cn,高端Java培训</title></head>
 <% request.setCharacterEncoding("GBK") ;%>
<body>
 <h2>session属性:<%=session.getAttribute("name")%></h2>
 <h2>request属性:<%=request.getAttribute("info")%></h2>
</body>
</html>

 

 

服务器跳转:

 

在Servlet中没有像JSP中的“<jsp:forward>”指令,所以,如果要想执行服务器端跳转的话,就必须依靠RequestDispatcher接口完成,此接口中提供了如下的方法: public void forward (ServletRequest

request,ServletResponseresponse)

throws ServletException,IOException

使用RequestDispatcher接口的forward()方法就可以完成跳转功能的实现,但是如果要想使用此接口还需要使用ServletRequest接口提供的如下方法进行实例化 public RequestDispatcher getRequestDispatcher(String path)

 
example :
 
使用服务器端跳转
package org.lxh.servletdemo;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServerRedirectDemo extends HttpServlet {  // 继承HttpServlet
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException { // 处理服务
  req.getSession().setAttribute("name", "李兴华") ; // 设置session属性
  req.setAttribute("info", "MJAVA") ;  // 设置request属性
  // 实例化RequestDispatcher对象,同时指定跳转路径
  RequestDispatcher rd = req.getRequestDispatcher("index.jsp");
  rd.forward(req, resp) ;    // 服务器跳转
 }
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, java.io.IOException { // 处理服务
  this.doGet(req, resp);    // 调用doGet()
 }
}
 
index.jsp:
 
接收属性
 
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>www.java.cn,高端Java培训</title></head>
 <% request.setCharacterEncoding("GBK") ;%>
<body>
 <h2>session属性:<%=session.getAttribute("name")%></h2>
 <h2>request属性:<%=request.getAttribute("info")%></h2>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值