将这段代码转换为jsif (contentType==null||contentType.length()==0) { return 5; } else { final int i = contentType.indexOf('/'); if (i != -1) { contentType = contentType.substring(0, i); } switch (contentType) { case "text" -> { return 1; } case "image" -> { return 2; } case "audio" -> { return 3; } case "video" -> { return 4; } default -> { return 5; } } }
时间: 2023-06-10 18:02:42 浏览: 112
这段Java代码可以转换为以下JavaScript代码:
if (contentType == null || contentType.length == 0){
return 5;
} else {
const i = contentType.indexOf('/');
if (i !== -1) {
contentType = contentType.substring(0, i);
}
switch (contentType) {
case 'text':
return 1;
case 'image':
return 2;
case 'audio':
return 3;
case 'video':
return 4;
default:
return 5;
}
}
相关问题
将这段代码转换为js:private int contentTypeIdentification(String contentType) { if (StringUtils.isEmpty(contentType)) { return 5; } else { final int i = contentType.indexOf('/'); if (i != -1) { contentType = contentType.substring(0, i); } switch (contentType) { case "text" -> { return 1; } case "image" -> { return 2; } case "audio" -> { return 3; } case "video" -> { return 4; } default -> { return 5; } } } }
function contentTypeIdentification(contentType) {
if (contentType.length == 0 || contentType === null || contentType === undefined) {
return 5;
} else {
const i = contentType.indexOf('/');
if (i != -1) {
contentType = contentType.substring(0, i);
}
switch (contentType) {
case "text":
return 1;
case "image":
return 2;
case "audio":
return 3;
case "video":
return 4;
default:
return 5;
}
}
}
后面的代码怎么写呢
好的,下面是一个使用 JSP、Servlet 和 Ajax 实现学生信息管理的简单示例,仅供参考:
1. JSP 页面:student.jsp
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生信息管理</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function() {
// 查看学生信息
$("#btnQuery").click(function() {
$.ajax({
url: "StudentServlet",
type: "POST",
dataType: "json",
data: {
action: "query"
},
success: function(data) {
if (data.code == 0) {
var html = "";
for (var i = 0; i < data.data.length; i++) {
html += "<tr>"
+ "<td>" + data.data[i].id + "</td>"
+ "<td>" + data.data[i].name + "</td>"
+ "<td>" + data.data[i].age + "</td>"
+ "</tr>";
}
$("#tblStudent tbody").html(html);
} else {
alert(data.msg);
}
}
});
});
// 删除学生信息
$("#tblStudent").on("click", ".btnDelete", function() {
var id = $(this).data("id");
if (confirm("确定要删除学生信息吗?")) {
$.ajax({
url: "StudentServlet",
type: "POST",
dataType: "json",
data: {
action: "delete",
id: id
},
success: function(data) {
if (data.code == 0) {
alert("删除成功!");
$("#btnQuery").trigger("click");
} else {
alert(data.msg);
}
}
});
}
});
});
</script>
</head>
<body>
<h1>学生信息管理</h1>
<button id="btnQuery">查看</button>
<table id="tblStudent" border="1">
<thead>
<tr>
<th>学号</th>
<th>姓名</th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
```
2. Servlet 程序:StudentServlet.java
```java
package com.example.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.bean.Student;
import com.example.dao.StudentDao;
import com.google.gson.Gson;
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("query".equals(action)) {
query(request, response);
} else if ("delete".equals(action)) {
delete(request, response);
}
}
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Student> list = new ArrayList<Student>();
StudentDao dao = new StudentDao();
try {
list = dao.queryAll();
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
Gson gson = new Gson();
String json = gson.toJson(list);
out.write("{\"code\":0,\"data\":" + json + ",\"msg\":\"\"}");
out.close();
} catch (Exception e) {
e.printStackTrace();
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("{\"code\":1,\"data\":[],\"msg\":\"查询失败!\"}");
out.close();
}
}
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
StudentDao dao = new StudentDao();
try {
dao.delete(id);
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("{\"code\":0,\"data\":[],\"msg\":\"\"}");
out.close();
} catch (Exception e) {
e.printStackTrace();
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("{\"code\":1,\"data\":[],\"msg\":\"删除失败!\"}");
out.close();
}
}
}
```
3. DAO 类:StudentDao.java
```java
package com.example.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.example.bean.Student;
import com.example.util.DBUtil;
public class StudentDao {
public List<Student> queryAll() throws Exception {
List<Student> list = new ArrayList<Student>();
String sql = "SELECT id, name, age FROM student_info ORDER BY id ASC";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt("id"));
stu.setName(rs.getString("name"));
stu.setAge(rs.getInt("age"));
list.add(stu);
}
} finally {
DBUtil.close(rs, ps, conn);
}
return list;
}
public void delete(int id) throws Exception {
String sql = "DELETE FROM student_info WHERE id=?";
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
} finally {
DBUtil.close(ps, conn);
}
}
}
```
4. JavaBean 类:Student.java
```java
package com.example.bean;
public class Student {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
其中,JSP 页面中使用了 jQuery 库和 Ajax 技术,向 Servlet 发送请求并接收响应,使用 JavaScript 技术将响应数据解析并显示在页面上。Servlet 程序中根据请求参数的不同分别调用 DAO 类中的方法进行数据库操作,然后将操作结果以 JSON 格式返回给 JSP 页面。DAO 类中使用 JDBC 连接数据库,执行 SQL 语句,将结果封装成 JavaBean 对象。
阅读全文