我正在尝试为学校项目制作一个简单的Web应用程序。我正在使用Java Maven,使用NetBeans中的servlet作为后端并使用vuejs脚本作为前端来编写API,服务器为GlassFish 4.1.1。问题是我没有从数据库中得到任何东西!
我有一个MySQL类:
public class MySQL implements Closeable {
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, null, ex);
}
}
ThreadLocal<Connection> conn=new ThreadLocal<>();
String url;
String user;
String pass;
public MySQL(String server, String schema, String user, String pass) {
this.url = String.format("jdbc:mysql://%s/%s",server,schema);
//conn = DriverManager.getConnection(url, user, pass);
this.user=user;
this.pass=pass;
}
private Connection getConnection() throws SQLException {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException ex){
Logger.getLogger(MySQL.class.getName()).log(Level.SEVERE, url);
}
Connection c=conn.get();
if(c==null){
c=DriverManager.getConnection(url, user, pass);
conn.set(c);
}
return c;
}
public ResultSet query(String sql, Object ... params) throws SQLException{
PreparedStatement s=getConnection().prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
for(int i=0; i<params.length; i++) {
s.setObject(i+1, params[i]);
}
return s.executeQuery();
}
public List<Integer> execute(String sql, Object ... params) throws SQLException {
PreparedStatement s=getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for(int i=0; i<params.length; i++) {
s.setObject(i+1, params[i]);
}
s.execute();
ResultSet r=s.getGeneratedKeys();
List<Integer> keys=new ArrayList<>();
while(r.next()) {
keys.add(r.getInt(1));
};
r.close();
s.close();
return keys;
}
public void close() {
try {
getConnection().close();
} catch (SQLException ex) {
}
}
}
我有一个在每个servlet中使用的Util类:
public class Util {
public static void copy(InputStream in, OutputStream out) throws IOException {
byte buffer[] = new byte[32 * 1024];
while (true) {
int t = in.read(buffer);
if (t <= 0) {
break;
}
out.write(buffer, 0, t);
}
}
public static void writeResult(HttpServletResponse response, String message) throws IOException {
response.setContentType("application/json");
JSONObject res = new JSONObject();
res.put("result", message);
try (PrintWriter out = response.getWriter()) {
out.print(res.toString());
}
}
public static String getContent(HttpServletRequest request) throws IOException {
BufferedReader r=request.getReader();
StringBuilder sb=new StringBuilder();
char[] buf=new char[2048];
int cnt;
do {
cnt=r.read(buf);
sb.append(buf,0,cnt);
} while(cnt==buf.length);
return sb.toString();
}
public static JSONObject getJSON(HttpServletRequest request) throws IOException {
return new JSONObject(getContent(request));
}
}
我的servlet,用于从数据库读取数据:
@WebServlet(name = "Centri", urlPatterns = {"/centri/api"})
public class Centri extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws SQLException, ServletException, IOException {
response.setContentType("application/json");
JSONArray jsa = new JSONArray();
String id = request.getParameter("cnt_id");
String ime = request.getParameter("cnt_ime");
String planina = request.getParameter("cnt_planina");
String duzStaza = request.getParameter("cnt_duzStaza");
try (ResultSet rs = Main.db.query("SELECT * FROM centri")) {
while (rs.next()) {
JSONObject obj = Util.getJSON(request);
obj.put("id", rs.getInt("cnt_id"));
obj.put("ime", rs.getString("cnt_ime"));
obj.put("planina", rs.getString("cnt_planina"));
obj.put("duzStaza", rs.getString("cnt_duzStaza"));
jsa.put(obj);
}
Util.getContent(request);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(Centri.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (SQLException ex) {
Logger.getLogger(Centri.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我的前vue js:
const Centri = {
data() {
return{
centri: []
}
},
methods: {
getCentri() {
axios.get("http://localhost:8080/ItaProjekatTest/centri", null, data => this.centri = data)
}
},
mounted() {
this.getCentri()
},
template: ` <div class="cent">
<table>
<tr><th>ID</th></tr><tr><th>Ime</th></tr><tr><th>Planina</th></tr><tr><th>Duzina Staza</th></tr>
<tr v-for="centar in centri">
<td>{{centar.id}}</td>
<td>{{centar.ime}}</td>
<td>{{centar.planina}}</td>
<td>{{centar.duzStaza}}</td>
</tr>
</table>
</div>`
}
POM XML:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
我从控制台日志中得到的错误是:
错误:请求失败,状态码为404 3 axios.min.js:8:6410
很抱歉,如果这些时间太长,但是我是初学者,需要答案。