zoukankan      html  css  js  c++  java
  • 请求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:

     1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'index.jsp' starting page</title>
    13   </head>
    14   
    15   <body>
    16     This is my JSP page. <br>
    17     <%
    18     Object message = request.getAttribute("message");
    19     if(message!=null && !"".equals(message)){
    20 
    21  %>
    22      <script type="text/javascript">
    23          alert("<%=message%>");
    24      </script>
    25  <%} %>
    26     <form action="<%=request.getContextPath() %>/servlet/checkLogin" method="post">
    27         <input type="text" name="username" value=""/><br/>
    28         <input type="submit" name="login" value="提交"/>
    29     </form>
    30   </body>
    31 </html>

    CheckLogin.java(servlet):

     1 package servlet;
     2 import java.io.IOException;
     3 import java.net.InetSocketAddress;
     4 import java.util.concurrent.Executor;
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 import javax.servlet.http.HttpSession;
    10 public class CheckLogin extends HttpServlet{
    11     @Override
    12     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    13             throws ServletException, IOException {
    14         doPost(req, resp);
    15     }
    16     @Override
    17     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    18             throws ServletException, IOException {
    19         String username = req.getParameter("username");
    20         HttpSession session=req.getSession();
    21         if(username.equals("admin")){
    22             //登陆成功,我是重定向到其它页面,所以这里我要用session才可以把成功的信息传递给count.jsp
    23             session.setAttribute("sucess", "登陆成功!");
    24             resp.sendRedirect(req.getContextPath()+"/count.jsp");
    25         }else{
    26             //登陆失败,我有的是转发,转发request作用域是连续的,所以我这里可以用request传递失败的信息给JSp页面
    27             req.setAttribute("message", "登陆失败!");
    28             req.getRequestDispatcher("/index.jsp").forward(req, resp);
    29         }
    30     }
    31     @Override
    32     protected void service(HttpServletRequest req, HttpServletResponse resp)
    33             throws ServletException, IOException {
    34         super.service(req, resp);
    35     }
    36     
    37 }

    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(登陆成功目标页)

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>Insert title here</title>
     8 </head>
     9 <body>
    10 <%
    11     Object message = session.getAttribute("sucess");
    12     if(message!=null && !"".equals(message)){
    13 
    14  %>
    15      <script type="text/javascript">
    16          alert("<%=message%>");
    17      </script>
    18  <%} %>
    19 hello word!
    20 </body>
    21 </html>
  • 相关阅读:
    DLS的迷茫
    DFS系统
    前辈的一次培训
    BAV99 开关二极管
    学习射频IC卡 MFRC522,入手了一块板子
    Olink出新版本了 兼容JlinkV8 带串口功能
    Keil中慎用Printf()
    Keil : Cannot enter Debug Mode解决方法:
    热敏打印机应用笔记
    MFRC522 晶震不起震
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/5855350.html
Copyright © 2011-2022 走看看