JavaScript的alert()与Java的response.sendRedirect()经验

示例代码:JSP文件
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>先弹出提示信息,之后点击确定再跳转向新的地址</title>
</head>
<body>
<%
String url=request.getParameter("url");
String hasdays=request.getParameter("hasdays");
%>
<script type="text/javascript">
alert("系统提示:您的账户马上就要到期了][剩余"+<%=hasdays%>+"天");
window.location.href="<%=url%>";
</script>
系统提示:您的账户马上就要到期了!!剩余<%=hasdays%>天;
<%
response.sendRedirect(url);
%>
</body>
</html>

本来是希望上述代码实现:先提示将要到期的信息,之后再点击确定才跳转向response.sendRedirect指向的地址
可是由于jsp中嵌入的java代码是在服务器端就执行了的,所以上述代码的执行效果并不能满足既定的功能设想,而
是不会出现弹出信息,直接就执行了response.sendRedirect!
为了实现上述的功能设想我们只有利用JavaScript来代替response.sendRedirect完成地址的跳转,代码如下:
<%@ page contentType="text/html; charset=gb2312"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>先弹出提示信息,之后点击确定再跳转向新的地址</title>
</head>
<body>
<%
String url=request.getParameter("url");
String hasdays=request.getParameter("hasdays");
%>
<script type="text/javascript">
alert("系统提示:您的账户马上就要到期了][剩余"+<%=hasdays%>+"天");
window.location.href="<%=url%>"; <!--JavaScript代替response.sendRedirect做跳转-->
</script>
系统提示:您的账户马上就要到期了,剩余<%=hasdays%>天;
</body>
</html>

OK!! 问题解决了!!
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>学生管理系统</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <jsp:useBean id="link" class="jdbcHomework.LinkMysql"></jsp:useBean> <% request.setCharacterEncoding("utf-8"); String uid = request.getParameter("sid"); String uname = request.getParameter("sname"); String sex = request.getParameter("ssex"); String banji = request.getParameter("banji"); String yuanxi = request.getParameter("yuanxi"); System.out.println(uid+uname+sex+banji+yuanxi); Connection con=null; PreparedStatement ps=null; con=link.getConnection(); String sql="update student set Sname=?,Ssex=?,Sclass=?,Syuanxi=? where Sid=?"; ps=con.prepareStatement(sql); ps.setString(1,uname); ps.setString(2, sex); ps.setString(3,banji); ps.setString(4,yuanxi); ps.setString(5,uid); int i=ps.executeUpdate(); if(i>0){ System.out.println("修改成功!"); response.sendRedirect("linkMysql-2.jsp"); //out.print("<script type='javascript'> alert('学生信息修改成功!');window.location='linkMysql.jsp';</script>"); } else{ System.out.println("修改失败!"); response.sendRedirect("linkMysql-2.jsp"); //out.print("<script type='javascript'> alert('学生信息修改失败!');window.location='login.jsp';</script>"); } ps.close(); con.close(); %> </body> </html>
06-02
.制作一个注册表单,表单元素要包含文本框、密码框、多行文本框,单选按钮、下拉菜单,复选框等。提交到另一个jsp页面,在该页面中输出上述表单元素输入或选择的值(如果在提交之前能进行JS验证更好)。 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>输入表单</title> </head> <body> <form action="outputResult.jsp" method="post"> 请输入内容: <input type="text" name="inputText"><br><br> <input type="submit" value="提交"> </form> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>结果页面</title> </head> <body> <% // 获取表单提交的数据 String inputText = request.getParameter("inputText"); if (inputText != null && !inputText.isEmpty()) { out.println("您输入的内容是:" + inputText); } else { out.println("未接收到任何输入!"); } %> </body> </html> <%@ page language="java" contentType="text/html; charset=gb2312"%> <html> <body> 请您输入自己的信息进行注册 <form action="checkForm_result.jsp" method="post"> 请您选择您的爱好: <input name="fav" type="checkbox" value="sing">唱歌 <input name="fav" type="checkbox" value="dance">跳舞 <input name="fav" type="checkbox" value="ball">打球 <input name="fav" type="checkbox" value="game">打游戏<BR> <input type="submit" value="注册"> </form> </body> </htmI> <%@ page language="java" contentType="text/html; charset=gb2312"%> <html> <body> <% String[] fav = request.getParameterValues("fav"); out.println("爱好为:"); for(int i=0;i<fav.length;i++){ out.println(fav[i]); } %> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <body> <form action="loginchuli.jsp" method="POST"> 请输入用户名:<input type="text" name="username"><br> 请输入密码:<input type="password" name="pwd"><br> <input type="submit" value="登录"> </form> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <body> <% String username = request.getParameter("username"); String password = request.getParameter("pwd"); // 添加空值检查和安全验证 if(username != null && password != null) { if ("1921801z".equals(username)) { // 避免NullPointerException session.setAttribute("loginname", username); out.println("登录成功!即将跳转到测试页面..."); // 添加自动跳转 response.setHeader("Refresh", "3;url=test1.jsp"); } else { response.sendRedirect("login.jsp"); } } else { response.sendRedirect("login.jsp"); } %> </body> </html> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <% String login = (String)session.getAttribute("loginname"); if(login == null) { response.sendRedirect("login.jsp"); } %> <body> <h2>测试页面</h2> <p>欢迎, <%= login %>!</p> <p>只有登录用户才能看到此内容</p> </body> </html>
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值