我在使用AJAX的JSP中没有刷新页面的发送表单时遇到问题。如何创建AJAX来发送此表单,从index.jsp到send.jsp而不刷新页面?谢谢你的所有答案。
这是表单代码,没有AJAX,它可以工作。 (的的index.jsp )
<form id="dom-realitka-notifikacia" action="send.jsp" method="post" >
<div class="stred">
<b class="cierna">E-mail:</b>
<input type="text" class="form-control form-rovno" id="email" name="email" placeholder="Sem napíšte e-mail">
<input type="hidden" value="domrealitka" id="tabulka" name="tabulka">
<input type="hidden" value="<%= rs.getInt("id") %>" id="id_realitka" name="id_realitka">
<button type="submit" class="btn btn-success">Odoslať</button>
</div>
</form>
这是( send.jsp ),没有AJAX工作并插入MYSQL。
page contentType="text/html" pageEncoding="UTF-8"
page import="javax.mail.*"
page import="javax.mail.internet.*"
page import="javax.sql.*"
page import="javax.naming.Context"
page import="javax.naming.InitialContext"
page import="java.sql.*"
request.setCharacterEncoding("UTF-8");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String email = request.getParameter("email");
int id_realitka = Integer.valueOf(request.getParameter("id_realitka"));
String tabulka = request.getParameter("tabulka");
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("nacitala sa driver");
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/janko");
con = ds.getConnection();
System.out.println("nacitala sa databaza");
String sql = "INSERT INTO janko.notifikacia (email, id_realitka, tabulka) VALUES ( ?, ?, ?)";
ps = con.prepareStatement(sql);
ps.setString(1, email);
ps.setInt(2, id_realitka);
ps.setString(3, tabulka);
ps.executeUpdate();
con.close();
}
catch(ClassNotFoundException e1) //ClassNotFoundException dame Class.forName a klikneme ctrl+space a e1 dopiseme hocico
{
System.out.println(e1.getMessage()); //vypis spravy e1 do konzoly
}
catch(SQLException e2) //SQLException mame z DriverManager.getConnection po kliknuti ctrl+space a e2 dopiseme hocico
{
System.out.println(e2.getMessage()); //vypis spravy e2 do konzoly
}
finally{
con = null;
rs = null;
ps = null;
}
答案 0 :(得分:0)
可以使用jQuery轻松动态提交表单。只需在文件中包含jQuery,然后将以下代码粘贴到HTML文件中即可。
# app/views/profiles/_transcript_fields.html.erb
<% if f.object.persisted? %>
<%= f.object.name %>
<% else %>
<%= f.file_field :attachment, class: 'col-lg-4 form-control' %>
<% end %>
此代码将序列化您的表单数据并将其发送到相关的servlet。 servlet代码保持不变。