请求servlet操作成功后,在JSP页面弹出提示框
应用环境:
点击前台页面,执行某些操作。后台action/servlet 执行后,返回处理结果(成功、失败、原因、状态等)信息。在前台jsp进行弹窗显示,alert();
后台处理代码:(把要提示的数据放到session中。)
if(flag){
message="分类修改成功";
req.getSession().setAttribute("message", message);
resp.sendRedirect(req.getContextPath()+"/servlet/categoryListServlet");
}else{
message="分类修改失败";
req.getSession().setAttribute("message", message);
}
前台处理代码:
<%
String mess=(String)session.getAttribute("message");
if("".equals(mess) && mess==null){ } else{%>
<script type="text/javascript">
alert("<%=mess%>");
</script> session.setAttribute("message", "");
<% }%>
上面的例子是把信息放入整个会话session中,这个session对整个用户请求都有效,在Session不过期时间之内都有效,其实有时候是要分别来对待的,比如新增、删除、修改、登陆成功等操作,我们操作成功后,是要重定向到目标页面,重定向request作用域不能延伸,所以我们要用Session存放弹框信息,但如果是转发到目标页面,我们就可以用request了
登陆实例:
index.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
</head> <body>
This is my JSP page. <br>
<%
Object message = request.getAttribute("message");
if(message!=null && !"".equals(message)){ %>
<script type="text/javascript">
alert("<%=message%>");
</script>
<%} %>
<form action="<%=request.getContextPath() %>/servlet/checkLogin" method="post">
<input type="text" name="username" value=""/><br/>
<input type="submit" name="login" value="提交"/>
</form>
</body>
</html>
CheckLogin.java(servlet):
package servlet;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CheckLogin extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
HttpSession session=req.getSession();
if(username.equals("admin")){
//登陆成功,我是重定向到其它页面,所以这里我要用session才可以把成功的信息传递给count.jsp
session.setAttribute("sucess", "登陆成功!");
resp.sendRedirect(req.getContextPath()+"/count.jsp");
}else{
//登陆失败,我有的是转发,转发request作用域是连续的,所以我这里可以用request传递失败的信息给JSp页面
req.setAttribute("message", "登陆失败!");
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
super.service(req, resp);
} }
web.xml
<servlet>
<servlet-name>checkLogin</servlet-name>
<servlet-class>servlet.CheckLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checkLogin</servlet-name>
<url-pattern>/servlet/checkLogin</url-pattern>
</servlet-mapping>
count.jsp(登陆成功目标页)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Object message = session.getAttribute("sucess");
if(message!=null && !"".equals(message)){ %>
<script type="text/javascript">
alert("<%=message%>");
</script>
<%} %>
hello word!
</body>
</html>
请求servlet操作成功后,在JSP页面弹出提示框的更多相关文章
- SilverLight 页面后台方法XX.xaml.cs 创建JS,调用JS ,弹出提示框
1.Invoke和InvokeSelf [c-sharp] view plaincopy public partial class CreateJSDemo : UserControl { publi ...
- PHP弹出提示框并跳转到新页面即重定向到新页面
本文为大家介绍下使用PHP弹出提示框并跳转到新页面,也就是大家所认为的重定向,下面的示例大家可以参考下 这两天写一个demo,需要用到提示并跳转,主要页面要求不高,觉得没必要使用AJAX,JS等, ...
- iOS bug 之 H5 页面没有弹出提示框
描述:在安卓上有提示框,但是在iOS上没有提示框. step 1: 失误,是我没有在正确的位置设置网址. step 2: 修改之后,测试页能弹出提示框,但是正式的页面没有提示框. step 3: 我输 ...
- bootstrap-table:操作栏点击编辑按钮弹出模态框修改数据
核心代码: columns: [ { checkbox:true //第一列显示复选框 }, ... { field: 'fail_num', title: '失败数' }, { field: 'op ...
- 最完美解决方案:js弹出窗口关闭当前页面,而不弹出提示框
该功能主要用于业务系统中的登录操作,登录后弹出全屏的业务系统主界面,而不需要工具栏.地址栏.菜单等功能. 之前针对不同浏览器找了无数种方法,包括网上能查到的所有方法,弹出的结果都不理想.结果有的IE6 ...
- C# ASP response.write()弹出提示框后页面布局被打乱
发现在使用了response.write后样式发生了变化,位置和字体都不正确.Response.Write("<script>alert(')</script>&qu ...
- javascript的关于刷新页面给出提示框的代码
// 页面刷新事件 ,或者关闭事件的3中方法!测试都可以!参考官方文档: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHan ...
- js 刷新当前页面会弹出提示框怎样将这个提示框去掉
//禁止刷新提示window.onbeforeunload = function() { var n = window.event.screenX - window.screenLeft; var b ...
- javascript;先弹出提示框,再跳转到其他页面。
context.Response.Write("<script>alert('删除成功!" + r.ToString() + "条');window.loca ...
随机推荐
- 如何在 ie6 中使用 "localStorage"
好吧,我只是个标题党,ie6 下根本无法使用跟 h5 沾边的 localStorage.今天要向大家介绍的是 ie 特有的 userData 的存储方式,并且对它进行封装,使得不支持 localSto ...
- 一次由于开启 Safari 无痕浏览 引发的艰难“捉虫”事件
事件回顾 做了一个移动端的页面,测试的时候出现了一个诡异的 bug.别的浏览器都好好的,就 ios 的 Safari 浏览器页面停止了渲染,似乎是有一段 js 文件没有载入.但是奇怪的是,同一型号的 ...
- 一款炫酷的幻灯片播放框架介绍(附demo及使用方法)
废话不多说,先上demo(建议在chrome下打开 F键全屏 esc退出全屏): 我的demo-博客园简介 官网demo 更多demo 今天为大家介绍一款基于css3和JavaScript的幻灯片播放 ...
- 制作鼠标移动到div上面显示弹出框
<div class="show-dialog hide"> <header> <div class="note"> < ...
- OS存储器管理(三) 虚拟存储器
基本概念与实现 1)局部性原理 在一段时间内,运行的作业程序仅访问(涉及到)一部分作业代码,即不会涉及整个地址空间.即在一段时间间隔内,仅装入一部分代码,作业照样能正常运行 2)虚拟存储器的引入 作业 ...
- Oracle中序列(SEQUENCE)的使用一例
曾经在触发器中使用序列(SEQUENCE): create or replace trigger TRI_SUPPLIER before insert on SUPPLIER for each row ...
- [bzoj2286][Sdoi2011]消耗战(虚树上的DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2286 分析:对于普通的树形dp:f[x]=min(∑f[son],m[x]),其中f[ ...
- [ZOJ2760]How Many Shortest Path(floyd+最大流)
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...
- android服务之启动方式
服务有两种启动方式 通过startService方法来启动 通过bindService来开启服务 布局文件 在布局文件中我们定义了四个按键来测试这两种方式来开启服务的不同 <?xml versi ...
- mysql 根据字段重复 删除 保留一条
delete from TableName where id not in (select minid from (select min(id) as minid from TableName gro ...