我想使用servlet将用户的输入存储在数据库中。 我尝试添加一个具有属性操作的表单标记,以便将其与webservlet注释一起映射,但它不起作用。我需要帮助来解决这个问题。
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$('#create_btn').click(function () {
$('#textArea').show();
$('#post_btn').show();
$('#cancel_btn').show();
});
$('#cancel_btn').click(function () {
$('#textArea').hide('slow');
$('#post_btn').hide('slow');
$('#cancel_btn').hide('slow');
});
$('#post_btn').click(function () {
// body...
var note = $('#textArea').val();
$('<li>').text(note).prependTo('.notes');
$('#textArea').val('');
$('#post_btn').addClass('disabled');
});
$('#textArea').keyup(function() {
var postLength = $(this).val().length;
if (postLength <= 0) {
$('#post_btn').addClass('disabled');
}
else
{
$('#post_btn').removeClass('disabled');
}
});
$('#post_btn').addClass('disabled');
});
</script>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav"></ul>
<form class="navbar-form navbar-left" role="search" action="SearchServlet">
<div class="form-group">
<input type="text" class="form-control" name="search_text" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="SignOutServlet">Sign Out</a></li>
</ul>
</div>
</div>
</nav>
<form action="CreateServlet">
<div class="container">
<div class="row">
<!--<button type="button" data-toggle="modal" data-target="#textArea">Create</button>-->
<a href="#" class="btn btn-primary btn-lg" id="create_btn">Create</a>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<div class="col-lg-10">
<textarea class="form-control" rows="3" name = "Note_Area" id="textArea" placeholder="Write down something here..." style="display: none; margin-top: 10px;"></textarea>
<grammarly-btn><div style="z-index: 2; opacity: 1; transform: translate(408.156px, 43px);" class="_9b5ef6-textarea_btn _9b5ef6-anonymous _9b5ef6-not_focused" data-grammarly-reactid=".0">
<div class="_9b5ef6-transform_wrap" data-grammarly-reactid=".0.0">
<div title="Protected by Grammarly" class="_9b5ef6-status" data-grammarly-reactid=".0.0.0"> </div>
</div>
<span class="_9b5ef6-btn_text" data-grammarly-reactid=".0.1"></span></div></grammarly-btn>
<a href="#" class="btn btn-primary" id = post_btn style="display: none; ">Post</a>
<a href="#" class="btn btn-primary" id="cancel_btn" style="display: none;">Cancel</a>
</div>
</div>
</div>
<ul class="notes"></ul>
</div>
</div>
</form>
</body>
</html>
java代码:
import java.io.IOException;
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 javax.servlet.http.HttpSession;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
@SuppressWarnings("serial")
@WebServlet(description = "For creating notes", urlPatterns = { "/CreateServlet" })
public class CreateServlet extends HttpServlet {
private int i=0;
private String note;
private String email;
private String password;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection con = null;
PreparedStatement ps = null;
response.setContentType("text/html");
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://LENOVO:1433;database=Minor;integratedSecurity=true;");
note = request.getParameter("Note_Area");
System.out.println(note);
HttpSession session = request.getSession(false);
if(session != null)
{
String id = request.getRequestedSessionId();
System.out.println(id);
email =(String)session.getAttribute("emailadd");
password=(String)session.getAttribute("password");
System.out.println(email);
System.out.println(password);
ps = con.prepareStatement("insert into Notes(MailAdd,Note) values(?,?)");
ps.setString(1, email);
ps.setString(2, note);
i = ps.executeUpdate();
if(i==0)
{
System.out.println("No note has been saved");
response.sendRedirect("index.html");
}
else
{
System.out.println("Note has been saved");
response.sendRedirect("index.html");
}
}
else
{
response.sendRedirect("signin.html");
System.out.println("Session Expired!!!");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
con.close();
ps.close();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
}
}