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

  重定向请求到另一个网页的最简单的方式是使用 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. 689. Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  2. 《Python绝技:运用Python成为顶级黑客》 用Python分析网络流量

    1.IP流量将何去何从?——用Python回答: 使用PyGeoIP关联IP地址和物理地址: 需要下载安装pygeoip,可以pip install pygeoip 或者到Github上下载安装htt ...

  3. Python小白学习之路(十八)—【内置函数三】

    一.对象操作 help() 功能:返回目标对象的帮助信息 举例: print(help(input)) #执行结果 Help on built-in function input in module ...

  4. Vue环境搭建及node安装过程整理

    一.nodejs的安装 Node.js安装包及源码下载地址为:https://nodejs.org/en/download/. 我们可以根据不同平台系统选择你需要的Node.js安装包.Node.js ...

  5. POJ 1147

    #include <iostream> #include <algorithm> #define MAXN 3005 using namespace std; int _m[M ...

  6. A Node Influence Based Label Propagation Algorithm for Community detection in networks 文章算法实现的疑问

    这是我最近看到的一篇论文,思路还是很清晰的,就是改进的LPA算法.改进的地方在两个方面: (1)结合K-shell算法计算量了节点重重要度NI(node importance),标签更新顺序则按照NI ...

  7. Codeforces Round #555 (Div. 3) c2 d e f

    c2:Increasing Subsequence (hard version) 那边小取那边,然后相等比较后面的长度 #include<bits/stdc++.h> using name ...

  8. openerp学习笔记 视图(tree\form)中隐藏按钮( 创建、编辑、删除 ),tree视图中启用编辑

    视图(tree\form)中隐藏按钮( 创建.编辑.删除 )create="false" edit="false" delete="false&quo ...

  9. 【转载】 C#中数组、ArrayList和List三者的区别

    原文地址:http://blog.csdn.net/zhang_xinxiu/article/details/8657431 在C#中数组,ArrayList,List都能够存储一组对象,那么这三者到 ...

  10. 网络游戏程序员须知 UDP vs TCP(转)

    本文为作者原创或翻译,转载请注明,不得用于商业用途. 作者:rellikt@gmail.com 首发链接:http://blog.csdn.net/rellikt/archive/2010/08/21 ...