Warm tip: This article is reproduced from stackoverflow.com, please click
eclipse html java javascript jsp

Image not loading in chrome but works fine in eclipse inbuilt browser

发布于 2020-04-11 22:19:21

I am building a dynamic web application using eclipse javaEE . I am adding a background image to a jsp page by body tag attribute background().When I run this program in eclipse inbuilt browser image loads completely fine but when I run the same program on chrome , background image doesn't load. Please help in rectifying this problem. Thanks in advance.

Questioner
INDRESH KHANDELWAL
Viewed
78
3,540 2018-11-16 09:42

I did face exact same issue INDRESH KHANDELWAL faced in my JAVA project.

Images weren't loading in any browser and I got this error message:

Not allowed to load local resource:

But works fine in eclipse IDE. So I used this logic to solve this problem. Upload your image path into Database and fetch from there whenever you need the image. Below is the code that I used for simple login page with background image,

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>

<html>
<head>

    <%
    Class.forName("com.mysql.jdbc.Driver"); 
    java.sql.Connection con = DriverManager.getConnection
    ("jdbc:mysql://localhost:3306/cart_db","root","toor"); 
    Statement st= con.createStatement(); 
    String sql = "select * from img_table where id = 'login_bgimg'";
    ResultSet rs = st.executeQuery(sql);
    String s1 = "";
    if(rs.next())
    {   
        s1=rs.getString(2); // Image URL
    %>
     <title> Login </title>
     <link rel="stylesheet" type="text/css" href="css/Login_style.css">
 </head>
 <body background = "<%=s1%>" >
     <div class="signin">
         <form action="LoginCode.jsp" method="post">
             <h2>Sign In</h2>
             <input type="text" placeholder="Enter Username" name = "txtUserName">
             <input type="password" placeholder="Enter Password" name = "txtPWD">
             <button class = "btn">Sign In</button> <br>
             <a href="#">Forget Password?</a> | 
             <a href="#">Create an Account</a> | 
             <a href="#">Back to Home</a>
         </form>
     </div>
   <% 
   }    
   %>

 </body>
</html>

Disclaimer: All, I am not an expert. I just posted what seems to be working for me. If there is a better way to handle it, please do post it here. It will be useful to me and everyone else.