1

I am trying to create a web application,where we have a html page with table containing rows and a submit button. When clicked on the submit button the table will be displayed .So I need to use servlets, But I am not sure where I can write the onclick button option and how to relate that to servlet class. Thanks in advance

Here are my files: //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 page:

<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>
joegale
  • 11
  • 3

1 Answers1

0

To call the Table servlet, add the action attribute in your form tag and give the location to your servlet.

<form action="/demo">
<input type = "submit" name = "submit" value = "submit">
</form>
lambad
  • 1,046
  • 10
  • 21