Javascript在JSP中弹出消息

时间:2017-06-19 09:40:22

标签: javascript java mysql jsp servlets

在没有创建新的页面jsp文件的情况下,有一种方法可以在我将注册数据成功插入表格后弹出“数据插入成功”消息。 弹出的意思是没有创建新的浏览器窗口但是消息显示在当前网页的新框中,当点击“确定”时,将我重定向到TRY SECTION中的下一个网页。

<%@page import="com.mysql.jdbc.Statement"%>
<%@page import="com.mysql.jdbc.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%Class.forName("com.mysql.jdbc.Driver");%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Registration Page</title>
</head>
<body>
    <%!
     String firstname, lastname, address, phoneNo;
     String sex, password;
     %>
     <%
     firstname = lastname = address = phoneNo= "";
     sex = ""; password = "";
     firstname = request.getParameter("firstname");
     lastname = request.getParameter("lastname");
     address= request.getParameter("address").trim();
     phoneNo = request.getParameter("phoneNo");
     sex = request.getParameter("Sex");
     password = request.getParameter("password");
     if(firstname.equals("")|| lastname.equals("")|| phoneNo.equals("")||
             sex.equals("")|| password.equals(""))
     {
         out.write("Empty fields, make sure you input data into all field before pushing the  " +
                    "REGISTER button");
     }
     else
     {
         String sql1="INSERT INTO customer_details  VALUES('"+firstname+"', '"+lastname+"','"+address+"', '" +phoneNo+"',' "+sex+ "','"+password+"')"; 
         String sql2 = "INSERT INTO verification VALUES('"+firstname+"', '"+password+"')";
         try{
             Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/" + "onlineburgerapp","root", "root");
             Statement st = (Statement) conn.createStatement();
             st.executeUpdate(sql1);
             st.executeUpdate(sql2);

             conn.setAutoCommit(true);
             conn.close();
             response.sendRedirect("availableBurgers.jsp");
         }
         catch(Exception e){
             e.printStackTrace();}
         }
     %>
</body>

3 个答案:

答案 0 :(得分:2)

您可以将JavaScript代码插入JSP代码中。 例如:

<%
try{

%>
<script type="text/javascript">
    alert("Inserted Successfully");
</script>
<%
    }catch(Exception e){

    }
%>

答案 1 :(得分:1)

如果我理解你的问题:如果您的SQL请求成功,您希望向用户显示“成功”消息/弹出窗口。

您如何申请此处显示的JSP? 如果它是一个经典的webbrowser请求(让我们假设),因为最后它会重定向到availableBurgers.jsp,此页面不会向您的浏览器发送任何内容。

我只想在availableBurgers.jsp设置成功弹出窗口,因为这是您的成功页面。

答案 2 :(得分:0)

对所选好答案的改进: 如果您想使用一些变量自定义消息,则可以:

class TicketDetails: Object, Decodable {
    var working = List<[WorkingHour]>()
    var workingHours = List<WorkingHour>()

    public convenience required init(from decoder: Decoder) throws {
        self.init()
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.working = try container.decodeIfPresent(List<WorkingHour>.self, forKey: .working) ?? List<WorkingHour>.self
        self.workingHours = try container.decodeIfPresent(List<WorkingHour>.self, forKey: .working) ?? List<WorkingHour>.self
    }
}