【发布时间】:2014-08-02 05:16:41
【问题描述】:
您不必浏览完整的代码。只需查看 body 标记内的第一个表分配和 document.write() 即可。我在一个脚本中使用 document.write() 创建了完整的页面。在代码的末尾,您可以看到在表单关闭之前有提交按钮。当我运行页面并单击提交按钮时,它不会转到 servlet...这里有什么问题?
<%@page import="s_pack.dbManager"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<script language = "javascript" type = "text/javascript">
table = '<form action = "AM2Servlet" method = "get" >'
document.write(table);
table = '<select name = "selected_course_id_in_marking">'
document.write(table);
var c_id,s_name;
<%
String co_id = request.getParameter("course_id_QSParam");
System.out.println(co_id);
%>
c_id = '<%=co_id%>'
document.write('<option>'+c_id+'</option>');
table = '</select>'
document.write(table);
table = '<select name = "selected_student_in_marking">'
document.write(table);
<%
String due_student_list[] = dbManager.get_students_with_due_result(co_id);
for(int i=0;i<due_student_list.length;i++)
{
%>
s_name = '<%= due_student_list[i]%>'
document.write('<option>'+ s_name + '</option>');
<%
}
%>
table = '</select>'
document.write(table);
table = '<table border="1" cellspacing="1" cellpadding="5">'
document.write(table);
<%
for(int i=1;i<6;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> In Course '+temp
table = table + ' Mark: <select name = "InCourse'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=30;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<2;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Assignment '
table = table + 'Mark: <select name = "Assignment'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=10;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<2;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Project '
table = table + 'Mark: <select name = "Project'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=40;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<2;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Lab '
table = table + 'Mark: <select name = "Lab'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=60;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<3;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Viva '+temp
table = table + ' Mark: <select name = "Viva'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=10;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<2;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Attendance '
table = table + 'Mark: <select name = "Attendance'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=10;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
<%
for(int i=1;i<2;i++)
{
%>
temp = '<%=i%>'
table = '<tr><td> Semester Final '
table = table + 'Mark: <select name = "SemesterFinal'+temp+'Mark">'
document.write(table);
for(var mark=0; mark<=60;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select></tr>');
<%
}
%>
table = '</table></form>'
document.write(table);
table = '<hr> </br></br>'
table = table + '<h1> Additional Exams </h1> <hr>'
document.write(table);
table = 'Name of Exam: <input type = "text" name="AdditionalExam1"> Obtained Mark:'
document.write(table);
table = '<select name = "AdditionalExam1Mark">'
document.write(table);
for(var mark=0; mark<=100;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select>');
table = 'Name of Exam: <input type = "text" name="AdditionalExam2"> Obtained Mark:'
document.write(table);
table = '<select name = "AdditionalExam2Mark">'
document.write(table);
for(var mark=0; mark<=100;mark=mark+.5)
{
document.write("<option>"+mark+"</option>");
}
document.write('</select>');
table = '<input type="submit" value="Assign Marks"></form>'
document.write(table);
</script>
</body>
</html>
【问题讨论】:
-
您确定您正在处理 servlet 中的 get 方法吗?并删除不必要的代码,而是发布一些从 servlet 生成的 html 和处理程序方法的 sn-p,好吗?
-
而且你不应该在jsp中做这一切,这是不好的坏习惯......
-
我是 jsp 新手。这是我第一次使用这个。
-
尽量避免使用 Scriplet,而是使用 JSTL 或表达式语言来避免此类错误。 Scriplet 将 HTML 和 JAVA 混合在一起,使找出问题变得复杂。将业务逻辑和用户界面逻辑放在不同的层中。
-
首先这里不会用到document.write,因为它会用write方法中给定的字符串替换整个页面
标签: javascript jsp servlets forms scriptlet