请求servlet操作成功后,在JSP页⾯弹出提⽰框
应⽤环境:
点击前台页⾯,执⾏某些操作。后台action/servlet 执⾏后,返回处理结果(成功、失败、原因、状态等)信息。在前台jsp进⾏弹窗显⽰,alert();
后台处理代码:(把要提⽰的数据放到session中。)
if(flag){
message="分类修改成功";
resp.ContextPath()+"/servlet/categoryListServlet");
}else{
message="分类修改失败";
}
前台处理代码:
1 2 3 4 5 6 7 8 9 10 11 12 13<%
String mess=(Attribute("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 = ContextPath();
4 String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+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 = Attribute("message");
19 if(message!=null && !"".equals(message)){
20
21 %>
22 <script type="text/javascript">
23 alert("<%=message%>");
24 </script>
25 <%} %>
26 <form action="<%=ContextPath() %>/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.InetSocketAddress;
4 import urrent.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 = Parameter("username");
20 HttpSession Session();
21 if(username.equals("admin")){
22 //登陆成功,我是重定向到其它页⾯,所以这⾥我要⽤session才可以把成功的信息传递给count.jsp
23 session.setAttribute("sucess", "登陆成功!");
24 resp.ContextPath()+"/count.jsp");
25 }else{
26 //登陆失败,我有的是转发,转发request作⽤域是连续的,所以我这⾥可以⽤request传递失败的信息给JSp页⾯
27 req.setAttribute("message", "登陆失败!");
28 RequestDispatcher("/index.jsp").forward(req, resp);
29 }
30 }
31 @Override
javaservlet和jsp的比较32 protected void service(HttpServletRequest req, HttpServletResponse resp)
33 throws ServletException, IOException {
34 super.service(req, resp);
35 }
36
37 }
<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" "/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 = Attribute("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>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论