This question already has answers here:
"Not allowed to load local resource: file:///C:....jpg" Java EE Tomcat
(8 answers)
Closed 2 years ago.
I am trying to create a simple application in which a user can add an image. The name of the image and the path of the folder where I am storing the image but when trying to retrieve it. It shows "Not allowed to load local resource: file:///H:/Projectpictures/BrowserForDataTables.PNG" Error on browser console. please have a look and let me know what is it I am doing wrong here.
Here is my code.
This is the DAOImplementation code for retrive:
#Override
#Transactional
public List<pictureModal> allImages() {
Session currentSession = sessionFactory.getCurrentSession();
Query<pictureModal> query = currentSession.createQuery("from pictureModal");
List<pictureModal> pictues= query.getResultList();
return pictures;
}
This is Controller code for getimages:
#RequestMapping("/seeAllImages")
public String getAllImage(Model theModal) {
List<pictureModal> myPictures= pictureServ.allImages();
theModal.addAttribute("myPicture",myPictures);
return "ImagePage";
}
And here is the code for JSP file:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>Pictures will display here</title>
</head>
<body>
<h2>Display Pictures </h2>
<table border="1">
<thead>
<tr>
<th>Picture Name</th>
<th>Picture</th>
</tr>
</thead>
<c:forEach var="tempPic" items="${myPicture}">
<tbody>
<tr>
<td>${tempPic.name}</td>
<td><img src="${tempPic.path}"></td>
</tr>
</tbody>
</c:forEach>
</table>
</body>
</html>
Maybe your program tries to access a location where does not have the correct rights. Maybe that is an administrator folder, and your program isn't started as an Administrator.
Related
I was going through spring MVC tutorials and came across ModelAndView.
My JSP view looks like this,
<%#taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring MVC </title>
</head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${student.getName()}</td>
</tr>
<tr>
<td>Age</td>
<td>${student.getAge()}</td>
</tr>
<tr>
<td>ID</td>
<td>${student.getBranch()}</td>
</tr>
</table>
</body>
</html>
It worked when I set both attribute name and attribute value in controller like the following,
ModelAndView mv = new ModelAndView();
mv.setViewName("result");
Student student = new Student("arun2", "CSE", 22);
mv.addObject("student",student);
return mv;
Then I came across other overloaded method ModelAndView.addObject(Object attributeValue) and I tried setting only attribute value mv.addObject(student); But this time it doesn't show student details in the browser.
My questions:
Is it possible to access those values in JSP by just setting attribute value as i did second-time mv.addObject(student);.
If yes, How? If not, Why do we have such overloaded method? Why do we need to set only value if we can't access it in the view (JSP)?
I went through the javadocs, But didn't find anything that could make me understand.
Yes, you can do it like that and access the parameters,but need to pay some attention.
Check the API for addObject at ModelAndView,it will shows method as below:
Then,let's look at the definition of ModelMap.addAttribute(Object)
It shows using a generated name,the definition of generated name listed as below:
So,if you just want to use the mv.addObject(student) method,you can access data in your jsp page as below:
<%#taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring MVC </title>
</head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${student.name}</td>
</tr>
<tr>
<td>Age</td>
<td>${student.age}</td>
</tr>
<tr>
<td>ID</td>
<td>${student.branch}</td>
</tr>
</table>
</body>
</html>
If the object is a Group class, then you can get value via ${group.name}
And pay attention that the ModelAndView is from org.springframework.web.servlet.ModelAndView
Also,I suggest you use the EL Expressions to access data in your jsp page directly.
I am currently working on a project that implies passing a aspx.net project to jsp.
The issue that I have been working on all day is as follows:
I create the following methods in a java file named DefaultMethods:
package model;
import com.merlin.MXL;
public class DefaultMethods {
public DefaultMethods() {
super();
}
public String versionNumber(){
try{
MXL mxl = new MXL();
return mxl.merlinVersion();
}
catch (Exception ex){
return "Contact MerlinXL";
}
}
public String serverName(){
try{
MXL mxl = new MXL();
return mxl.serverName();
}
catch (Exception ex){
return "Contact MerlinXL";
}
}
}
Where MXL is the webservice specifically created for this application.
So then I go to the jsp page and import the java class and call the methods in <% %> tags:
<%# page import="model.DefaultMethods"%>
<%# page language="java" contentType="text/html; charset=iso-8859-1" pageEncoding="ISO-8859-1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MerlinXL</title>
<link href="web/css/merlinxl.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="/favicon.ico" />
<style type="text/css">
Now go a series of css styles, tables, etc. And then I call the methods here:
<tr>
<td valign="top" class="style2">
<strong>
<%
DefaultMethods defaultMethods = new DefaultMethods();
String theNumber = defaultMethods.versionNumber();
%>
<label ID="labVersion" style="font-weight: 700; font-size: small; text-align: center"/>
Version <%= theNumber %>
</label>
</strong>
</td>
<td valign="top" class="auto-style1">
<p style="margin-left: 0px">
</p>
</td>
<td valign="top" class="style3">
<strong>
<%
DefaultMethods defaultMethods2 = new DefaultMethods();
String theServer = defaultMethods2.serverName();
%>
<label ID="labServer" style="font-weight: 700; font-size: small; text-align: center"/>
Server Name <%= theServer %>
</label>
</strong>
</td>
</tr>
Now, the funny thing is that the imports work fine, gives no mistakes on the client side when loading the page on the server, what's more, when I press Ctrl + Space in the IDE (JDeveloper) on the instance of DefaultMethods it autompletes giving me all the methods I have declared in the class as options. But when it loads on the server, it keeps giving me error coming up as method not found
I know I'm not coding in the best practice yet, I wanted to start a simple one to see if it worked and then go in with MVC.
Yes, I have googled, and yes, I have looked up the solutions in here, but I seem to have everything in order with my code and I'm out of ideas or just can't see the tree from the woods.
Well, needless to answer the comments, as the code worked beautifully after I opened the project up this morning.
There has to be some kind of bug in JDeveloper when creating new packages and files, odd thing is I restarted the web service to make sure that the new folders would integrate, but by the looks of it JDeveloper needs, at the very least, to be restarted in order to integrate new folders/files.
Definitely this IDE is a great let down from Oracle.
Thank you all for your comments.
I am new to Spring MVC I have setup a test DB to try to get a single flow through and have a table displayed in a JSP page. I assume I have something configured wrong but I cannot find it. Maybe a naming convention.
I have a mySql DB
A DAO Class that just does a select * from my table that works as I can step into it.
My controller just gets a list of my Run Models. No errors are thrown The JSP simple loads an empty table. The controller seems to have the correct info in it.
Model (snippet all the getters and setters seem to work as the controller gets a list of them fine)
public class QAModel {
private int idRun;
private String SuiteID;
private String run_name;
Controller Code
#RequestMapping(value="/RunList")
public ModelAndView listRun(ModelAndView model) throws IOException{
//#ModelAttribute
System.out.println("**** Controller ******");
List<QAModel> listRun = runDao.list();
model.addObject("RunList", listRun);
model.setViewName("RunList");
return model;
}
That does get a list of Run Model Objects that contain the correct DB info I can see them if I step into it. However the JSP loads a blank table.
JSP Code (I assume I'm missing some mapping or naming convention?)
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Contact Manager Home</title>
</head>
<body>
<div align="center">
<h1>Contact List</h1>
<h3>New Run</h3>
<table border="1">
<th>No</th>
<th>Suite ID</th>
<th>Run Name</th>
<c:forEach var="QAModel" items="${RunList}" varStatus="status">
<tr>
<td>${status.index + 1}</td>
<td>${QAModel.SuiteID}</td>
<td>${QAModel.run_name}</td>
<td>
Edit
Delete
</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
If you are not able to print any jstl variable then one possible reason can be that your jstl core tag is not added.
Add this to begining of your JSP file.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
This question already has answers here:
Display BLOB (image) through JSP
(4 answers)
Closed 9 years ago.
Ok so i am trying to display an image along with user first name and last name on a jsp page but i only get the image not the first name and last name...so what's the problem here? ...your help will be appreciated:)
<%# page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"%>
<%# page import="java.sql.*" %>
<%# page import="java.io.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Insert title here</title>
</head>
<body>
<H1>Fetching Data From a Database</H1>
<%
Blob image = null;
byte[] imgData = null;
Connection con;
String url="jdbc:mysql://localhost:3306/image";
String uName="root";
String pwd="root";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(url,uName,pwd);
String sql="Select * from image ";
PreparedStatement stmt=con.prepareStatement(sql);
ResultSet resultset=stmt.executeQuery();
while(resultset.next())
{
Blob bl = resultset.getBlob("image");
byte[] pict = bl.getBytes(1,(int)bl.length());
response.setContentType("image/jpg");
OutputStream o = response.getOutputStream();
%>
<TABLE BORDER="1">
<TR>
<TH>First Name</TH>
<TH>Last Name</TH>
<TH>picture</TH>
</TR>
<TR>
<td>Image</td><td><%o.write(pict);%></td>
<%o.flush();
o.close();%>
<TD> <%= resultset.getString(2) %> </TD>
<TD><%= resultset.getString(3) %></TD>
</TR>
</TABLE>
<BR>
<%
o.flush();
o.close();
}
%>
</body>
</html>
Your code includes calls to o.close() after writing the picture out and later on. That will close the response output stream, and nothing more will be sent out. Don't do that - let the web container take care of closing the output stream.
That should explain why the first name and last name shouldn't be displayed: the response stream was closed.
Also, I don't think the response content type should be "image/jpg" especially considering how the HTML meta tag says the content is "text/html". I think they should be the same - "text/html".
Finally - and maybe most importantly? - JSP is very old. You may want to consider using Facelets (JavaServer Faces technology).
I'm writing a web app that uses a servlet to maintain an ArrayList of VideoData objects (these just contain basic information about movies like the title, type of movie, etc).
The servlet puts this List in the request's scope and forwards both the request and response to a jsp (only part of the servlet code is shown here):
public class VideoServlet extends HttpServlet {
private ArrayList<VideoData> library = new ArrayList<VideoData>();
public void doGet(HttpServletRequest request,
HttpServletResponse response) {
try {
// put ArrayList in Request's scope
request.setAttribute("the_table", library);
request.getRequestDispatcher("/listvideos.jsp").forward(request,
response);
...
The listvideos.jsp is shown below. I'm getting a Tomcat error stating that the uri for the JSTL cannot be resolved. I've used EL in other parts of my jsp code without having to have any special import line like this, and I'm not sure if JSTL is still the preferred way to solve this type of problem while still trying to adhere to MVC2 and keeping all the Java code in the Servlet. Can anyone point me in the right direction here? Ideally I'd like a pure EL solution, if that's possible.
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'
'http://www.w3.org/TR/html4/loose.dtd'>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Cattle Drive Assignment Servlets-4: Videos</title>
</head>
<body>
<h1>Cattle Drive Assignment Servlets-4: Videos</h1>
<form method='post' action='/videos/VideoServlet'>
Add a video
<br>
<br>
<table border="1">
<tr>
<th>Title</th>
<th>Star</th>
<th>Type</th>
<th>VHS</th>
<th>DVD</th>
<th>Description</th>
</tr>
<c:forEach items="${the_table}" var="movie">
<tr>
<td>${movie.getTitle()}</td>
<td>${movie.getStar()}</td>
<td>${movie.getType()}</td>
<td>${movie.inVHS()}</td>
<td>${movie.inDVD()}</td>
<td>${movie.getDesc()}</td>
</tr>
</c:forEach>
</table>
</form>
</body>
</html>
Your code looks basically correct. Looks like the error you're seeing indicates that the JSTL taglibs cannot be found in the classpath. Please make sure that jstl.jar and standard.jar are in your war's WEB-INF/lib folder.