Servlet 网页重定向
当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向。当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向。
重定向请求到另一个网页的最简单的方式是使用 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 网页重定向的更多相关文章
- 关于Servlet中重定向
public class Red1Servlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...
- chrome网页重定向
使用chrome浏览器打开某网页时总会出现错误:此网页包含重定向循环 解决办法: 关闭chrome浏览器, 到你的机器的:C:\Users\username\AppData\Local\Google\ ...
- Servlet实现重定向的两种方式
使用Servlet实现请求重定向:两种方式 1. response.setStatus(302); response.setHeader("location", "/Re ...
- ASP.NET - 网页重定向 Response.Redirect()
在网页中使用重定向,意思就是在网站中的某一个页面跳转到另一个页面. Response.Redirect(~/abc.aspx); 使用“~”的作用是可以从任意位置跳转. 如果没有“~”,那么跳转的时候 ...
- servlet转发重定向
1.request.getRequestDispacther("/test.jsp").forword(request,response); 转发 浏览器URL是一个地 ...
- servlet的重定向和作用域
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- Servlet Response 重定向
重定向 response.sendRedirect("index.jsp"); //登录用户名不存在,重定向到index.jsp 1重定向在客户端发挥作用,通过浏览器重 ...
- 关于servlet中重定向、转发的地址问题
先写一个正斜杠"/",再判断是服务器使用该地址还是网站使用该地址. 访问网络资源用/,访问硬盘资源用\. 例如: 转发: request.getRequestDispat ...
- 纯html网页重定向与跳转
javaScript 跳转 方法一: <script language="javascript"> window.location = "http:// ...
随机推荐
- laravel5的Bcrypt加密方式对系统保存密码的小结
laravel5文档介绍 //对 A 密码使用Bcrypt 加密 $password = Hash::make('secret'); //你也可直接使用 bcrypt 的 function $pass ...
- Restframework 渲染器 render 组件实例-4
渲染器默认存放位置: 在默认配置下 default-settings里 (APIVIEW点击去--> 1. renderer_classes = api_settings.DEFAULT_REN ...
- day 60 Django第一天
jinjia2 : Jinja2是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity. 它能完全支持unicode,并具有集成的沙箱执行 ...
- TOJ2470
#include <stdio.h> struct node{ int x; int y; int step; }first; int zx[4]={-1,0,1,0}; int zy[4 ...
- apache测试网页执行效率
apache软件下有一个测试网页访问速度的工具ab.exe,位于apache的bin目录下,windows下使用命令行进入bin目录,执行ab.exe -n 10000 -c 10 http://12 ...
- elasticsearch 5.x Delete By Query API(根据条件删除)
之前在 2.X版本里 这个Delete By Query功能被去掉了 因为官方认为会引发一些错误 如需使用 需要自己安装插件. bin/plugin install delete-by-query 需 ...
- day 65 crm(2) admin源码解析,以及简单的仿造admin组件
前情提要: crm中的admin组件重写. 一:admin的autodiscover 作用:实现扫面该项目中的所有的admin 1:执行顺序-> 按照注册的顺序执行 二:单例模式 1:普通案例的 ...
- 【xsy1116】数学题 奥数题
真实奥数题 题目大意:给你正整数k$,r$.问你存在多少对$(x,y)$,满足$x<y$且$x^2+y^2=kz^2$,并将所有符合条件的数对输出. 数据范围:$r≤1e9$,$k={1,2,3 ...
- Ubuntu 连接手机 不识别设备 -- 解决办法
1.usb线连接手机,输入命令 $ lsusb Bus 004 Device 002: ID 8087:8000 Intel Corp. Bus 004 Device 001: ID 1d6b:000 ...
- QuantLib 金融计算
我的微信:xuruilong100 <Implementing QuantLib>译后记 QuantLib 金融计算 QuantLib 入门 基本组件之 Date 类 基本组件之 Cale ...