我是webdev的新手,但遇到了问题。 我不知道如何将JavaScript变量从jsp传递到servlet。 这是我的带有JavaScript代码的jsp:
<form action="http://localhost:8080/PerryBet/EventoServlet" method="post">
<input type="hidden" id ="deleteForm" name="action" value="delete">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">Palinsesto</th>
<th scope="col">Evento</th>
<th scope="col">Campionato</th>
<th scope="col">Data</th>
<th scope="col">Squadra Casa</th>
<th scope="col">Squadra Casa</th>
<th scope="col"> 1 </th>
<th scope="col"> X </th>
<th scope="col"> 2 </th>
<th scope ="col"> Modifica </th>
<th scope ="col"> Elimina </th>
</tr>
</thead>
<%
EventoModel model = new EventoModel();
Collection<EventoBean> eventi = new ArrayList<EventoBean>();
eventi = model.doRetrieveAll();
if(eventi != null & eventi.size() > 0) {
Iterator<?> it = eventi.iterator();
while (it.hasNext()) {
EventoBean bean = (EventoBean)it.next();
%>
<tbody>
<tr>
<td><%= bean.getEvento() %></td>
<td><%= bean.getPalinsesto() %></td>
<td><%= bean.getCampionato()%></td>
<td><%= bean.getData()%></td>
<td><%= bean.getSquadraCasa() %></td>
<td><%= bean.getSquadraTrasferta() %></td>
<td><%= bean.getQuota1()%></td>
<td><%= bean.getQuotaX()%></td>
<td><%= bean.getQuota2() %></td>
<td><button type="submit" >Elimina</button></td>
</tr>
</tbody>
<%
}
}
%>
</table>
</form>
<script type="text/javascript">
$('button').click(function () {
// Get the first td
var codEvento = $(this).closest('tr').children('td:eq(1)').text();
alert('Codice Evento: ' + codEvento);
});
</script>
servlet:
package controller;
import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
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 bean.EventoBean;
import model.EventoModel;
/**
* Servlet implementation class EventoServlet
*/
@WebServlet("/EventoServlet")
public class EventoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
EventoModel model = new EventoModel();
/**
* @see HttpServlet#HttpServlet()
*/
public EventoServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
EventoBean bean = new EventoBean();
try {
if(action != null) {
if(action.equalsIgnoreCase("delete")) {
System.out.println("test");
String cod = (String)request.getParameter("codEvento");
System.out.println(cod);
//model.eliminaEvento(evento);
System.out.println("deleted");
}
} else {
bean.setPalinsesto((String)request.getParameter("palinsesto"));
bean.setEvento((String)request.getParameter("evento"));
bean.setCampionato((String)request.getParameter("campionato"));
bean.setData(setData(request));
bean.setSquadraCasa((String)request.getParameter("squadraCasa"));
bean.setSquadraTrasferta((String)request.getParameter("squadraTrasferta"));
bean.setQuota1((String) request.getParameter("quota1"));
bean.setQuotaX((String) request.getParameter("quotaX"));
bean.setQuota2((String) request.getParameter("quota2"));
model.aggiungiEvento(bean);
request.getRequestDispatcher("Eventi.jsp").forward(request, response);
}
} catch (SQLException e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public Date setData(HttpServletRequest request) {
String startDateStr = request.getParameter("data");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
java.util.Date parsed = null;
try {
parsed = sdf.parse(startDateStr);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date sqlDate = new Date(parsed.getTime());
return sqlDate;
}
}
现在,我想将var codEvento传递给servlet,该怎么办? 是否有类似setAttribute之类的方法? 我尝试过使用ajax,但是我完全不知道它是如何工作的,以及我应该如何使用它。
答案 0 :(得分:1)
在jsp中使用input<type="hidden" id ="codEvento" name="codEvento"/>
,然后在您的javascript代码中设置此值,例如
<script type="text/javascript">
$('button').click(function () {
// Get the first td
var codEvento = $(this).closest('tr').children('td:eq(1)').text();
document.getElementById("codEvento").value= codEvento
alert('Codice Evento: ' + codEvento);
});
</script>
然后在servlet中,您可以获取以下值:
String codEvento = request.getParameter("codEvento");