请求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>
上一篇:BCM wifi分析


下一篇:android——wifi系统架构