請求servlet操作成功後,在JSP頁面彈出提示框
阿新 • • 發佈:2019-02-05
應用環境:
點選前臺頁面,執行某些操作。後臺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); }
前臺處理程式碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
);
|
上面的例子是把資訊放入整個會話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>