请求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 ...
随机推荐
- 常用 redis 命令(for php)
Redis 主要能存储 5 种数据结构,分别是 strings,hashes,lists,sets 以及 sorted sets. 新建一个 redis 数据库 $redis = new Redis( ...
- parsing XML document from class path resource
遇到问题:parsing XML document from class path resource [spring/resources] 解决方法:项目properties— source—remo ...
- spring boot/cloud 应用监控
应用的监控功能,对于分布式系统非常重要.如果把分布式系统比作整个社会系统.那么各个服务对应社会中具体服务机构,比如银行.学校.超市等,那么监控就类似于警察局和医院,所以其重要性显而易见.这里说的,监控 ...
- nios II--实验7——数码管IP软件部分
软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...
- SpringMVC学习--参数绑定
spring参数绑定过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接收 ...
- jq实现登陆页面的拖拽功能
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <script src ...
- C#查看各种变量的指针地址
将项目的“可编译不安全代码”属性设置为true就可以了,方法如下:项目属性对话框->配置属性->生成->允许不安全代码块 namespace Pointer { struct XYZ ...
- 我的防Q+
Q+链接: http://onemore.web-45.com/index1.html: 兼容IE8: __页面被主机屋收回去了,现在又在弄自己的服务器,稍等呗
- 区间DP lightoj 1031
在此游戏中任意时刻的状态都是原始序列的一段子序列故: 定义d(i, j) : 表示原来序列的第i ~ j个元素组成的子序列,在双方都采取最优策略的情况下,先手得分的最大值. 状态转移时,需要枚举从左边 ...
- WakeLock, AlarmManager, JobScheduler
应用程序耗电的实质,是所启用的硬件在消耗电量. 手机的耗电单元 CPU: 应用处理器(AP)和基带处理器(BB或BP) GPU(图形处理单元) 外设:wifi,BT, GPS,LCD等 AP是ARM架 ...