使用jsp在服务器上运行时找不到路径 [英] fail to find the path when running on the server using jsp
问题描述
下面的所有代码都是用jsp编写的.当我启动服务器时,它应该警告json类型.当我不在服务器上运行以下代码时,它实际上可以工作.我怀疑文件路径是否存在问题. data.json的路径为 C:\ Users \ fujimaro \ Desktop \ data.json ,否则我不知道可能出了什么问题.
All the code below are written in jsp. When I start the server, it should alert the json type. When I run the code below not on the server, it actually works. I suspect if is there something wrong with my file path. The the path of the data.json is C:\Users\fujimaro\Desktop\data.json , otherwise I have no clue what could be wrong.
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.tutorials.*"%>
<%@page import="javax.swing.*"%>
<%@page import="com.tutorials.*"%>
<%@page import="java.io.FileNotFoundException"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>noName</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="javascrip.js"></script>
<script type="text/javascript" src="data.json"></script>
</head>
<script type="text/javascript">
$(document).ready(function() {
var mydata = JSON.parse(data);
var arr = [];
for(var x in mydata) {
arr.push(mydata[x].name);
arr.push(mydata[x].age);
}
for(var y in arr ) {
alert(arr[y]);
}
});
</script>
Json
Json
data ='[{"name":"Harry","age":"32"}]';
data = '[{"name" : "Harry", "age" : "32"}]';
推荐答案
在您的JSP(将其命名为showJson.jsp)中,您将像这样加载javascript.js和data.json:
In your JSP (lets name it showJson.jsp) you loads javascript.js and data.json like this:
<script type="text/javascript" src="javascript.js"></script>
<script type="text/javascript" src="data.json"></script>
在src
中,您直接提供文件名而没有前缀.
因此, data.json,javascript.js和您的showJson.jsp应该位于同一文件夹中.
In the src
you give the filenames directly without prefix.
Therefore the data.json, javascript.js and your showJson.jsp should be in the same folder.
glassfish 4(java ee)中的项目结构如下:
The project structure in glassfish 4 (java ee) look like this:
当文件直接位于WebContent
-文件夹中时,可以使用以下网址进行访问:
When the files are directly in WebContent
-Folder, they can be access with these urls:
- http://localhost:8080/projectName/showJson.jsp
- http://localhost:8080/projectName/data.json
- http://localhost:8080/projectName/javascript.js
- http://localhost:8080/projectName/showJson.jsp
- http://localhost:8080/projectName/data.json
- http://localhost:8080/projectName/javascript.js
但是应该先部署它们.部署基本上是在创建 Web存档文件projectName.war并将其移入特定的服务器文件夹.即:
But they should be first deployed. The deployment is basically creating an Web Archive file projectName.war and moving it in a specific server folder. i.e.:
C:\ glassfish4 \ glassfish \ domains \ domain1 \ applications \ domain1
C:\glassfish4\glassfish\domains\domain1\applications\domain1
在大多数情况下,此projectName.war将提取为:
This projectName.war will be (in most cases) extracted to:
C:\ glassfish4 \ glassfish \ domains \ domain1 \ applications \ domain1 \ projectName_war
C:\glassfish4\glassfish\domains\domain1\applications\domain1\projectName_war
这篇关于使用jsp在服务器上运行时找不到路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!