Javascript 使用jsp在表单中插入值后,返回一条消息,其中的值插入到表单页面的顶部
我有一个表单,在按下Add按钮后,会转到DataInsert.jsp 运行插入操作后,只需插入结尾() 我想把消息“插入”并重定向到主页 我有如下内容(index.jsp中的表单):Javascript 使用jsp在表单中插入值后,返回一条消息,其中的值插入到表单页面的顶部,javascript,html,jsp,Javascript,Html,Jsp,我有一个表单,在按下Add按钮后,会转到DataInsert.jsp 运行插入操作后,只需插入结尾() 我想把消息“插入”并重定向到主页 我有如下内容(index.jsp中的表单): 姓名: 城市: 插入在:Data_insert.jsp中完成 <body> <% String name=request.getParameter("name"); String city=request.getParameter("city"); // Conn
姓名:
城市:
插入在:Data_insert.jsp中完成
<body>
<%
String name=request.getParameter("name");
String city=request.getParameter("city");
// Connect to the database using Tomcat Resources
Context ctx = null;
Context initCtx = null;
Connection con = null;
DataSource ds = null;
ctx = new InitialContext();
initCtx = (Context) ctx.lookup("java:comp/env");
ds = (DataSource) initCtx.lookup("jdbc/table_pool");
con = ds.getConnection();
PreparedStatement stmt;
String instruccionSql="INSERT INTO table (name,city) VALUES ('" + name + "','" + city + "')";
stmt = con.prepareStatement(instruccionSql);
stmt.executeUpdate();
stmt.close();
con.close();
out.println("Insertado"+"<br>");
%>
</body>
问题是“插入”消息会清除屏幕。
如果我们发出Javascript警报,
window.alert(“插入!”);
它不会显示为警报框
我们还尝试:
<script>
window.location.href = '/index.jsp';
</script>
window.location.href='/index.jsp';
它会重定向到页面,但我们不会将插入的消息视为弹出窗口
如何使“插入”消息以弹出窗口的形式出现在主页顶部。
谢谢您可以使用
setAttribute
在其中设置一些消息,根据该消息,警报将显示在您的index.jsp
页面上。如下所示:
<%
String name=request.getParameter("name");
String city=request.getParameter("city");
int row=0;
// Connect to the database using Tomcat Resources
Context ctx = null;
Context initCtx = null;
Connection con = null;
DataSource ds = null;
ctx = new InitialContext();
initCtx = (Context) ctx.lookup("java:comp/env");
ds = (DataSource) initCtx.lookup("jdbc/table_pool");
con = ds.getConnection();
PreparedStatement stmt;
String instruccionSql="INSERT INTO table (name,city) VALUES (?,?)";
stmt = con.prepareStatement(instruccionSql);
stmt.setString(1,name);
stmt.setString(2,city);
row=stmt.executeUpdate();
stmt.close();
con.close();
//if inserted
if (row > 0) {
//setting message
session.setAttribute("add", "Added Succesfully");
//redirecting
request.getRequestDispatcher("index.jsp").forward(request,response);
}
%>
或者使用EL
在您的
标签上方添加以下行:
<p style="color: red;text-align:center">${add}</p>
${add}
因此上述代码位于DataInsert.jsp
或您的index.jsp
中?谢谢Swati,是的,我在“DataInsert.jsp”的末尾添加了该部分。我还玩了在“index.jsp”末尾添加通知的游戏。但我认为最好将其添加到“DataInsert.jsp”,并显示类似“Inserted,value1=xxx,value2=yyy”的内容。如果有在“index.jsp”上工作的代码,也可以。我还尝试移动到index.jsp:window.alert(“Inserted!”);(效果稍微好一点)。我还尝试了index.jsp:var name=document.getElementById(“name”).value;window.alert(“Inserted!”+name)那么,现在你们想在同一页面或重定向页面上显示警报吗?嗨,Swati!jsp是表单页面,在后台调用DataInsert.jsp(执行真正插入的代码)。我希望看到index.jsp顶部的“Inserted!”警报。目前,如果我没有从DataInsert.jsp重定向到index.jsp,在插入之后我会看到一个空白页面。我不确定这一切是否都与:你能编辑你的问题并包含相关代码吗?现在还不清楚,没有看到更多的代码,我不能告诉你任何事情。你也可以使用DataInsert.jsp
中的一些数据setAttribute
,然后在index.jsp
上检查是否设置了值,这取决于你可以显示alert
。嗨,Swati,这两个选项都非常有效。非常感谢你!!
<% if(session.getAttribute("add") != null){ %>
<script>
alert("${add}");
</script>
<% } %>
<p style="color: red;text-align:center">${add}</p>