js
writer.println(" <tr>\n" +" <td>" + department_id + "</td>\n" +
" <td>" + department_name + "</td>\n" +
" <td>" + manager_id +"</td>\n" +
" <td>"+ City + "</td>\n" +
" <td><a href=\"/Servlet4/dept/detail?department_id="+department_id + "\" target=\"_black\">详情</a></td>\n"+
" <td><a href='javascript:void(0)' onclick='del(" + department_id + ")'>删除</a></td>\n"+
" <td><a href='javascript:void(0)' onclick='Update(" + City + ")'>修改</a></td>\n"+
" </tr>");
有颜色的那一行代码点击后无法执行语句
如果把City换为department_id就可以执行
两个都是String类型 界面
修改点后无反应,换成department_id就有反应 她与晚风 发表于 2023-4-24 16:23
界面
修改点后无反应,换成department_id就有反应
把全部代码打包发出来吧 本帖最后由 她与晚风 于 2023-4-24 16:36 编辑
wp231957 发表于 2023-4-24 16:27
把全部代码打包发出来吧
package coco;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class DeptListServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("-------------------------------");
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println(" <script>\n" +
" function del(department_id){\n" +
" var bool = window.confirm('确定删除吗?');\n" +
" if(bool){\n" +
" alert('ok,正在执行删除操作');\n" +
" document.location.href='/Servlet4/dept/delete?department_id=' + department_id;\n" +
" }else {\n" +
" alert('好的,已取消操作');\n" +
" }\n" +
" };\n" );
writer.println(" function Update(city){\n" +
" var bool = window.confirm('确定要修改吗?');\n" +
" if(bool){\n" +
" var key = prompt('请输入要修改的属性名称:');\n" +
" var value = prompt('请输入新的' + key + ':');\n" +
" if(key == 'department_name'){\n" +
"\n" +
" }else if(key == 'manager_id'){\n" +
"\n" +
" }else if(key == 'city'){\n" +
" document.location.href='/Servlet4/dept/update?city1=' + city + '&city2=' + value;\n" +
" }\n" +
" else {\n" +
" alert('输入参数错误')\n" +
" }\n" +
" }\n" +
" }\n" +
" </script>");
writer.println("<h1 align=\"center\">welcome dept html</h1>\n" +
" <table border=\"1px\" cellpadding=\"30px\" cellspacing=\"0\">\n" +
" <tr>\n" +
" <th>Department_id</th>\n" +
" <th>Department_name</th>\n" +
" <th>Manager_id</th>\n" +
" <th>City</th>\n" +
" <th colspan='3'>Function</th>\n" +
" </tr>");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myemployees","root","123456");
preparedStatement = connection.prepareStatement("select department_id,department_name,manager_id,city from departments a left join locations b on a.location_id=b.location_id");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String department_id = resultSet.getString("department_id");
String department_name = resultSet.getString("department_name");
String manager_id = resultSet.getString("manager_id");
String City = resultSet.getString("city");
writer.println(" <tr>\n" +
" <td>" + department_id + "</td>\n" +
" <td>" + department_name + "</td>\n" +
" <td>" + manager_id +"</td>\n" +
" <td>"+ City + "</td>\n" +
" <td><a href=\"/Servlet4/dept/detail?department_id="+department_id + "\" target=\"_black\">详情</a></td>\n"+
//" <td><a href='#' onclick='del(" + department_id + ")'>删除</a></td>\n"+
" <td><a href='javascript:void(0)' onclick='Update(" + City + ")'>修改</a></td>\n"+
" </tr>");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
writer.println("</table>");
}
}
wp231957 发表于 2023-4-24 16:27
把全部代码打包发出来吧
这个限制发送大小发不过去,问题出在script Update函数下应该是,其它都正常 wp231957 发表于 2023-4-24 16:27
把全部代码打包发出来吧
这个链接点不了 本帖最后由 isdkz 于 2023-4-24 16:40 编辑
问题的根本原因在于 `onclick` 事件传递参数的方式。当传递 `City` 参数时,它没有被引号包围,因此浏览器无法正确解析。为了解决这个问题,需要在将 `City` 传递给 `Update()` 函数时使用引号将其包围。这样,当 `City` 是一个字符串时,浏览器将能正确解析它。修改后的代码如下:
writer.println(" <tr>\n" +
" <td>" + department_id + "</td>\n" +
" <td>" + department_name + "</td>\n" +
" <td>" + manager_id +"</td>\n" +
" <td>"+ City + "</td>\n" +
" <td><a href=\\"/Servlet4/dept/detail?department_id="+department_id + "\\" target=\\"_black\\">详情</a></td>\n"+
" <td><a href='javascript:void(0)' onclick='del(" + department_id + ")'>删除</a></td>\n"+
" <td><a href='javascript:void(0)' onclick='Update(\\"" + City + "\\")'>修改</a></td>\n"+
" </tr>");
注意,在 `onclick='Update(\"" + City + "\")'` 部分,我们使用了转义字符 `\` 来在单引号内包含双引号。这将确保 `City` 作为字符串参数传递给 `Update()` 函数。现在,点击修改链接应该可以正确执行 `Update()` 函数。 isdkz 发表于 2023-4-24 16:37
问题的根本原因在于 `onclick` 事件传递参数的方式。当传递 `City` 参数时,它没有被引号包围,因此浏览器 ...
city本身就是一个字符串 isdkz 发表于 2023-4-24 16:37
问题的根本原因在于 `onclick` 事件传递参数的方式。当传递 `City` 参数时,它没有被引号包围,因此浏览器 ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function del(department_id){
var bool = window.confirm('确定删除吗?');
if(bool){
alert('ok,正在执行删除操作');
document.location.href='/Servlet4/dept/delete?department_id=' + department_id;
}else {
alert('好的,已取消操作');
}
}
function update(city){
alert(typeof city);
/*
var bool = window.confirm('确定要修改吗?');
if(bool){
var key = prompt('请输入要修改的属性名称:');
var value = prompt('请输入新的' + key + ':');
if(key == 'department_name'){
}else if(key == 'manager_id'){
}else if(key == 'city'){
document.location.href='/Servlet4/dept/update?city1=' + city + '&city2=' + value;
}
else {
alert('输入参数错误')
}
}*/
}
</script>
</head>
<body>
<a href="javascript:void(0)" onclick="update('hjk')">s</a>
<a href="javascript:void(0)" onclick="del(2)">s</a>
</body>
</html>
isdkz 发表于 2023-4-24 16:37
问题的根本原因在于 `onclick` 事件传递参数的方式。当传递 `City` 参数时,它没有被引号包围,因此浏览器 ...
我单拿到html页面里面没问题,一放到Servlet里面就执行不了了 她与晚风 发表于 2023-4-24 16:40
我单拿到html页面里面没问题,一放到Servlet里面就执行不了了
根据您提供的完整 HTML 代码,我没有发现任何问题。然而,您提到在将其放入 Servlet 中时,代码无法正常执行。这可能是由于在将 HTML 代码插入 Servlet 时出现了问题。以下是使用 `PrintWriter` 将 HTML 代码插入 Servlet 的示例:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html lang=\\"en\\">");
out.println("<head>");
out.println(" <meta charset=\\"UTF-8\\">");
out.println(" <title>Title</title>");
out.println(" <script>");
// 注意:在这里插入 JavaScript 代码
out.println(" </script>");
out.println("</head>");
out.println("<body>");
// 注意:在这里插入其他 HTML 代码
out.println("</body>");
out.println("</html>");
out.close();
}
}
请确保您已正确插入 JavaScript 和 HTML 代码。如果仍然无法正常执行,请检查以下方面:
1. 确保 Servlet 配置正确。请检查 `web.xml` 文件,确保 Servlet 映射正确。
2. 检查浏览器的开发者控制台,查看是否有 JavaScript 错误或其他相关错误。
3. 清除浏览器缓存,确保加载的是最新的 Servlet 输出。
如果问题仍然存在,请提供更多关于您的 Servlet 代码和配置的详细信息,以便我能更好地帮助您解决问题。 isdkz 发表于 2023-4-24 16:37
问题的根本原因在于 `onclick` 事件传递参数的方式。当传递 `City` 参数时,它没有被引号包围,因此浏览器 ...
package coco;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class DeptListServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<!DOCTYPE html>");
writer.println("<html lang=\"en\">");
writer.println("<head>");
writer.println("<meta charset=\"UTF-8\">");
writer.println(" <title>Title</title>");
writer.println(" <script>");
writer.println(" function update(city1){");
writer.println(" var city2 = prompt('请输入修改后的城市名:');");
writer.println(" if(window.confirm('你确定要将' + city1 + '修改为' + city2 + '吗?')){");
writer.println(" alert('ok,正在执行操作');");
writer.println(" document.location.href='/Servlet4/dept/update?city1=' + city1 + 'city2=' + city2;");
writer.println(" }else {");
writer.println(" alert('取消成功');");
writer.println(" }");
writer.println(" }");
writer.println(" function Delete(department_id){");
writer.println(" if(window.confirm('确定要执行删除操作吗?')){");
writer.println(" alert('Ok,正在执行删除操作');");
writer.println(" document.location.href='/Servlet4/dept/delete?department_id='+'department_id'");
writer.println(" }else {");
writer.println(" alert('已确认取消');");
writer.println(" }");
writer.println(" }");
writer.println(" </script>");
writer.println("</head>");
writer.println("<body>");
writer.println(" <table border='1px' cellspacing='0' cellpadding='30px'>");
writer.println(" <tr>");
writer.println(" <th>department_id</th>");
writer.println(" <th>department_name</th>");
writer.println(" <th>manager_id</th>");
writer.println(" <th>city</th>");
writer.println(" <th colspan='3'>Function</th>");
writer.println(" </tr>");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myemployees","root","123456");
preparedStatement = connection.prepareStatement("select department_id,department_name,manager_id,cityfrom departments a left join locations b on a.location_id=b.location_id");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String department_id = resultSet.getString("department_id");
String department_name = resultSet.getString("department_name");
String manager_id = resultSet.getString("manager_id");
String City = resultSet.getString("city");
writer.println(" <tr>");
writer.println(" <td>"+ department_id +"</td>");
writer.println(" <td>"+ department_name +"</td>");
writer.println(" <td>"+ manager_id +"</td>");
writer.println(" <td>"+ City +"</td>");
writer.println(" <td><a href='javascript:void (0)' onclick= 'Delete(" +department_id+")' target='_black'>update</a></td>>");
writer.println(" <td><a href='javascript:void (0)' onclick= 'update("+City+")' target='_black'>delete</a></td>");
writer.println(" <td><a href='/Servlet4/dept/detail?department_id=' + " + department_id +" target='_black'>详情</a></td>");
writer.println(" </tr>");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
writer.println(" </table>");
writer.println("</body>");
writer.println("</html>");
}
} isdkz 发表于 2023-4-24 16:43
根据您提供的完整 HTML 代码,我没有发现任何问题。然而,您提到在将其放入 Servlet 中时,代码无法正常 ...
放这个里面还是有问题,不知道错哪里了 她与晚风 发表于 2023-4-24 19:09
放这个里面还是有问题,不知道错哪里了
我发现了一个问题,您在 `onclick` 属性中传递给 `update` 函数的 `City` 参数没有用引号包围。请尝试将以下代码行:
writer.println(" <td><a href='javascript:void (0)' onclick= 'update("+City+")' target='_black'>delete</a></td>");
更改为:
writer.println(" <td><a href='javascript:void (0)' onclick= 'update(\\""+City+"\\")' target='_black'>delete</a></td>");
同时,还需要修改以下这行代码:
writer.println(" <td><a href='javascript:void (0)' onclick= 'Delete(" +department_id+")' target='_black'>update</a></td>>");
将 `update` 改为 `delete` 并去掉多余的尖括号:
writer.println(" <td><a href='javascript:void (0)' onclick= 'Delete(" +department_id+")' target='_black'>delete</a></td>");
这应该解决您遇到的问题。请确保在传递字符串参数时使用引号包围参数。如果问题仍然存在,请提供更多详细信息,以便我可以进一步帮助您。
页:
[1]