博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet的使用——重定向和转发
阅读量:3963 次
发布时间:2019-05-24

本文共 2819 字,大约阅读时间需要 9 分钟。

网络路径问题

路径一共分为相对路径、绝对路径和根路径。

相对路径

用在同一个网站中, aaa/1.jpg,仅限静态资源,如果页面比较多,并且使用框架,会出现混乱。

绝对路径

用在不同网站之间的跳转,比如:http://www.baidu.com/aaa/1.jpg

根路径

http://localhost:8080/day12web1/loginservlet

(1)如果是作用于服务器,根指定就是主机名(服务器) 【/】 表示 (/day12web1)

(2)如果是作用于浏览器,就是在页面中写的路径,【/】标示(http://localhost:8080),要写服务器名

一、重定向

重定向就是通过各种方法将网络请求重新定个方向转到其它位置。

1.1 原理

在这里插入图片描述

1.2 特点

  1. 重定向是客户端行为。
  2. 重定向是浏览器做了至少两次的访问请求。
  3. 重定向浏览器地址改变。
  4. 重定向两次跳转之间传输的信息会丢失(request范围)。
  5. 重定向可以指向任何的资源,包括当前应用程序中的其他资源,同一个站点上的其他应用程序中的资源,其他站点的资源。
    注意:传递给HttpServletResponse.sendRedirect 方法的相对URL以“/”开头,它是相对于整个WEB站点的根目录(作用于浏览器)

1.3 代码实现

@WebServlet(name = "LoginServlet", value = "/loginservlet")public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); //重定向原理就是通过响应头Headers下的Location实现的 String username = request.getParameter("username"); String password = request.getParameter("password"); if (username.equals("admin") && password.equals("123456")) {
//登录成功,重定向,跳转首页 //这个方法是给浏览器使用,根目录必须有服务器名 response.sendRedirect("/0831web1/index.html"); } else {
//登录失败 response.getWriter().println("用户名或密码错误"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); }}

二、转发

2.1 原理

在这里插入图片描述

2.2 特点

  1. 转发是服务器行为
  2. 转发是浏览器只做了一次访问请求
  3. 转发浏览器地址不变
  4. 转发两次跳转之间传输的信息不会丢失,所以可以通过request进行数据的传递
  5. 转发只能将请求转发给同一个WEB应用中的组件
    注意:如果创建RequestDispatcher 对象时指定的相对URL以“/”开头,它是相对于当前WEB应用程序的根目录(所用于服务器)

2.3 代码实现

@WebServlet(name = "RegistServlet", value = "/registservlet")public class RegistServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); //可以将转发代码的response.setContentType消除 //或者将login.html改为jsp //response.setContentType("text/html;charset=utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String repassword = request.getParameter("repassword"); String email = request.getParameter("email"); System.out.println("用户名" + username + ",密码" + password + ",邮箱:" + email); System.out.println("注册成功"); //获取转发器,然后forwoard执行转发,这里地址只能使用相对地址和根地址,只能转发本网站 //该方法是在服务器中使用,所以/就代表了服务器名 request.getRequestDispatcher("/login.html").forward(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); }}

转载地址:http://asgzi.baihongyu.com/

你可能感兴趣的文章
如何查看进程的信息(线程数)
查看>>
Linux中的chage命令
查看>>
linux-详细解析密码文件passwd与shadow
查看>>
su- 与su的区别
查看>>
linux下发邮件mail
查看>>
echo如何手动输出换行
查看>>
身份证的正确使用方法——非常重要的知识
查看>>
ExtJS & Ajax
查看>>
Tomcat在Windows下的免安装配置
查看>>
JMeter常用测试元件
查看>>
JMeter——使用技巧
查看>>
Hibernate 实体层设计--Table per subclass
查看>>
Ruby解决方案:The 'ffi' native gem requires installed build tools ; 含最新DevKit下载地址
查看>>
Python之操作MySQL数据库(二)
查看>>
简单介绍如何使用robotium进行自动化测试
查看>>
Python之操作XML文件
查看>>
eclipse+ADT 进行android应用签名详解
查看>>
Robotium只有apk文件例如Music.apk
查看>>
UI自动化测试框架对比(二)
查看>>
Selenium-webdriver系列教程(9)——如何操作select下拉框
查看>>