Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重定向前来自java文件的警报_Javascript_Java - Fatal编程技术网

Javascript 重定向前来自java文件的警报

Javascript 重定向前来自java文件的警报,javascript,java,Javascript,Java,我在我的JSP页面上有一个表单,它在提交时会转到一个Java文件。执行某些步骤后,此Java文件重定向到另一个JSP页面 在重定向之前,我想显示一个关于表单提交是否成功的警报。在其他JSP页面中,我使用javascript alert()函数来执行此操作。我不能这样做,因为这是一个java文件 我试着使用JOptionPane,但警告框没有出现在broswer页面上,而是出现在我的桌面上(这是奇怪的还是应该是正常的…)。我尝试了println,结果只打印到控制台 那么,我是否可以在.java文件

我在我的JSP页面上有一个表单,它在提交时会转到一个Java文件。执行某些步骤后,此Java文件重定向到另一个JSP页面

在重定向之前,我想显示一个关于表单提交是否成功的警报。在其他JSP页面中,我使用javascript alert()函数来执行此操作。我不能这样做,因为这是一个java文件

我试着使用JOptionPane,但警告框没有出现在broswer页面上,而是出现在我的桌面上(这是奇怪的还是应该是正常的…)。我尝试了println,结果只打印到控制台

那么,我是否可以在.java文件中调用Javascript警报,或者以其他方式显示警报?java文件中的所有内容都在警报中起作用

Java文件

private void alert(String msg){

        //this doesn't print to console, doesn't show alert
        PrintWriter out = new PrintWriter(System.out);
        out.println("<script type=\"text/javascript\">");
        out.println("alert(" + msg + ");");
        out.println("</script>");

        //This prints to console only
        /*System.out.println("<script type=\"text/javascript\">");
        System.out.println("alert(" + msg + ");");
        System.out.println("</script>");*/
    }

protected void doPost(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException, IOException {
        // gets values of text fields
        String name = request.getParameter("name");

        InputStream inputStream = null; // input stream of the upload file

        // obtains the upload file part in this multipart request
        Part filePart = request.getPart("file");
        if (filePart != null) {
            inputStream = filePart.getInputStream();
        }

        Connection conn = null; // connection to the database

        try {
            // connects to the database
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = DriverManager.getConnection(dbURL, dbUser, dbPass);

            // constructs SQL statement
            String sql = "INSERT INTO table(name, image) values (?, ?)";
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, name);

            if (inputStream != null) {
                statement.setBlob(2, inputStream);
            }

            // sends the statement to the database server
            int row = statement.executeUpdate();
            if (row > 0) {
                //This ends up appearing on my desktop instead of inside the browser.
                //JOptionPane.showMessageDialog(null, "Input successful");

                //**THIS is the alert that doesn't show up** 
                alert("Input successful");

            }
        } catch (SQLException ex) {
            ex.printStackTrace(); 
        } finally {
            if (conn != null) {
                // closes the database connection
                try {
                    conn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            response.sendRedirect("home.jsp");
        }
    }
private void警报(字符串消息){
//这不会打印到控制台,不会显示警报
PrintWriter out=新的PrintWriter(System.out);
out.println(“”);
out.println(“警报(“+msg+”)”);
out.println(“”);
//这只打印到控制台
/*System.out.println(“”);
System.out.println(“警报(“+msg+”);”);
System.out.println(“”)*/
}
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
//获取文本字段的值
字符串名称=request.getParameter(“名称”);
InputStream InputStream=null;//上传文件的输入流
//获取此多部分请求中的上载文件部分
Part filePart=request.getPart(“文件”);
if(filePart!=null){
inputStream=filePart.getInputStream();
}
连接conn=null;//与数据库的连接
试一试{
//连接到数据库
registerDriver(新的com.mysql.jdbc.Driver());
conn=DriverManager.getConnection(dbURL、dbUser、dbPass);
//构造SQL语句
String sql=“插入表(名称、图像)值(?,)”;
PreparedStatement=conn.prepareStatement(sql);
语句.集合字符串(1,名称);
如果(inputStream!=null){
语句.setBlob(2,inputStream);
}
//将语句发送到数据库服务器
int row=statement.executeUpdate();
如果(行>0){
//这最终会出现在我的桌面上,而不是浏览器中。
//showMessageDialog(null,“输入成功”);
//**这是未显示的警报**
警报(“输入成功”);
}
}catch(SQLException-ex){
例如printStackTrace();
}最后{
如果(conn!=null){
//关闭数据库连接
试一试{
康涅狄格州关闭();
}catch(SQLException-ex){
例如printStackTrace();
}
}
sendRedirect(“home.jsp”);
}
}

将HTML放在servlet中确实是一件需要避免的事情。我个人会将其转换为AJAX调用,或者,至少,首先重定向到“success.jsp”(或类似的东西),然后从那里重定向到“home.jsp”。这不是最好的用户体验,但您的设计相当具有挑战性。

不确定本例中的Ajax调用。我明白你的想法。我想如果我找不到其他的选择,我会把它作为我最糟糕的计划:)。