JSP中实现页面跳转的三种方法以及区别(转载)

本文介绍了JSP中实现页面跳转的三种方法:forward、sendRedirect及使用header进行重定向,并详细阐述了它们之间的区别,包括跳转类型、地址栏变化、request属性保留等关键特性。

JSP中实现页面跳转的三种方法以及区别

通过重定向可以实现跳转,跳转后页面地址变为跳转后的地址。

我们已经知道有两种跳转方法了

Java代码
  1. 第一种是:<jsp:forward page=””/>;   
  2.   
  3. 第二种是:response.sendRedirect(“地址”);  
  4.   
  5. 第三种:response.setHeader("refresh" , "2;URL=responseDemo02.jsp" ) ;  
  6.   
  7. //2秒后自动跳转到responseDemo02.jsp页面,也是一种重定向方法   
Java代码 复制代码
  1. 第一种是:<jsp:forward page=””/>;    
  2.   
  3. 第二种是:response.sendRedirect(“地址”);   
  4.   
  5. 第三种:response.setHeader("refresh","2;URL=responseDemo02.jsp") ;   
  6.   
  7. //2秒后自动跳转到responseDemo02.jsp页面,也是一种重定向方法  
第一种是:<jsp:forward page=””/>; 

第二种是:response.sendRedirect(“地址”);

第三种:response.setHeader("refresh","2;URL=responseDemo02.jsp") ;

//2秒后自动跳转到responseDemo02.jsp页面,也是一种重定向方法



【response.setHeader("refresh","1");表示页面1秒后将自动刷新】


这两种跳转有什么区别呢?【面试的时候经常遇到】

<jsp:forward page=””/>
-跳转后地址不变。这种跳转称为服务器端跳转。

跳转语句之后的语句不会得到执行。

能保留request属性。

通过<jsp:param name=”” value=””/>传递参数


response.sendRedirect(“地址”):

跳转后地址改变。这种跳转称为客户端跳转。

跳转语句前后代码都执行完毕之后再跳转。

不能保留request属性,地址改变了,客户端跳转。

通过客户端跳转可以使用重写URL的方式把参数传递过去。

import com.zyy.pojo.User; import java.sql.Connection; import java.sql.SQLException; /** * @ClassName: UserDao * @Description: TODO 类描述 * @Author: zyy * @Date: 2022/01/09 10:38 * @Version: 1.0 */ public interface UserDao { /** * 根据用户编码获取用户信息 * @param con * @param userCode * @return */ User getLoginUser(Connection con, String userCode) throws SQLException; } 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 编写dao接口的实现类 import com.zyy.dao.BaseDao; import com.zyy.dao.user.UserDao; import com.zyy.pojo.User; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; /** * @ClassName: UserDaoImpl * @Description: TODO 类描述 * @Author: zyy * @Date: 2022/01/09 10:51 * @Version: 1.0 */ public class UserDaoImpl implements UserDao { public User getLoginUser(Connection con, String userCode) throws SQLException { PreparedStatement ps = null; ResultSet rs = null; User user = null; if (con != null) { String sql = "select id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate from smbms_user where userCode=?"; Object[] params = {userCode}; rs = BaseDao.execute(con, ps, rs, sql, params); if (rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setUserCode(rs.getString("userCode")); user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setGender(rs.getInt("gender")); user.setBirthday(rs.getDate("birthday")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); user.setUserRole(rs.getInt("userRole")); user.setCreatedBy(rs.getInt("createdBy")); user.setModifyBy(rs.getInt("modifyBy")); user.setCreationDate(rs.getTimestamp("creationDate")); user.setModifyDate(rs.getTimestamp("modifyDate")); } //关闭 BaseDao.closeResource(ps, rs); } return user; } } 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 业务层接口 import com.zyy.pojo.User; /** * @InterfaceName: UserService * @Description: TODO 接口描述 * @Author: zyy * @Date: 2022/01/09 11:18 * @Version: 1.0 */ public interface UserService { /** * 登录 * @param userCode * @param password * @return */ User login(String userCode, String password); } 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 业务层实现类 import com.zyy.dao.BaseDao; import com.zyy.dao.user.UserDao; import com.zyy.dao.user.impl.UserDaoImpl; import com.zyy.pojo.User; import com.zyy.service.user.UserService; import java.sql.Connection; import java.sql.SQLException; /** * @ClassName: UserServiceImpl * @Description: TODO 类描述 * @Author: zyy * @Date: 2022/01/09 11:20 * @Version: 1.0 */ public class UserServiceImpl implements UserService { /** * 业务层会调用dao层,所以这里引入Dao层 */ private UserDao userDao; public UserServiceImpl() { userDao = new UserDaoImpl(); } public User login(String userCode, String password) { Connection con = null; User user = null; try { con = BaseDao.getConnection(); user = userDao.getLoginUser(con, userCode); } catch (SQLException e) { e.printStackTrace(); } finally { BaseDao.closeResource(con); } return user; } } 一键获取完整项目代码 java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 编写servlet import com.zyy.pojo.User; import com.zyy.service.user.UserService; import com.zyy.service.user.impl.UserServiceImpl; import com.zyy.util.Constants; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; /** * @ClassName: LoginServlet * @Description: TODO 类描述 * @Author: zyy * @Date: 2022/01/09 11:36 * @Version: 1.0 */ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("LoginServlet---start----"); //用户前端输入的用户名和密码 String userCode = req.getParameter("userCode"); String userPassword = req.getParameter("userPassword"); //调用业务层 UserService userService = new UserServiceImpl(); User user = userService.login(userCode, userPassword); if (user != null) { //查有此人 //将用户的信息放到session中 req.getSession().setAttribute(Constants.USER_SESSION, user); //跳转到主页 resp.sendRedirect("jsp/frame.jsp"); } else { //查无此人 //转发回登录页面,并提示它用户名或者密码错误 req.setAttribute("error", "用户名或者密码错误"); req.getRequestDispatcher("login.jsp").forward(req, resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } } ———————————————— 版权声明:本文为优快云博主「程序猿tu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.youkuaiyun.com/qq_41171409/article/details/123553501没有验证密码为什么可以实现登录
最新发布
12-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值