当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向。当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向。

  重定向请求到另一个网页的最简单的方式是使用 response 对象的 sendRedirect() 方法。

转发页面跳转

1.request.getRequestDispatcher("//WEB-INF/jsp/reg.jsp").forward(request,response);
2.response.sendRedirect("/web/user?m=login");
 
public class UserController extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doGet(request,response);
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受客户端发送的参数
String m = request.getParameter("m");
if("reg".equals(m)){
//显示注册界面
//1.转发
request.getRequestDispatcher("//WEB-INF/jsp/reg.jsp").forward(request,response);
//跳转页面,forward执行转发 需要request,response两个参数
}if("regDo".equals(m)){ //执行注册 将获取参数注册到数据库中
regDo(request,response);
}else if("login".equals(m)) {
//转发
//获取分发器
RequestDispatcher dis = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
//执行转发
dis.forward(request, response);
}
}
  private void loginDo(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username =request.getParameter("username");
String password =request.getParameter("password");
UserEntity user =SqlUtil.login(username,password); //返回对象
if(user !=null){ //对象不为空登录成功
response.sendRedirect("/web/user?m=main");
}else { //登录失败
response.sendRedirect("/web/user?m=login");
} }



Servlet 网页重定向的更多相关文章

  1. 关于Servlet中重定向

    public class Red1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...

  2. chrome网页重定向

    使用chrome浏览器打开某网页时总会出现错误:此网页包含重定向循环 解决办法: 关闭chrome浏览器, 到你的机器的:C:\Users\username\AppData\Local\Google\ ...

  3. Servlet实现重定向的两种方式

    使用Servlet实现请求重定向:两种方式 1. response.setStatus(302); response.setHeader("location", "/Re ...

  4. ASP.NET - 网页重定向 Response.Redirect()

    在网页中使用重定向,意思就是在网站中的某一个页面跳转到另一个页面. Response.Redirect(~/abc.aspx); 使用“~”的作用是可以从任意位置跳转. 如果没有“~”,那么跳转的时候 ...

  5. servlet转发重定向

    1.request.getRequestDispacther("/test.jsp").forword(request,response);     转发   浏览器URL是一个地 ...

  6. servlet的重定向和作用域

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  7. Servlet Response 重定向

    重定向 response.sendRedirect("index.jsp");       //登录用户名不存在,重定向到index.jsp 1重定向在客户端发挥作用,通过浏览器重 ...

  8. 关于servlet中重定向、转发的地址问题

    先写一个正斜杠"/",再判断是服务器使用该地址还是网站使用该地址. 访问网络资源用/,访问硬盘资源用\. 例如: 转发:      request.getRequestDispat ...

  9. 纯html网页重定向与跳转

    javaScript 跳转 方法一: <script language="javascript">    window.location = "http:// ...

随机推荐

  1. iis部署 .net core webapi

    iis部署 .net core webapi 1.修改应用程序池: IIS 发布站点,这里就不介绍 IIS 安装等.这里要修改的是应用程序池,选择“无托管代码”: 2.下载安装.net core托管捆 ...

  2. Bit Manipulation-476. Number Complement

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

  3. Centos出现-bash: unzip: command not found的解决办法

    利用unzip命令解压缩的时候,出现-bash:  unzip: command not found的错误. unzip——命令没有找到,其原因肯定是没有安装unzip. 利用一句命令就可以解决了.  ...

  4. SELECT 三级联动 [转]

    <!DOCTYPE html> <html> <head> <meta charset=gbk /> <title>selectList&l ...

  5. jQuery选择器(基础及应用)

    jQuery选择器 jQuery的核心思想就是:选取元素,对其操作. jquery选择器对开发有以下优势:写法简洁,不需要考虑主流浏览器是否支持某些选择器(jquery支持css1-css3),不需要 ...

  6. php7 改为从栈上分配内在的思路

    php7的特点是规则上不从堆上分配内存,改为从栈上分配内存, 因为有些场景是从堆上分配内在后,还要手动释放内存,利用栈分配内在快的特点,在有需要的时候,再在堆上分配内在 但是栈上分配的内存,不能返回, ...

  7. POJ 1082

    #include <iostream> using namespace std; int main() { //freopen("acm.acm","r&qu ...

  8. linux 目录结构+常用命令+压缩命令+vim使用+及基础知识

    linux目录架构 / 根目录 /bin 常用的命令 binary file 的目录 /boot 存放系统启动时必须读取的档案,包括核心 (kernel) 在内 /boot/grub/menu.lst ...

  9. (转)python学习笔记5--decimal

    原文:https://blog.csdn.net/lemonwyc/article/details/37583125 上一节提到了除了基本类型之外的decimal,这节就学习下.查看python3.4 ...

  10. (转)http://blog.csdn.net/renfufei/article/details/38474435

    原文:http://blog.csdn.net/renfufei/article/details/38474435