在Servlet中forward与sendRedirect的跳转用法区别

网站地址:下面是别人总结的很全面

 

http://www.blogjava.net/yzzh9/archive/2009/06/09/280908.html

 

 

 

http://www.diybl.com/course/4_webprogram/jsp/jsp_js/200863/121115.html#

 

 

 

 

下面是自己总结的:代码可以运行的。可以运行下看看,加深理解。

 

1、登录jsp页面

 

<%@ page language="java" contentType="text/html"
    pageEncoding="utf-8"%>
<html>
<head>
<title>this first jsp and Servlet</title>
</head>
<body>
 <form action="tpath" method="post">
名字:<input type="text" name="uname">
密码:<input type="text" name="password"><br>
     <input type="submit" value="提交">
     </form>
</body>
</html>

 

 

2、servlet类的写法

 

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestTZ extends HttpServlet{

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  this.doPost(req, resp);
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
      String path2="/testPath.jsp";
      String path3="testPath.jsp";
       String uname=req.getParameter("uname");
       String password=req.getParameter("password");
      
       req.setAttribute("k", uname);
       req.setAttribute("v", password);
      
        //   this.getServletConfig().getServletContext().getRequestDispatcher(path2).forward(req, resp);
      
        // req.getRequestDispatcher(path3).forward(req, resp);
     // 上面两个forward方法都可以,跳转到另外一个页面(他们是服务器端跳转)。IE里面的地址看不出跳转到那个jsp页面。
     //但是上面的两个跳转的具体区别,还有点不太清楚。
     //============================================================
      
      
       //sendRedirect它是客户端跳转,可以在IE里面看到,跳转到那个jsp页面。
       //但是它不能通过request.getAttribute("变量");得到值。
       resp.sendRedirect(path3);
 }
}

 

3、接受值的jsp的写法

 

<%@ page language="java" contentType="text/html"
    pageEncoding="utf-8"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
结果是:<br>
<%=request.getAttribute("k") %><br>
<%=request.getAttribute("v") %>
</body>
</html>

 

4、servlet的配置文件web.xml的写法

 

       <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>
  <servlet-name>jack3</servlet-name>
  <servlet-class>org.tiaozhuan.com.TestTZ</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>jack3</servlet-name>
  <url-pattern>/tpath</url-pattern>
  </servlet-mapping>
 
 </web-app>

 

5、总结:主要看一下servlet类的写法和注释就明白了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值