我是 webdev 新手,有一个问题。 我不知道如何将 javascript var 从 jsp 传递到 servlet。 这是我的 jsp 和 javascript 代码:

<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,但我完全不知道它是如何工作的以及我应该如何使用它。

请您参考如下方法:

使用input<type="hidden" id ="codEvento" name="codEvento"/>在 jsp 中并在 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");


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!