我正在尝试创建一个Web应用程序,其中我们有一个包含行和提交按钮的表的html页面。单击提交按钮时,将显示该表。因此,我需要使用servlet,但我不确定在哪里可以编写onclick按钮选项以及如何将其与servlet类相关联。提前致谢
以下是我的文件: //Table.java ----- servlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
class Table extends HttpServlet {
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try{
RequestDispatcher rd = request.getRequestDispatcher("index.html");
rd.forward(request, response);
}
finally{
out.close();
}
}
}
的index.html:
<!DOCTYPE HTML>
<html>
<head>
<h1>Student</h1>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Major</th>
<th>GPA</th>
<th>hours</th>
</tr>
<tr>
<td>1</td>
<td>Allen</td>
<td>Physics</td>
<td>3.0 </td>
<td>19</td>
</tr>
<tr>
<td>2</td>
<td>Brad</td>
<td>Computer Sciences</td>
<td>3.4 </td>
<td>30</td>
</tr>
<tr>
<td>3</td>
<td>Caroll</td>
<td>Finance</td>
<td>3.8 </td>
<td>22</td>
</tr>
<tr>
<td>4</td>
<td>Fred</td>
<td>Arts</td>
<td>3.5</td>
<td>24</td>
</tr>
<td valign="bottom" align="right">
<form><input type = "submit" name = "submit" value = "submit"></form>
</td>
</body>
</html>
web.xml页面:
<servlet>
<servlet-name>xyz</servlet-name>
<servlet-class>Table</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xyz</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
要调用Table servlet,请在表单标记中添加action属性,并将该位置提供给servlet。
<form action="/demo">
<input type = "submit" name = "submit" value = "submit">
</form>